Win a copy of High Performance Python for Data Analytics this week in the Python forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Immutability on String object question

 
Ranch Foreman
Posts: 15
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The book provided the following example:

String s = "1";
s += "2";
s += 3;
System.out.println(s);

Which prints 123. It's trying to show the reader when the + went into addition mode or concatenation mode. Then it jumps into Immutability.

"Once a String object is created, it is not allowed to change."

It then provided an example of Immutability:

String s1 = "1";
String s2 = s1.concat("2");
s2.concat("3");
System.out.println(s2);

It prints "12".

Which really confused me since the two conflicts each other. Is it trying to say, a string object can be modified using the + operand but it can not be modified through the concat method?

Thanks in advance!
 
Marshal
Posts: 71752
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Cosmid Constantine wrote:. . . . Is it trying to say, a string object can be modified using the + operand but it can not be modified through the concat method? . . .

No. If you count the = characters (assignment operators), you find there is no = in the penultimate line. That is what causes the confusion.
 
Cosmid Constantine
Ranch Foreman
Posts: 15
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right, it's trying to modify the object that s2 is referencing in the 2nd example. My confusion is why the string object referenced by s in the first example can be modified but the string object referenced by s2 in the 2nd example can not be modified.
 
Campbell Ritchie
Marshal
Posts: 71752
312
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Cosmid Constantine wrote:Right, it's trying to modify the object that s2 is referencing . . . why the string object referenced by s in the first example can be modified . . . .

No.

No String objects were modified in the making of this motion picture.

String objects cannot be modified by any normal code. You are not modifying any objects. You are creating new String objects, both with + and concat() but those new objects disappear into some sort of cyber‑limbo never to be seen again if they are not used somehow. In the code you showed, there are = operators to use those objects by replacing what s2 points to. Remember the += operator includes =.is equivalent toJLS (=Java┬« Language Specification) link.

Please use the reply button rather than the quote button.
 
Cosmid Constantine
Ranch Foreman
Posts: 15
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I finally got it! Because there's a = in all of these, so it's not modifying any of these objects but instead it keeps referencing a newly created object. And there is only one line without the = and that's where it's trying to modify a string object and causes an error. Thank you for your patience to keep explaining it to me!

I think taking a break also helped, haha
 
Campbell Ritchie
Marshal
Posts: 71752
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For having a question mentioned in the December 2020 CodeRanch Journal, congratulations: this question earns you a cow
 
reply
    Bookmark Topic Watch Topic
  • New Topic