May as well note, since you may not know if you have to check a vars equality against a String that you know ALWAYS do it with the var in the equals like the following:

Code:

String whatever = "turkey";

if ("chicken".equals(whatever)) {
...
}

as opposed to:

Code:

if (whatever.equals("chicken")) {..}

Reason is nullys but yeah

01-02-2010, 12:23 AM

Lil_Aziz1

Reason you can't use String1 == String2 is because they are objects, not primitive data. Since String is an object and not primitive data, String1 == String2 compares their address instead of their values (which is what you're trying to do). You can do int1 == int2 or char1 == char2 or double1 == double2 to compare values because int, char, and double are primitive data.

01-02-2010, 01:13 AM

artemff

Lil_Aziz1, adz, Thanks.

01-02-2010, 05:12 AM

Lil_Aziz1

ur welcome

08-28-2011, 10:31 AM

j2me64

Quote:

Originally Posted by Lil_Aziz1

Reason you can't use String1 == String2 is because they are objects, not primitive data. Since String is an object and not primitive data, String1 == String2 compares their address instead of their values (which is what you're trying to do). You can do int1 == int2 or char1 == char2 or double1 == double2 to compare values because int, char, and double are primitive data.

but why does the following code returns true:

Code:

String s1 = "hello";
String s2 = "hello";
System.out.println(s1==s2);

08-28-2011, 11:51 AM

lok pun

String a , b;

a.equal(b);

a==b

those are applicable try it

08-29-2011, 01:37 AM

Junky

Quote:

Originally Posted by j2me64

but why does the following code returns true:

Are you asking because you do not know or are you asking to improve the knowledge of n00bs?

08-29-2011, 08:00 AM

DarrylBurke

Zombie alert. j2me64, please stop resurrecting zombies. This is the second I've seen today.