If statements comparing a string to a string? (a = "text" doesn't work)

Hiya - I’ve been lurking the forums for a while looking for good re-introductions to Java programming (I’ve done a bit of C# about a year ago).

Anyway - I’ve been following a series of videos on TheNewBoston on Java, and I’ve hit a bit of a dead end.

I am trying to compare the contents of a text String to a String value in an If statement, but I can’t seem to get it to acknowledge that the two are the same - Could you try and explain what I am doing wrong? The video only deals with Int comparing which is pretty simple to understand, compared to my own little attempt.

import java.util.Scanner;

class inputtest {
	public static void main(String arg[]) {
		Scanner userinput = new Scanner(System.in);
		String ishumantest;
		
		System.out.println("Are you a human? If so type in yes");
		ishumantest = userinput.nextLine();
		// FOR DEBUGGING INPUT
		//System.out.println();
		if (ishumantest == "yes")
		{
			System.out.println("You are welcome to enter.");
		}
		else
		{
			System.out.println("Non-Humans not allowed.");
		}
	}
}

== checks for identical references, not for object equality


boolean eq = new String("abc") == "abc"; // eq = false;
boolean eq = new String("abc").equals("abc"); // eq = true;

Oh I see - he failed to mention that. Your suggestion works perfectly. :smiley:

Because String is kinda special. new String(“abc”) is not same with constant “abc” itself. To take it rude, “there two of them on memory”.

That’s what I remember from reading about how bad String is. That’s why some are using StringBuilder.

Not exactly. StringBuilder provides means to concatenate Strings without creating new Strings over and over, since Strings are immutable. Has nothing todo with comparison.

As a rule of thumb: use .equals() on all types starting with a capital letter.

Like they said above. I just wanted to throw in there that Object.equals(otherObj) is used when comparing OBJECTS. == is used ONLY when comparing primitive data types (float, int, byte, etc.). Every time you use a java object class, you will have the .equals method available (Since they all implement Comparable - Look into doing this if you want to make you’re own .equals method).

ToXSiK 8)

Not true, sometimes you have to check if an object is null (and calling equals() on a null object will throw an exception), or you want to see if two objects are just the same references.

Also wrong in the fact that .equals() is not in Comparable, but in Object, which is the superclass of ALL classes :wink:

You’re writing C# :stuck_out_tongue:

This is one of the things that trips a lot of new Java programmers up, so the designers of C# decided to put in a special case for strings. So people who move from C# to Java are now even more likely to trip up than people who start Java with no prior knowledge of any language.

Sorry, I meant in object. We all make typos 8)

[quote]Not true, sometimes you have to check if an object is null (and calling equals() on a null object will throw an exception), or you want to see if two objects are just the same references.
[/quote]
But by calling .equals(), whether you are checking if it is null or not or if it is just a reference, you still must use .equals for situations dealing with objects… All I am saying.

I use String.compare().

compareTo() is only useful for comparing two Strings lexicographically (by letter value).
equals() is best for comparing equality.

I feel like nobody has said what’s really important to learn here.

The reason why you can’t use == for a String in Java is because a String is an Object, not a primitive. If you call == on an object, you are just comparing the memory addresses of pointers. So, if both pointers are at the same memory address, then it’s true, otherwise it’s false. When calling the equals() method, you are actually comparing the objects that lie at the memory you are pointing to, using a method that each object has its own implementation of. A string for example compares every single letter for equality (or maybe it does a hash of some kind, I’m not sure).

Primitives are:
int
boolean
float
double
byte
char

Everything else is an object. Just think of == as “the exact same object” and .equals() as “have the same value.” Also, you don’t need to use == to check for null (even though I would do it that way), because myObj.equals(null) will also be false.

[quote=“Eli Delventhal,post:13,topic:36537”]
short
long

Forgot “short” and “long” :wink:

you can forget short but not long, it’s important for updating delta time on game loop :smiley:

But by calling .equals(), whether you are checking if it is null or not or if it is just a reference, you still must use .equals for situations dealing with objects… All I am saying.
[/quote]
That’s not the problem, the problem is that you said:

[quote]== is used ONLY when comparing primitive data types
[/quote]
which is not true.

[quote=“pjt33,post:14,topic:36537”]

;D

Thanks, I knew I was forgetting a couple.