• 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
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

a very big, simple but very BASIC question about String!! :)

 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok K&B book says String objects are immutable, but references to Strings are not!

so with what exact code are changing the value of a String object? (lead to compile or runtime whatever error?)
can you help to show me an example?

and references to Strings are mutable.
can you help to show me an example, too?

thank you!
 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
case:1
String s="java".
s.concat("rules")
answer is "java". Here "java rules" is created but lost immediately because nothing refers to it.

case:2
String s="java".
s=s.concat("rules").
answer is"javarules". Here"java" lost, because now s is refers to "javarules".
That's why Strings are immutable.
I think i will clear your doubt.
 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Study dolly's code and you can see why Strings are immutable. As to force error to come out by changing existing String object without compiler automatically creating new one.... hmmm no idea. Perhaps java designer designed it this way where it's hard for you to create errors. So people don't keep crashing programs.
 
lowercase baba
Posts: 13073
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when you have a line like

String s = "java";

there are two things created. there is the actual String object, that lives on heap. then, there is the reference "s" that points to that object in memory.

My favorite analogy is a house, and a piece of paper. the house is the object. the paper is your reference.

you build the house, and write down the address on the slip of paper.

You can't change - physically alter - the house, but you can erase one address and put another one on the paper. The house is immutable, the paper is not.

so with what exact code are changing the value of a String object?

I don't understand your question here. you CANNOT change the value of a string once it is created. it's not a question of the compiler saying "Nope!!!". There is simply no method that exists that will change the string.

all the things that you might THINK change the string don't - they actually create NEW strings, leaving the old ones untouched.
 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


so with what exact code are changing the value of a String object? (lead to compile or runtime whatever error?)



As you see, the original String object "Hello" is not changed by the concat() method. Just a new String "Hello World" is created and return to you. String object can't be changed by any method, that's what immutable mean.
 
Space pants. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic