• 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 as Object

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which of the following return true?
1. "john" == "john"
2. "john".equals("john")
3. "john" = "john"
4. "john".equals(new Button("john"))
Select all correct answers.
---
I think only 1 should be the answer but I have seen 1 and 2 both are correct answers. In option 2, according to my understanding, this should not true as "john" making a static text that should not be able to call equals() method.
What I can think of is JVM is converting that static text as a string object. Is this correct?
If you can explain it more that wud be appricialble.
Thanks.


 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When using the == operator it compares references to objects. String objects are created at compile time and put into a "constant pool". So, when a String object is created it is checked against the constant pool and if there is the same String object there the JVM just creates a reference to the same String object. Therefore, if you use the == operator on two String objects that are equal it returns true if they have the same reference. The equals() method actually takes two String objects and checks every character to see if they are the same.

Output:
joe == joe2: true
joe2.equals( joe3 ): true
joe2 == joe3: false //not the same object reference

I hope that helps,
Joe
[This message has been edited by Joseph Russell (edited February 13, 2001).]
 
rehan hamid
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Joe, You are creating a String var into string pool when creating string variable jeo2. I wud like to hear an answer that explains why option 2 is also correct with ref. to my question.
There its not a string var rather a "static"(not java static) text.
Rehan.
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rehan,
Yes you are somewhat correct. The compiler replaces literal strings with a substitute string object. It also replaces all the same literal strings with the same object.
1. "john" == "john"
compiler output (psuedo):
string1 = "john";
string1 == string1;
2. "john".equals("john")
compiler output (psuedo):
string1 = "john";
string1.equals(string1);
3. "john" = "john"
compiler output (psudo)
string1 = "john";
string1 = string1;
4. Don't care here ...

Regards,
Manfred.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The .equals() method of object class is overriden in the string class.for String objects they are equal if they have same character sequece.also if a class does not overrides the equals() method then it is same as ==.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please verify that I have the correct logic for Russell's code
String joe3 = new String( "joe" );
The new operator makes a variable called joe3 which contains a reference to the sting "joe". This contrasts to the assignment statements of:
String joe = "joe";
String joe2 = "j" + "oe";
Since string objects are immutable, therefor they are constant. Here the compiler sees that joe2 is actually the same string as joe and therefor joe2 gets the same reference to the original joe.
At compile time everything is known about the joe strings. It would seem that all three "joe" should have the same reference(using the logic of the constant pool). Is it the new operator that is causing a unique reference to joe3?
 
Rototillers convert rich soil into dirt. Please note that this tiny ad is not a rototiller:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic