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

Immutable classes in java

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
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
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.
 
Ranch Hand
Posts: 384
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 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
 
Ranch Hand
Posts: 59
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
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

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 Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic