Results 1 to 11 of 11
- 06-19-2010, 09:18 PM #1
Member
- Join Date
- Jun 2010
- Posts
- 5
- Rep Power
- 0
fundamental question about Integer (for example)
I have a fundamental question about object creation, using an Integer object as an example. This has been bugging me:
My interpretation of this is, "create an instance x of the class Integer, and call the int constructor to store the value 97."Java Code:Integer x = new Integer(97);
If I declare again somewhat differently:
I interpret this as, "create an instance y of the class Integer, then store in it the value 97." I'm sure I'm missing some subtlety of the "=," but we'll get to that.Java Code:Integer y = 97;
When I put my new instances x and y to the test, they don't quite behave as I expect:
...Java Code:Integer x = new Integer(97); //x = 97; Integer y = 97; if (x == y) { System.out.println("x == y"); } else { System.out.println("x != y"); }
====> x != y
Now if I uncomment the line which assigns 97 to x, I get a different result:
...
====> x == y
So I guess the fundamental question is, why are these 2 things different?
Does the difference arise from the "=" itself?Java Code:Integer x = new Integer(97); Integer y = 97;
Thanks for helping me think this through.
Kman
-
You may be confusing one object being the same object as another vs holding the same value as another. A better test is this one:
Java Code:if (x.equals(y)) { System.out.println("x.equals(y)"); } else { System.out.println("!x.equals(y)"); }
- 06-19-2010, 09:38 PM #3
Is there any boxing going on here?
Does the compiler generate the new Integer() for you or some such code.
- 06-19-2010, 09:38 PM #4
Member
- Join Date
- Jun 2010
- Posts
- 5
- Rep Power
- 0
Fubarable, thanks. Basically you're saying I'm abusing the == operator. I was googling around and ran across an explanation pointing out that == compares references, not values. So my test failed because the references x and y are not the same, rather than their values. I think I get it now.
Thanks again.
Kman
- 06-19-2010, 09:41 PM #5
Member
- Join Date
- Jun 2010
- Posts
- 5
- Rep Power
- 0
Norm,
Sorry, not sure what you mean by boxing?
- 06-19-2010, 10:08 PM #6
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
Java doesn't like primitive types like ints, doubles and so on. Java likes objects, like Integers, Doubles etc. This is where the process of autoboxing comes in. Note this snippet:
And you think nothing of it. But the compiler looks at this and thinks, oh my, I have an Integer variable, but the value assigned to it is of the type int. What to do, what to do... I know, instead of bothering the programmer about this minutea, I'll simply convert this call toJava Code:Integer a = 5;
Autoboxing is the compiler being lenient towards you by using primitives and their wrapper classes almost interchangably.Java Code:Integer a = new Integer(5);
Ever seen a dog chase its tail? Now that's an infinite loop.
- 06-19-2010, 10:17 PM #7
Member
- Join Date
- Jun 2010
- Posts
- 5
- Rep Power
- 0
So the compiler is essentially casting the int to Integer when it sees the "=" assignment, right? That's what Norm meant by boxing?
Thanks moon.
-Kman
- 06-19-2010, 10:38 PM #8
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
No, it's not a cast, but rather it's creating a new Integer object even though the code isn't specifically telling the compiler to do this.
- 06-19-2010, 10:49 PM #9
Member
- Join Date
- Jun 2010
- Posts
- 5
- Rep Power
- 0
thanks curmudgeon
- 06-21-2010, 11:02 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,459
- Rep Power
- 16
It doesn't replace it with a "new" call, but with a call to Integer.valueOf(). This allows it to reuse already defined Integer objects, which is why:
returns true. In fact, this is why you generally shouldn't call "new Integer()", since valueOf() (in cases where you are creating lots of numbers) saves some space.Java Code:Integer x = 97; Integer y = 97; if (x == y) {}
- 06-21-2010, 12:40 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
Feeling a bit more confident about autoboxing and caching? You won't be after reading this:
kind regards,Java Code:import java.util.ArrayList; import java.util.List; public class Autoboxing { public static void main(String[] args) { Object foo = new Long(0xcafebabedeadbeefL); List<Object> bars = new ArrayList<Object>(); bars.add(true ? (Long)foo : (Long)foo); bars.add(true ? (Long)foo : (Number)foo); bars.add(true ? (Long)foo : (Double)foo); bars.add(true ? (Long)foo : ((Long)foo).longValue()); System.out.print("== :"); for (Object bar : bars) System.out.print(" "+(foo == bar)); System.out.println(); System.out.print("equals:"); for (Object bar : bars) System.out.print(" "+foo.equals(bar)); System.out.println(); } }
Jos ;-)
Similar Threads
-
convert unsigned integer to signed integer in java?
By diskhub in forum New To JavaReplies: 6Last Post: 05-17-2010, 12:50 AM -
how to know the input value of integer
By ran830421 in forum New To JavaReplies: 15Last Post: 11-18-2009, 09:01 PM -
Integer length
By jithan in forum New To JavaReplies: 1Last Post: 06-12-2008, 03:35 PM -
VeryLong Integer.. help
By hey in forum New To JavaReplies: 4Last Post: 12-14-2007, 09:48 PM -
Integer vs int
By bugger in forum New To JavaReplies: 1Last Post: 11-14-2007, 09:13 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks