• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

string immutable?

 
Ranch Hand
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read a article in that String class are immutable.
String a=�a�;
String h=�h�;

String class is immutable, that means that one an instance of String class is created, the string it contais cannot be changed, only reference will change to point to a
different string.

If two references point to same instance then If I change the values of in one, does not affect other reference?
 
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I read a article in that String class are immutable.
.....
If two references point to same instance then If I change the values of in one, does not affect other reference?



They are immutable, how do you change the value in one instance?
 
jacob deiter
Ranch Hand
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
then what is use of

String a=�a�;
String h=�h�;

than

String a=new String("a");
String h=new String("h");
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We don't use the second one.
 
jacob deiter
Ranch Hand
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why could you please in detail.I read a book ,this is not given clearly.
Can you assist in this >?
 
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
String objects are immutable. What that means is that you cannot change the contents of a String object. You can have to variables referencing the same String object, but since the String object itself cannot be changed, you can't get strange effects.

Because String objects are immutable, the Java compiler can safely implement an optimization trick. If you write a program in which you use the same literal string more than once, for example like this:

Then the Java compiler is smart enough to create only one String object with the content "Hello", and make the variables 'one' and 'two' refer to that single String object. This is good, because it saves memory.

If you do this:

Then you are explicitly creating two String objects with the same content "Hello". This is less memory-efficient.

There's no use ever to using new String("...") in a Java program, so you should never write code that does that.
[ November 21, 2008: Message edited by: Jesper Young ]
 
Live a little! The night is young! And we have umbrellas in our drinks! This umbrella has a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic