• Post Reply Bookmark Topic Watch Topic
  • New Topic

String literal comparison inside a System statement with a String  RSS feed

 
johnny smithy
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. Help much appreciated. This unexpected behaviour is not making sense. Line 11 is expected. Line 9 for example is expected to be 'a==c true', but comes out false?

1. String a = "fluffy";
2. String b = new String("fluffy");
3. String c = "fluffy";
4. String d = new String("duffy");
5. System.out.println("Here:"+"\n");
6. System.out.println("a==b");
7. System.out.println("a==b" + a==b);
8. System.out.println("a==c");
9. System.out.println("a==c" + a==c);
10. a=c;
11. System.out.println(a==c);

-------------------------------------------------------------------------------------------------
output:Here:

a==b
false
a==c
false
true

 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java String is an object and cannot be compared like an Atomic and get the right answer, you need to use the String Object comparison operator equals. When you use a "==" for comparing Strings, you are literally asking if hey are the same String--same address space and etc. Use the String comparison equals and you'll be fine.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
johnny smithy wrote:Line 11 is expected. Line 9 for example is expected to be 'a==c true', but comes out false?


Hint: Which operator has the higher precedence? is it the concat (+) or the equal to operator (==)?

Henry
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here ya go, API quote:

public boolean equals(Object anObject)

Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
Overrides:equals in class ObjectParameters:anObject - The object to compare this String againstReturns:true if the given object represents a String equivalent to this string, false otherwiseSee Also:compareTo(String), equalsIgnoreCase(String)
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:Here ya go, API quote:

public boolean equals(Object anObject)


Although, admittedly, in the majority of cases, the error is caused by the used of the "==" operator versus the equals() method -- this isn't one of those cases. It is using the "==" on two string references which is assigned via a string literal, and with the string pool, these two strings should refer to the same string object.

As mentioned, in my hint, this situation is related to the OP expecting a different precedence between the "+" and "==" operators.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!