Results 1 to 10 of 10
  1. #1
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default 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. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,774
    Blog Entries
    7
    Rep Power
    21

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Difference between Integer and int

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

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,774
    Blog Entries
    7
    Rep Power
    21

    Default Re: Difference between Integer and int

    Quote Originally Posted by allaudin View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Difference between Integer and int

    ok thanks josAH ....

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,774
    Blog Entries
    7
    Rep Power
    21

    Default Re: Difference between Integer and int

    Quote Originally Posted by allaudin View Post
    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) {
    
    		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
    Last edited by JosAH; 05-25-2013 at 09:00 PM.
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default 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.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,774
    Blog Entries
    7
    Rep Power
    21

    Default Re: Difference between Integer and int

    Quote Originally Posted by jim829 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: Difference between Integer and int

    Quote Originally Posted by JosAH View Post
    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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,774
    Blog Entries
    7
    Rep Power
    21

    Default Re: Difference between Integer and int

    Quote Originally Posted by jim829 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Integer Comparison, Outputting Largest Integer Not Working
    By killingthemonkey in forum New To Java
    Replies: 4
    Last Post: 10-16-2011, 09:59 PM
  2. Replies: 1
    Last Post: 04-19-2011, 05:32 PM
  3. what the difference?!!
    By Engineer in forum New To Java
    Replies: 14
    Last Post: 08-29-2010, 05:57 PM
  4. convert unsigned integer to signed integer in java?
    By diskhub in forum New To Java
    Replies: 6
    Last Post: 05-17-2010, 01:50 AM

Posting Permissions

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