• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

primitive data type manipulation in wrapper classes

 
Arvind Varma
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why is it we do not have set() methods in the wrapper classes for primitive data types..(e.g. setIntValue() in Integer )
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The wrapper classes are all immutable. Their values can not be changed so no "set" methods are available.
 
Junilu Lacar
Bartender
Posts: 7602
53
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instances of the wrapper classes are immutable: you can't change their values once they have been created. That's why you will not find mutator methods on them.
 
Arvind Varma
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i probably was not clear in my Q...why did the wrapper classes had to be immutable? I didn't figure out what is it going to buy us...
 
Arvind Dua
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Performance is one of the reasons.
 
Junilu Lacar
Bartender
Posts: 7602
53
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arvind Varma:
why did the wrapper classes had to be immutable? I didn't figure out what is it going to buy us...

and right back atcha: What would it buy us if they were mutable?
Regardless of other and perhaps more practical concerns, to me the immutability of the primitive wrapper classes makes perfect OO sense. If you were to make an object represent the number 1, wouldn't it make sense to be able to count on it to always remain a representation of 1?
From "Effective Java" by Joshua Bloch:

Immutable classes are easier to design, implement, and use than mutable classes. They are less prone to error and are more secure.
...
Immutable objects are simple... are inherently thread-safe; they require no synchronization... can be shared freely... make great building blocks for other objects.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>>why did the wrapper classes had to be immutable? I didn't figure out what is it going to buy us

In this example, we store pairs of integers and Objects in a container. The container can hold only Objects, not primitive values. So we wrap the integer in an Integer. The key is unique. The key must not change. We want the key to be immutable.
�The second purpose of wrappers is to create objects to hold values of a primitive type for generically written classes that know only how to handle Object references.�
The Java Programming Language 11.1, Arnold, Gosling, Holmes
[ March 29, 2003: Message edited by: Marlene Miller ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Smalltalk, numbers are objects.
�Java containers can store any value that is ultimately derived from class Object. Unfortunately, ... the primitive values, such as integers and floating point numbers, are not objects in the technical sense. Thus, primitive values cannot be stored directly in a container...�
�the solution is to provide a series of auxiliary classes that do little more than to act as a box that can hold a single primitive value. In Java these are called wrapper classes...�
An Introduction to Object-Oriented Programming 19.2.2, Timothy Budd
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A bit more on immutable objects by Dough Lea in Concurrent Programming in Java:

If an object cannot change state then it can never encounter conflicts or inconsitencies whem multiple activities attemp to change its state in incompatible ways....existing objects are never changed, but instead new ones are continously created during the course of any computation.
Immutable objects can serve as instances of simple abstract data types representing values: Integer, awt.Color , String etc. These classes serve to encapsulate values, so the identity of their instances do not matter: for instance two instance s of Color representing the same value are typically intended to be treated as equivalent. this is the reason why ADT-style classes should normally override equals and hashCode to reflect equality of abstract value.

An immutable object can also serve to represent an object in a new state by copying the fields of the old object to those in the new object that do not chage, and then adjust the fields that did changed. If the need for objects in the new state is rare or cheap copying can outweight the drawbacks of proper synchronization.
Many objects can refer to the same immutable object because they are thread-safe. We are saving space because only one object is needed.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jose, I am looking for the quote in Lea's book. So far I have only found the first sentence. Would you mind providing the section(s) or page(s) for the quote? Thank you.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marlene, do you mean my last paragraph ? It is not a literal quote. But the spirit of it is on page 73.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jose, I am referring to the �quote:�. I found your quote in Lea�s book:
2.1 first two sentences
2.1.1.1 first paragraph, first and third sentences
2.1.1.1 second paragraph, first, second and third sentences
Are you quoting from the second edition? Your quotes are not exactly the same as the book?
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well yes I am a lucky owner of a volume of the 2nd ed. of this excellent book.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic