• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String literals

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please read the following 2 questions.
I agree with the Question1's answer. But i dont understand why is the answer for Question2 "Not Equal".

Question 1.
if("String".toString() == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");
Answer
the code will compile an print "Equal".

Question 2.

if(" String ".trim() == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");

Answer
the code will compile an print "Not Equal".
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Sonali -
In Java, the == tests for whether two object references point to the same object, not whether their values are equal. For equality testing of objects, you must use their equals() method.
Think of it this way: the == operator tests if a value is the same. What does a reference have? The address of the object it's referencing. Therefore,

Here, isSame will be false, because the references, a and b, have different addresses because they point to different objects. Since an address is the value for a and b, their values are different, and the comparison returns false.
I hope that helps!
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To quickly add to that;
remember that String objects are immutable therefore the trim() method does
not change the original object - " String ".
The implication of this is that " String " and "String" do not refer to the
same object hence result is fault.
//N.B. difference? = white spaces.
i hope moi explanation is good enough!
Cya
 
Jeff Bosch
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


remember that String objects are immutable therefore the trim() method does
not change the original object - " String ".


String.trim() returns a copy of the original string with the leading and trailing spaces removed. Therefore, the test is not

but

Immutability has nothing to do with the result.
 
O Joseph
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I beg to disagree with u. "String ".trim() is not assigned to any new
reference. therefore the original object remains unchanged and so :
/*if(" String ".trim() == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");*/
prints "Not Equal".
" String " != "String" according to "==".
 
Jeff Bosch
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't have to assign it to a reference if you're using it in the same place you create it. By invoking String.trim() in the test, you are testing the object returned by the operation -- a copy of the original String.
Try this simple test if you don't believe me:

The vertical bars act as an indicator to show whether or not the spaces are indeed trimmed.
 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I am still confused...
If " String ".trim() returns -->"String"
then, the statement if ("String"=="String")
should return true..isnt it?
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String API for trim() method says that trim()


...let k be the index of the first character in the string whose code is greater than '\u0020' [white space], and let m be the index of the last character in the string whose code is greater than '\u0020'. A new String object is created, representing the substring of this string that begins with the character at index k and ends with the character at index m-that is, the result of this.substring(k, m+1).
Returns:
A copy of this string with leading and trailing white space removed, or this string if it has no leading or trailing white space


"String"=="String" references comparison should always return true because they are compile time constants pointing to the same object on the heap.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cathy --
Two String objects can contain the same characters, in the same order, but still be distinct objects. Just like two identical cans of soup. They're identical in every way, but they're physically separate, so they're !=. The == operator means "are the same object," not "are equivalent." The "equals" method means "are equivalent", so " String ".trim().equals("String") returns true, because, although the two String objects being compared are located in two different places in the computer's memory, they both contain the word "String".
OK?
 
Jeff Bosch
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


If " String ".trim() returns -->"String"
then, the statement if ("String"=="String")
should return true..isnt it?


No, because even though their values are the same, they have different addresses because they're different objects. For objects, == tests whether the objects have the same address; equals() tests whether the values of the objects are meaningfully equivalent.
For primitives, because the value of the primitive is a value and not an address, == tests the value. There is no equals() for primitives.
Here's a hypothetical, general example:

a and b are String object references, which means they can hold the value for an address to an object of type String.
The value of a is 1234, which is the address of the first String object.
The value of b is 1244, which is the address of the next String object.
Now, if we compare whether a == b, we are comparing whether 1234 == 1244. They're not equal, so the test returns the result "false". We could then compare a.equals(b), which tests whether the values are equal. In this case, we would see "true", because both objects have the value "abc".
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if("String"=="String") compares references of two String literal constants. I believe they point to the same object on the heap and therefore the result is true.
 
Ranch Hand
Posts: 218
VI Editor Ruby Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, we all know that Java maintain a string pool for the string literal...
but I do not think String.trim() did say that it will return result coming
of the string pool.
So assuming " String ".trim() == "String" is a little too much.
In most cases when we used direct string literal all over our code, it might
be easier for the compiler to do the optimization to refer to the same string.
I would guess in cases where you have method like String.trim() which happen
at runtime, the API coder probably figure its not worth effort to check the
string pool and return existing strign...so it just create a brand new String object.
You'll probably get better result with " String ".trim().intern() == "String"
Just my 2 cents..
 
Jeff Bosch
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Wirianto.
 
Cathy Song
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Thanks for your inputs. So, let me try to summarize the post.
" String ".trim() returns a new String instance (not a literal)
That is why the following statement returns false.
if (" String ".trim() == "String)
Thanks again.
 
O Joseph
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here's moi own conclusion: the instance methods of the string e.g. trim()
look like they would transform the object they are invoked upon but
actually alter the object and instead return new string objects.implication?
yes, the implication is that comparing "==" gives false.
finne.
 
Think of how stupid the average person is. And how half of them are stupider than that. But who reads this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic