• 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 Immutable ?

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can anyone explain in simple terms how strings are immutable ?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Immutable means that once a String object is created, its content cannot be modified.

Have a look at the methods in class String (look at the API documentation). You'll see that there are no methods that change the content of the String. Note that all methods that do something with the String, such as toLowerCase() and toUpperCase(), return a new String object - they do not modify the original String object.
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Immutable means the created stirng object can never be changed.Just look at below example,

String s = "abc";
String s = s2;
s2 = concat.("def");

In above case s is referencing still "abc"
Only s2 now referenced to "abcdef"

check the below code,

String s = "hello";
s.concat("world");
System.out.println(s);

Output is "hello"
The object created on second line is "hello world".It doesnt have any reference so its lost though its existing.
 
Sid Robin
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
s2 = concat.("def");


I think the statement must be s.concat("def") ?


Thank you a lot..............!

Java Ranch Rocks
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know if this qualifies for a simple explanation, but I found it helpful to actually see how the bytecode comes out.

So do something simple like:


Than compile it and run javap -c to decompile it to bytecode. If you keep the program REALLY simple, you'll be able to see what's really going on with Strings behind the scenes. You don't have to understand the opcode to a tee (the operation instructions in the bytecode) to be able to see when it's added a string literal to the string pool (Constant Literal Pool) because it will say ldc for load constant, or when it creates a whole new String, etc. I'm sure some may discourage this, but I found it quite helpful. Give it a try, it couldn't hurt!
 
Whatever. Here's a tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic