Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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).]
 
All of the following truths are shameless lies. But what about this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic