File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Why are wrapper objects (to primitive data types) immutable? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why are wrapper objects (to primitive data types) immutable?" Watch "Why are wrapper objects (to primitive data types) immutable?" New topic
Author

Why are wrapper objects (to primitive data types) immutable?

Anirudh Aithal
Greenhorn

Joined: Jun 09, 2009
Posts: 7
Hi,

I am a Java newbie. Can anyone tell me why I can not change the value of a wrapper object? To be more precise, I expected the following piece of code to print the output as 10 150. Why isn't this the case?



Regards,
Anirudh.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13870
    
  10

Welcome to JavaRanch.

The reason why your code doesn't work has nothing to do with immutability. If you would have used int instead of Integer, you would have gotten the same result. Your code does not produce the output you expect because Java passes arguments to methods by value - which means, if you pass a variable to a method, only the value of the variable will be passed, not the variable itself.

The Integer aInt is a new variable, which receives the value you pass to the method changeIntVal when you call it - if you change the value of aInt, the value of the original variable (newInt in the main method) is not changed.

See the story Pass-by-Value Please for more information on Java's pass-by-value mechanism.

That being said, the wrapper classes are indeed immutable, and that's a good thing. There are several reasons why it's good. Have a look at the following article, for example:

Immutable types: understand their benefits and use them

(Note, the article uses C# instead of Java, but the idea is the same).

Some more info about immutability: http://en.wikipedia.org/wiki/Immutable_object


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Anirudh Aithal
Greenhorn

Joined: Jun 09, 2009
Posts: 7
Thanks a lot Jesper.

I guess that this same argument holds true for 'enum' objects as well?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13870
    
  10

Everything in Java is pass-by-value, also variables of an enum type.
anandhi mohan
Greenhorn

Joined: Mar 24, 2009
Posts: 9

//Try this code to get output like 10 150

class Wrappers{
static Integer newInt = null;
public static void changeIntVal(Integer aInt){
newInt = aInt * 15;
}

public static void main(String[] args){
newInt = new Intger(10);
System.out.println(newInt);
Wrappers.changeIntVal(newInt);
System.out.println(newInt);
}
}

// //
Wrappers.changeIntVal(newInt);
You are passing only value of newInt to that changeIntVal method ,not the variable.
and you cannot access that variable from outside because it is local to the main method.
If you want to access the variable from anywhere in the class then you can use static
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why are wrapper objects (to primitive data types) immutable?
 
Similar Threads
"==" on Wrapper Class
AutoBoxing
Why doesn't autoboxing work here ?
JDK 1.5 AutoBoxing Confusion in Code
input from console