• 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Immutability?  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear fellow jolly ranchers
I'd be very grateful if someone can explain to me, or point me in the direction of, the meaning of IMMUTABILITY?
All I know, is that the String class IS immutable...but that's about it!
Cheers
Mez
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it means that the object cannot change. When you create a string, you are not able to change one char at an especific position. String class does not provide any method to do so!!!
String s="string" ; //this creates a new allocated string
s="strings";// this also creates a new allocated string
what happened to the old one?? now, it is subject to the garbage collector. Any array is immutable, since you cannot change it's size.

Hope this helps. Correct me if wrong.
 
Mez Raja
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks leandro
are there any other immutable classes?
Mez
 
Leandro Oliveira
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know. Arrays and strings are the only immutable objects i know, hope somebody in the forum knows!!!
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the wrapper classes, like Integer, Boolean can only be set once, when you instantiate it -- there's only getXXX() methods, no setXXX() methods.
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The wrapper classes are immutable: Byte, Short, Integer, Long, Float, Double, Character, Boolean, Void. Once you have constructed a wrapper object, you cannot change the primitive value that it contains.
Please see this IBM developer works article, which defines an immutable object as one whose externally visible state cannot change after it is instantiated. Under that definition, I would not consider an array to be an immutable object.
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with John. The values contained within an array can be changed so an array is not considered to be immutable.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by leandro oliveira:

String s="string" ; //this creates a new allocated string
s="strings";// this also creates a new allocated string


The above strings are compile-time constants and they both exist in a pool of string constants when the program is loaded. The above two lines of code assign to reference variable s the reference value of the String constants that already exist in the constant pool.
The following line of code will create at run-time a new instance of a String object.
String s1 = new String("string");
The following line will create another instance of a String that contains the same value.
String s2 = new String("string");
If you compare the above strings, s1 and s2, using the equality operator, ==, then the result will be false because the references refer to unique instances. If you want both to refer to the same instance then you can invoke the intern() method on s1 and s2.
s1 = s1.intern();
s2 = s2.intern();
Both s1 and s2 now refer to the same String instance in the pool of constant strings.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!