0

Why is this false?

String str1 = new String("Java    ");
String str2 = str1;
System.out.println(str1.trim()==str2.trim());  //false

Initially str2 was referencing str1 object. So, comparing with == will return true for str1==str2

Then why is this false with .trim() method?

Even it returns false for literals (without new keyword)

String str1 = "Java    ";  //this is now without new keyword
String str2 = str1;
System.out.println(str1.trim()==str2.trim());

Note: I know how to use .equals method. But want to know == behavior especially in case of .trim() with above given two example.

AmitG
  • 10,365
  • 5
  • 31
  • 52

11 Answers11

4

Use the equals() or equalsIgnoreCase() methods to compare Strings. == compares object identity.

Maroun
  • 94,125
  • 30
  • 188
  • 241
Marco Forberg
  • 2,634
  • 5
  • 22
  • 33
4

use equal instead of ==

System.out.println(str1.equals(str2.trim()));
4

Strings are immutable in Java. Also string literals are interned i.e. java maintains a pool of string literals.

With the first 2 lines you are creating one object and 2 references str1 and str2 to the same object.

When trim() is applied on a string it forms a new string object and assigns the current reference to the new object. But since new is used while object creation str1.trim() and str2.trim(), both end up creating 2 separate objects.

Refer: http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#trim%28%29

trim() method creates a new object. Since you applied trim() individually on same object (though referred to by multiple references str1 and str2) hence 2 new objects are created. This is the reason why reference equality is not working.

prashant
  • 1,805
  • 12
  • 19
2

String are compared with equals, not ==

Eugene
  • 117,005
  • 15
  • 201
  • 306
1

If you wanna compare the content of a string, you need .equeals.

Your (modified) example

String str1 = new String("Java    ");
String str2 = str1;
System.out.println(str1.trim().equals(str2.trim()));  //is now true
JackPoint
  • 4,031
  • 1
  • 30
  • 42
1

For string comparison you should use str1.equals(str2) or str1.equalsIgnoreCase(str2).

For more points check this question.

Community
  • 1
  • 1
kaysush
  • 4,797
  • 3
  • 27
  • 47
1

Since you are using String str1=new String("Java "); You cannot use == operator

If you used String str1="Java ";, you could use ==

So here either change code to String str1="Java ";

or change

System.out.println(str1.trim().equals(str2.trim())); 
iCode
  • 8,892
  • 21
  • 57
  • 91
  • 2
    Not accurate. You **can alway** use `==` operator, but it will compare references. If you don't use `new` keyword, the references of the Strings that has *the same* string value will be the same (because of the pool). That's why the `==` is *"good"* in this case. – Maroun Apr 15 '13 at 08:06
  • Yes. `==` can use always. But if String is created using new operator, it will show _not equal_ since both string have different hash codes in memory. If it is without `new` hash code of both string wil be same and then if `==` is used, it will compare both hashcodes and will take as same – iCode Apr 15 '13 at 08:11
1

"=="

works on refrences function trim creates a new object that will have new refrence. that is the reason it will allways return false

ManMohan Vyas
  • 4,004
  • 4
  • 27
  • 40
0

You have two different Strings so two different references. To compare the content use equals().

David
  • 3,388
  • 2
  • 21
  • 25
0

== will compare the if reference of tow object are the same, as it check if both object are the same without checking the content of the object. As string is an object == will work only with the same Object of type string without checking the content of the string. to check the content of the String use

  equals() or equalsIgnoreCase
aymankoo
  • 653
  • 6
  • 12
0

You are using == sign for comparison. In java this will compare the actual objects, that is, whether the two variables point to the same physical object in memory, the same memory location.

If you want to compare the actual content of the strings, then you need to use method equals of String class (or equalsIgnoreCase) if you don't care about the case.

The same applies to any other types of objects. If you compare them using ==, you will be comparing the physical memory location of two variables. You would use equals method to compare the actual content of the objects (provided the class actually implements this method).

Aleks G
  • 56,435
  • 29
  • 168
  • 265