Difference between Integer and int

• 05-25-2013, 05:07 PM
allaudin
Difference between Integer and int
While searching difference between Integer and int I learnt that int is primitive datatype and Integer is object. Ok that's good. Now as Integer is object so we should write like {Integer n = new Integer(40);} but its also works by writing Integer n = 40 ... ??? Whats the logic behind this ? Thanks in advance
• 05-25-2013, 05:31 PM
JosAH
Re: Difference between Integer and int
That is a mechanism called 'auto boxing'; everywhere where an Integer is expected and an int is supplied, the int is automatically 'boxed' in an Integer object. Vice versa works too. b.t.w. use the static Integer.valueOf(int i) method instead of creating new Integer objects.

kind regards,

Jos
• 05-25-2013, 05:36 PM
allaudin
Re: Difference between Integer and int
you mean Integer n = 40 is automatically boxed to Integer n = new Integer(40) ?
• 05-25-2013, 06:18 PM
JosAH
Re: Difference between Integer and int
Quote:

Originally Posted by allaudin
you mean Integer n = 40 is automatically boxed to Integer n = new Integer(40) ?

Yep, the compiler does it for you; actually is creates the code: Integer n= Integer.valueOf(40).

kind regards,

Jos
• 05-25-2013, 07:41 PM
allaudin
Re: Difference between Integer and int
ok thanks josAH ....
• 05-25-2013, 07:56 PM
JosAH
Re: Difference between Integer and int
Quote:

Originally Posted by allaudin
ok thanks josAH ....

You're welcome of course; here's a nice little riddle to spice things up a bit ;-)

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();         } }```
Try to explain its output.

kind regards,

Jos
• 05-25-2013, 08:32 PM
jim829
Re: Difference between Integer and int
Here's a hint: Chapter*15.*Expressions

BTW, I didn't know this until I consulted the JLS. Not something one would typically find in a tutorial. Unfortunately I find reading the JLS painful. I can only do it in small doses.

Regards,
Jim
• 05-26-2013, 10:48 AM
JosAH
Re: Difference between Integer and int
Quote:

Originally Posted by jim829
Here's a hint: Chapter*15.*Expressions

BTW, I didn't know this until I consulted the JLS. Not something one would typically find in a tutorial. Unfortunately I find reading the JLS painful. I can only do it in small doses.

Yup, it's language lawyer speak and it doesn't read as a novel. Especially chapter 15 is a hodge podge of 'rules' (look at those bounded generics ...); my example shows the idiosyncracies of the ternary operator rules together with those (un)boxing rules ...

I do hope they're never going to allow user defined operator overloading because that chapter will explode ...

kind regards,

Jos
• 05-26-2013, 03:52 PM
jim829
Re: Difference between Integer and int
Quote:

Originally Posted by JosAH
I do hope they're never going to allow user defined operator overloading because that chapter will explode ...

kind regards,

Jos

Interesting you should mention operator overloading. I had looked for operator overloading when I first started using Java (having come from a Perl background). I don't miss not having it but I wish there was an easy way the designers could incorporate operator overloading for the BigInteger and BigDecimal classes. But even that could be difficult. Especially if it would allow "interoperation" with regular numeric types. That could be a real nightmare!

Regards,
Jim
• 05-26-2013, 06:46 PM
JosAH
Re: Difference between Integer and int
Quote:

Originally Posted by jim829
Interesting you should mention operator overloading. I had looked for operator overloading when I first started using Java (having come from a Perl background). I don't miss not having it but I wish there was an easy way the designers could incorporate operator overloading for the BigInteger and BigDecimal classes. But even that could be difficult. Especially if it would allow "interoperation" with regular numeric types. That could be a real nightmare!

Yup, and that only for a notational convenience (i.e. being able to write "a+b" instead of "a.add(b)"); but imagine if operators could be overloaded for classes that don't even resemble numbers (as in C++). It isn't such a burden if you know what you're doing but this forum would need another entire section for it: "why can't I multiply my Strings with a HashMap?" *shudder*

kind regards,

Jos