| Author |
What is the difference between Integer and int
|
chaitanya karthikk
Ranch Hand
Joined: Sep 15, 2009
Posts: 779
|
|
Hi everybody, I am Chaitaya, I have read a topic regarding boxing and autoboxing in java. Then I got a doubt regardin the difference between Integer and java.
I searched the web, they said int is mutable and Integer is immutable, in the example which I was referring regarding autoboxing they changed the values of Integer object. So what should I believe.
You can find the explanations here boxing and autoboxing, difference between int and Integer
Thank you all in advance. Have a good day.
|
Love all, trust a few, do wrong to none.
|
 |
Wouter Oet
Saloon Keeper
Joined: Oct 25, 2008
Posts: 2700
|
|
An Integer is immutable. Any operation that tries to change it value returns a new Integer object.
Try this:
It doesn't compile because value++ returns a new Integer object and tries to assign it to the value reference.
But because it's final it can't be changed.
|
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
|
one has identity, another doesnt have that.
|
 |
K Abhijit
Ranch Hand
Joined: Mar 03, 2008
Posts: 88
|
|
String is Imutable class:
People often get confused between FINAL and Immutable:: Just to clarify::
Final is : Constant Pointer:
now finalStrReference is Constant Reference (Pointer)...if you say
This would flag error , as once Memory address is assigned to Final Reference, it can not be changed...
You can not change the reference , but you CAN change the Object that reference is referring to...ie.
IS ALLOWED
Immutable is Pointer to Constant :
now strReference is normal Reference (Pointer)
if you say
This is allowed, but THIS ASSIGNMENT WOULD CREATE ANOTHER STRING OBJECT ON Heap and that memory address would be Assigned to reference strReference But the original Object "Java is nothing but SUN Technology" WOULD NEVER CHANGE (it might get Garbage Collected if there is no handle referring to it; but that's different story)
|
“The difference between 'involvement' and 'commitment' is like an eggs-and-ham breakfast: the chicken was 'involved' - the pig was 'committed'.”
|
 |
K Abhijit
Ranch Hand
Joined: Mar 03, 2008
Posts: 88
|
|
Coming to your question:
Considering the concept of Final and Imuatble now you would find the difference.
int- mutable : - you can change the values of the int variable several times...
Interger Object: Imutable: The Object of Interger once Contructed CAN NEVER CHANGE IT'S STATE (like String )
Anyway, you should not compare Objects with Primitive datatypes...
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32611
|
|
chaitanya karthikk wrote: . . . int is mutable and Integer is immutable, . . .
That sounds misleading at best; where did you find it? An int is a primitive; it has no methods allowing you to change its state, just as no other primitive has any methods. I think "mutable" has no meaning for primitives. As you have been correctly told, you shouldn't compare objects with primitives.
But as you know you can say . . . unless i is marked as final. When you write i = i + j;, the JVM carries out an arithmetical calculation (i + j) and then assigns the result, by putting it into the memory location which you have called i. And you have had a nice demonstration of the difference between immutable and final from K Abhijit.
|
 |
chaitanya karthikk
Ranch Hand
Joined: Sep 15, 2009
Posts: 779
|
|
Wouter Oet wrote:An Integer is immutable. Any operation that tries to change it value returns a new Integer object.
Try this:
It doesn't compile because value++ returns a new Integer object and tries to assign it to the value reference.
But because it's final it can't be changed.
Hi Wouter, I did not understood the point you quoted. You have placed a final before the value object, so definitely one cannot change it, I write the following code.
This is working fine, and however if I keep final before int i can make the integer variable also as immutable.
Please anyone clarify my doubt. Thank you all in advance.
|
 |
Wouter Oet
Saloon Keeper
Joined: Oct 25, 2008
Posts: 2700
|
|
What I was trying to demonstrate is that you can't change the value of an Integer object. This is what value++ means:
value gets unboxed to an int
that int is incremented
that int is then boxed and assigned to value.
But because value is final the reference can't be changed thus it doesn't compile.
In your example the Integer after the increment is not the same object as before the increment.
|
 |
chaitanya karthikk
Ranch Hand
Joined: Sep 15, 2009
Posts: 779
|
|
Thank you Wouter for taking time and explaining me in a detailed manner. I am reading another article here. It is also nice and it explains both autoboxing and difference between int and Integer.
Thanks a lot.
|
 |
chaitanya karthikk
Ranch Hand
Joined: Sep 15, 2009
Posts: 779
|
|
Hi everybody, after reading all the posts I got a doubt, are String autoboxed?
Can I say "Strings are also autoboxed"?
Thank you all in advance.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32611
|
|
chaitanya karthikk wrote: . . . Can I say "Strings are also autoboxed"? . . .
No.
It's nice to have an easy question occasionally
|
 |
chaitanya karthikk
Ranch Hand
Joined: Sep 15, 2009
Posts: 779
|
|
|
Thank you Ritche.
|
 |
K Abhijit
Ranch Hand
Joined: Mar 03, 2008
Posts: 88
|
|
@Chaitanya:
check your PM
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
K Abhijit, if the PM is relevant to this discussion please post it here. UseTheForumNotEmail also applies to PMs.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
K Abhijit
Ranch Hand
Joined: Mar 03, 2008
Posts: 88
|
|
@Rob : Point Noted
I posted one simple question to Chaitanya-
The reason I preferred to keep it private was:
Request (Chaitanya ) had got his answer to his Q (
Chaitanya karthikk wrote: . . . Can I say "Strings are also autoboxed"? . . .)
by Campbell
But instead of having it answered as YES / NO I was keen to make him think what it's got behind it....
Hence I posed it...
Q --> To which primitive type you can think String could be auto-boxed
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32611
|
|
K Abhijit wrote: . . . The reason I preferred to keep it private was: . . .
That was a useful point, which is why it ought not to be kept quiet.
|
 |
 |
|
|
subject: What is the difference between Integer and int
|
|
|