• 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

strings

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

Hey i have a very silly doubt......
String s="abc";
String s1=new String("abc");
if (s1.equals(s))
System.out.println("true")
else
System.out.println("false")
also
String s ="abc";
s=s+"def";
System.out.println(s);
What will the value of s be now???
Help someone
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer to first question will be "true".The string class overrides the equals method it inherites from 'Object' class so equals in String provides a deep comparison which is a chracter by character comparison(unlike== which is shallow comparison).
The value of string s will will be "abcdef" in your 2nd question.The + operator is overloaded by the Java Language which allows string concatenation to be done in this manner.
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<PRE>String s ="abc";
s=s+"def";
System.out.println(s);
What will the value of s be now???
</PRE>
What happens here is that s now denotes a different String object ("abcdef") from the one it denoted before ("abc").
s is not a String but a String object reference.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
String s ="abc";
s=s+"def";
System.out.println(s);
What will the value of s be now???
instead of saying s=s+"def"; if u said
String s ="abc";
s+"def";
System.out.println(s);
then the value of s still remains "abc". This is because the new string formed is not assigned to any reference of type string and is hence "lost" to be garbage collected.
Rahul.

[This message has been edited by rahul_mkar (edited June 08, 2000).]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic