Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Java Wrapper Classes

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    114
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Java Wrapper Classes

    I understand the reason behind String class being immutable. Would like to know for what reasons the other wrapper classes like Integer,FLOAT,LONG are made immutable?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Java Wrapper Classes

    Can you think of any reasons? I wouldn't spend too much time worrying about why certain decisions were made about the language. Why is it called a JFrame instead of a JForm? Why is it int and not integer? It's usually best just to accept the fact of the situation and move on.

    I can think of at least one reason for making them immutable though. Say you have a class that contains an Integer that you want other classes to be able to access, but not change. You might do something like this:


    public class IntegerHolder{
     
       private Integer i = 15;
     
       public Integer getI(){
          return i;
       }
    }

    That's all well and good, right? The Integer is private so other classes can't access it directly, and there's only a getter method, so other classes can't change it. But if Integer was not immutable, another class could do this:

    IntegerHolder holder = new IntegerHolder();
    holder.getI().setInt(10000);
    Last edited by KevinWorkman; June 13th, 2012 at 08:07 AM.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Java Wrapper Classes

    Making a class immutable has advantages for both Garbage Collection and security. Security wise, one cannot alter the value of the object and leave it in an unexpected state. Examples and how this relates to threading is discussed in the following:
    See Immutable Objects (The Java™ Tutorials > Essential Classes > Concurrency)
    For a discussion on the advantages for garbage collection, see
    Java theory and practice: Garbage collection and performance
    If a class has no reason to have its behavior overridden or inherited (as would be the case for Integer, Long, etc...), there's a good chance I will make the class immutable for the above reasons.
    Last edited by copeg; June 13th, 2012 at 12:26 PM. Reason: spelling

  4. The Following User Says Thank You to copeg For This Useful Post:

    KevinWorkman (June 13th, 2012)

  5. #4
    Junior Member
    Join Date
    Jan 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Wrapper Classes

    well Immutability offer several advantages :

    1) Immutable object like String and wrapper can be used as key in HashMap (those are most used keys anyway)
    2) Immutability enable them to be reused in application. valueOf() often return same object if its immutable
    3) Immutable are inherently thread-safe

  6. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Java Wrapper Classes

    Quote Originally Posted by javabuddy View Post
    well Immutability offer several advantages :

    1) Immutable object like String and wrapper can be used as key in HashMap (those are most used keys anyway)
    2) Immutability enable them to be reused in application. valueOf() often return same object if its immutable
    3) Immutable are inherently thread-safe
    Be careful in getting Immutability confused with Wrapper classes and caching (points 1 and 2). Immutability does not have much to do with being able to wrap a primitive and place it into a Map (it does from the context of thread safety and the hashCode() method contract - and FWIW it is quite relative to say they are the most used keys), and Immutability has nothing to do with the caching involved with the valueOf method of those classes (and just to note, is incorrect to say it always returns the same object - only a few select values are actually cached, in which case for only a few select values will you get the same object)
    Last edited by copeg; June 15th, 2012 at 09:44 AM.

Similar Threads

  1. Replies: 1
    Last Post: April 3rd, 2012, 06:32 AM
  2. JAVA .NET WRAPPER
    By kafka82 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 18th, 2011, 08:46 AM
  3. Need a jar wrapper for Mac
    By namhm in forum Java SE APIs
    Replies: 2
    Last Post: October 10th, 2011, 11:31 AM
  4. the definition of a wrapper class
    By nickypass in forum Java Theory & Questions
    Replies: 1
    Last Post: October 17th, 2010, 01:54 AM
  5. Java: interfaces and abstract classes
    By pinansonoyon in forum Object Oriented Programming
    Replies: 1
    Last Post: May 6th, 2010, 10:17 AM