• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Immutable classes in java

 
Shahid Pathan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which are the immutable classes in java? (eg String)
What is the use of immutable objects?
Why we use immutable classes?
How we can create immutable classes?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There thousands of classes in the standard Java library, so it is a bit hard to give you a complete list of all classes from the standard library that are immutable. But indeed, String is immutable, as well as the wrapper classes Byte, Short, Integer, Long, Float, Double, Boolean and Character.

There's a lot of detail about your question in this Wikipedia article: Immutable object.

Also, this question has been asked before on the forums here, so if you do a search for "immutable class", you will find older posts with useful information.
 
Lalit Mehra
Ranch Hand
Posts: 384
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
immutable means that you cannot change its value ...

we use immutable objects to make sure that we do not accidentally change the actual value.
Strings and Wrappers are indeed common examples of the same
 
Ravi Majety
Ranch Hand
Posts: 59
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Immutable objects should be used very restrictively. If you got the String variable which changes many times in a code, then i prefer to make use of String buffer instead of String variable.

Because if we go on changing the value of a string variable, then for each time the value changes, jvm creates a new memory with new value and maps the new memory with string variable, so the previous memory is unused. This unused space will get freed only when JVM Calls GarbageCollector. Until then this space would be useless.

In larger applications, where the values changes often then i prefer stringbuffer, because use of string in such scenarios would lead to memory problem.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Smarty Ravi wrote:Immutable objects should be used very restrictively. If you got the String variable which changes many times in a code, then i prefer to make use of String buffer instead of String variable.

I disagree. Immutable objects have many advantages - they make your code simpler and especially if your program is multi-threaded immutable objects are good, because they are thread-safe.

Smarty Ravi wrote:Because if we go on changing the value of a string variable, then for each time the value changes, jvm creates a new memory with new value and maps the new memory with string variable, so the previous memory is unused. This unused space will get freed only when JVM Calls GarbageCollector. Until then this space would be useless.

Java's garbage collector is very smart and efficient, especially with short-lived objects. The latest Java 6 versions also have advanced optimizations (escape analysis - I won't explain in detail because it's certainly not a beginners' topic) that make creating and garbage collection temporary objects almost free. Don't do these kind of optimizations manually because you think creating and destroying objects is expensive, because usually it is not.

Smarty Ravi wrote:In larger applications, where the values changes often then i prefer stringbuffer, because use of string in such scenarios would lead to memory problem.

There are certain circumstances in which using StringBuilder (note, you should be using StringBuilder instead of StringBuffer) is preferable above using String. For example, when you have to concatenate things in a loop, it's more efficient to use a StringBuilder. But do not use StringBuilder or -Buffer as a general replacement for String, because you think it is somehow more memory-efficient.

Creating and using immutable objects is good practice.
 
Ravi Majety
Ranch Hand
Posts: 59
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Jesper de Jong

I disagree. Immutable objects have many advantages - they make your code simpler and especially if your program is multi-threaded immutable objects are good, because they are thread-safe.

I didnt mentioned that String didnt got advantages. I just given a part where string is not used ...thats it . I know String variable got many advantages than Stringbuffer or Stringbuilder.

I just defined when Stringbuffer would be useful than String.

Java's garbage collector is very smart and efficient, especially with short-lived objects. The latest Java 6 versions also have advanced optimizations (escape analysis - I won't explain in detail because it's certainly not a beginners' topic) that make creating and garbage collection temporary objects almost free. Don't do these kind of optimizations manually because you think creating and destroying objects is expensive, because usually it is not.


By the way, in real environments we cant determine the how situations worse . So we cant always depend on Garbagecollection in certain scenarios to free up the memory. So we need to optimize the code and decide , which one should be used among String or StringBuffer or StringBuilder based on logic.


There are certain circumstances in which using StringBuilder (note, you should be using StringBuilder instead of StringBuffer) is preferable above using String. For example, when you have to concatenate things in a loop, it's more efficient to use a StringBuilder. But do not use StringBuilder or -Buffer as a general replacement for String, because you think it is somehow more memory-efficient.


I agree with this. I dont prefer to use the Stringbuffer in replacement of String everytime. I just given a situation when to use Stringbuffer in place of String. Thats it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic