aspose file tools*
The moose likes Beginning Java and the fly likes equals() question? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "equals() question?" Watch "equals() question?" New topic
Author

equals() question?

Ernesto Leyva
Ranch Hand

Joined: Feb 23, 2006
Posts: 62
Hello

Let say I have this code:

String x = "abc";

is there any problems if I do this:

if (x!=null && x=="abc") System.out.print("Hello World!");

what's the reason I should do this, if the first one work.

if (x!=null && x.equals("abc")) System.out.print("Hello World!");

thanks
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Ernesto Leyva:
...what's the reason I should do this, if the first one work...

Well, if the first one will work...

But it's not always obvious that this is the case. See Strings Literally. To be certain of the comparison, and to make the intent of your code clear to others, it's better to use equals when comparing Strings.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
bart zagers
Ranch Hand

Joined: Feb 05, 2003
Posts: 234
And you are aware you can also just write

to get the same result?
Whether or not you want to do this is another discussion.
damien malone
Ranch Hand

Joined: May 06, 2003
Posts: 35
when doing comparisons, its always nicer to put the constant first(i.e. "abc") as this will prevent null pointer errors
Adam Schaible
Ranch Hand

Joined: Oct 04, 2007
Posts: 101
Originally posted by Ernesto Leyva:
Hello

Let say I have this code:

String x = "abc";

is there any problems if I do this:

if (x!=null && x=="abc") System.out.print("Hello World!");

what's the reason I should do this, if the first one work.

if (x!=null && x.equals("abc")) System.out.print("Hello World!");

thanks


So I think the question is: Why use .equals() if == works?

Here's the short:
The == operator returns true if the references on either side refer to the same object. It's known as the identity operator.

The equals() method is known as the equality operator. Lets say we Dogs, same breed, same name, height, weight, color, etc - our program might say those dogs are equal - but they are definately different dogs, so the identity operator would say they are different - the equals method would say they are the same.

The reason it works for String's is due to the way Strings are placed in the constants pool (Interning) - see Strings Literally as Mark Weber has suggested. The short summary is that most strings that can be determined at compile time will be placed in memory, and whenever you attempt to instantiate another String that's equal to one in the String pool, you'll be given a reference to the one that already exists.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11229
    
  16

Here's the shorter:

== won't always work. unless and until you understand the difference, and know exactly what you are doing, .equals() is the correct way to go.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
chloe wong
Greenhorn

Joined: Aug 02, 2006
Posts: 17
==

This operator compares two object references to see whether they refer to the same instance. Meaning to say, it is used to check whether the two objects are the same object or not.

String s1 = new String(�mickey�);

String s2 = new String(�mickey�);

System.out.println(s1==s2); ��> returns false

String s3 = s1;

System.out.println(s3==s1); ��-> returns true

equals

This method creates two char arrays and puts the characters of each String objects in separate array and then performs the comparison.

String f1 = �mouse�;

String f2 = �mouse�;

String f3 = �MOUSE�;

System.out.println(f1.equals(f2)); ��> returns true

System.out.println(f1.equals(f3)); ��> returns false

equalsIgnoreCase

The special about this is it�ll do comparison on two String but ignoring the case of them.

String f1 = �mouse�;

String t1 = �cartoon�;

String t3 = �CARTOON�;

System.out.println(t1.equals(t2)); ��> returns true
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by chloe wong:
it is used to check whether the two objects are the same object or not.


<PEDANT>
it is used to check whether the two object references are the same object or not
</PEDANT>


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: equals() question?