aspose file tools*
The moose likes Beginning Java and the fly likes Immutable classes in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Immutable classes in java" Watch "Immutable classes in java" New topic
Author

Immutable classes in java

Shahid Pathan
Greenhorn

Joined: Dec 28, 2010
Posts: 23
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

Joined: Aug 16, 2005
Posts: 14107
    
  16

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 384

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


http://plainoldjavaobject.blogspot.in
Ravi Majety
Ranch Hand

Joined: Feb 26, 2009
Posts: 59

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

Joined: Aug 16, 2005
Posts: 14107
    
  16

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

Joined: Feb 26, 2009
Posts: 59

@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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Immutable classes in java