Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

MindQ question #11

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I don't understand why the answer is false. Could you please help me out? Thanks.

11. The statement ...
String s = "Hello" + "Java";
yields the same value for s as ...
String s = "Hello";
String s2= "Java";
s.concat( s2 );
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Strings are constant; their values cannot be changed after they are created.
So, after this statement s.concat( s2 );, s still have "Hello",
And we will get a new object "Hello Java".

Originally posted by Watson Li:


I don't understand why the answer is false. Could you please help me out? Thanks.

11. The statement ...
String s = "Hello" + "Java";
yields the same value for s as ...
String s = "Hello";
String s2= "Java";
s.concat( s2 );


 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Watson Li,
The answer to your question is based on what do you use to do comparison i.e equals() method or == operator and whether equals() method has been overriden in your class or no.
Consider two objects, x and y. If you use == to compare these objects, then what you are essentially doing is comparing the references (or the memory addresses) which point to this objects. This will work fine for you if you want to ensure both references point to the same object (only then the memory addresses will be same, right !!). But if want to check whether the two objects x and y are alike or are equal to each other content wise, then you need to use "equals()". But, remember this !!! "equals()" method is defined in Object class and as per the Java API for Object class "The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true)" which means Object class method equals() just uses == comparison . So, if you are looking to check if two objects are exactly alike then you need to override this method in your class, like the "equals()" method in String class which overrides Objects "equal()" method.
I have set of 15 questions on my homepages which deals only with object equivalence fundamentals. You may find it useful..
The url is http://www.tipsmart.com/javacert/selfreview/objequiv.htm
Sandeep Nachane
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Watson,
Always remember this, if you perform the operations like trim, concat, toUpper etc on string and dont assign the result to another string the change you have done is lost - ie orginal string is unaffected as strings are immutable. What really happens in such cases is a new string is created and returned.
You assign them to another string then results stays in the program otherwise this newly created string goes to POOL of STRINGS created by JVM. In future the program in question attempts to create a string similar to the one in pool of strings then no new string is created just the reference is asigned to it. In fact every time a new string is attempted to be created then POOL is searched first, new string is created only when the literal does not exist in the pool
Remember when you use "new" to create s string
like :
String s1 = new String ("new");
String s2 = new String ("new");
now both s1 and s2 are different strings with distinct references
Also now s1==s2 returns false while s1.equals(s2) returns true as content wise both string objects are equal.
Another thing to be noted is StringBuffer class is mutable version of String class.That means operations like concat, append etc change the source object.
Most important that StringBoffer does not override equals() method.
Hence Dont expect following to be true.
StringBuffer ss1= StringBuffer ("just");
StringBuffer ss2 = StringBuffer ("just);
if (ss1.equals(ss2)) --> returns false.
so is ss1 == ss2
Regards
Shrinivas

 
Watson Li
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you every body, I really appreciated. I think I miss understand the question. I thought it is asking to compare the two outcomes.
Anyway, thnx!!!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic