• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

string

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic