Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

string

 
anushree ari
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,
if(" String ".trim() == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");
the ans is not equal.how?
thx
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The equality operator, ==, compares reference values. The String literal, "String", refers to an instance of a String in the String constant pool. The String created by the String.trim method is not a String constant. When the references of the two Strings are compared they are found to be not equal.
If the two strings were compared using the String.equal method then the two Strings would be found to be equal because they contain the same characters.
If you were to invoke the String.intern method on the String returned by the String.trim method then the result of the intern method would be a reference to the String in the String constant pool. If the equality operator were applied to the String literal and the result of the intern method then the result would be true.
 
anushree ari
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok dan, but
if( "STRING".toUpperCase() == "STRING")
System.out.println("Equal");
else
System.out.println("Not Equal");
this results equal,
how?
 
ramki srini
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
toUpperCase() method will return a new String only if the value gets changed or else it will point to the same String in the pool.
In your case "STRING".toUpperCase will have no impact(as everything is uppercase already) on the "STRING" literal and so it will point to the same STRING in the pool and thats why the result EQUAL.
 
Keen Chen
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all:
Let me do it.
if( "STRING".toUpperCase() == "STRING")
yes, it is true.
bucause
1. java put all String literals to a String pool. "STRING"s defined in two place are the same one in pool.
2.in LHS, toUpperCase() return the same reference since String "STRING" all uppercase, it neednt' change it and return a new one.
 
anushree ari
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey keen how about this one
if("String".substring(0) == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");
this results equal, explain me
thx
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's basically the same question as the previous one. The String methods first check to see if a modification of the existing String is necessary. If no modification is necessary then the method just returns the reference "this". For example, the substring method returns "this" when (beginIndex == 0) && (endIndex == count).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic