• 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

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
This is a very simple question. We know that Strings are immutable. Then how come this is possible?
String s1 = "first";
String s2 = "second";
s1=s2; // shdn't this be invalid
System.out.println(s1);
-sanjana
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
NO Sanjana,
Ur just altering the references, both s1 & s2 are references pointing to different string constants stored in STRING POOL. Later s1 = s2, alters the reference s1 earlier pointing to string"first" to the string pointed by s2.
hence u'll get second as the answer.
hope this clears ur doubt !!, when do u plan to take SCJP 1.4 exam ?
~ Shalini
[ January 06, 2004: Message edited by: Taurean Lord ]
 
sanjana narayanan
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
THANKS Taurean..I am planning to take up the exam next month.
-sanjana
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic