• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

String immutability security breach!

 
Ranch Hand
Posts: 86
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've encountered something strange. If strings are truly immutable, then what the following code does makes no sense:

I can understand that a string's immutability makes the toUpperCase method possible with output but not for the string itself.

I can accept but not understand how interfering with white spaces of an original string does not qualify as changing the string, so the immutability is in question on that for now.

But line 5 should not be able to do what it does. We're not dealing with a StringBuilder object here, so how is it possible to just waltz in and add to an immutable string just by concatenating to it?
 
Marshal
Posts: 5466
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On line 5 you are assigning a new string to 's', not mutating it.
 
Sam Peterson
Ranch Hand
Posts: 86
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Cooke wrote:On line 5 you are assigning a new string to 's', not mutating it.


Are those rules similar to as illustrated in my other post?
 
Saloon Keeper
Posts: 14490
325
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. The += operator doesn't mutate the original object. It returns a new object which is then assigned to the variable.

Initially, a and b refer to the same object. Then we create a new object and assign it to b. Line 6 will still print "Hello" because the original object was never mutated.
 
Marshal
Posts: 76825
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sam Peterson wrote:. . .. . .

You are correct about lines 2 and 3. In the case of line 2, you are passing the value returned from the toUpperCase() method as an argument for println(). You are using the “PURR” immediately but because you don't keep a reference to that object, you are consuming that object's reference and you can only use it once. In line 3 you are creating a different “PURR” object and making no use of it.
 
They weren't very bright, but they were very, very big. Ad contrast:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic