# Thread: Difference between Integer and int

1. ## 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

2. ## 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

3. ## Re: Difference between Integer and int

you mean Integer n = 40 is automatically boxed to Integer n = new Integer(40) ?

4. ## Re: Difference between Integer and int

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

5. ## Re: Difference between Integer and int

ok thanks josAH ....

6. ## Re: Difference between Integer and int

Originally Posted by allaudin
ok thanks josAH ....
You're welcome of course; here's a nice little riddle to spice things up a bit ;-)

Java Code:
```import java.util.ArrayList;
import java.util.List;

public class Autoboxing {

public static void main(String[] args) {

List<Object> bars = new ArrayList<Object>();

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
Last edited by JosAH; 05-25-2013 at 09:00 PM.

7. Senior Member
Join Date
Jan 2013
Location
United States
Posts
2,692
Rep Power
4

## 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
Last edited by jim829; 05-26-2013 at 06:15 AM.

8. ## Re: Difference between Integer and int

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

9. Senior Member
Join Date
Jan 2013
Location
United States
Posts
2,692
Rep Power
4

## Re: Difference between Integer and int

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

10. ## Re: Difference between Integer and int

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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•