File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Anirudh Aithal
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Anirudh Aithal
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Jesper.

I guess that this same argument holds true for 'enum' objects as well?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everything in Java is pass-by-value, also variables of an enum type.
 
anandhi mohan
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

//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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic