• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

equals() question?

 
Ernesto Leyva
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
bart zagers
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12100
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
chloe wong
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
==

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
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic