Results 1 to 2 of 2

Thread: Integer vs int

  1. #1
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

    Default Integer vs int

    I am new to Java. Can any one tell me the difference between the following:

    Java Code:
    Integer first_i = 2;
    int second_i = 2;
    Integer third_i = new Integer(2);
    They seem to do the same.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    In j2se 1.5 autoboxing was introduced which automatically converts the int value "2" to an Integer object in this statement.
    Java Code:
    Integer first_i = 2;
    Before j2se 1.5 this would have caused a compile error.
    Integer is a wrapper class that wraps the primitive int value and makes it an Object for use where objects are required such as for use in the interface Comparable or for Collection interface implementations, eg, ArrayList.
    For more see The Numbers Classes.

Similar Threads

  1. Breaking down an integer
    By Emily in forum New To Java
    Replies: 1
    Last Post: 03-06-2008, 06:39 PM
  2. Integer Usage
    By bluegreen7hi in forum New To Java
    Replies: 11
    Last Post: 01-24-2008, 08:54 AM
  3. VeryLong Integer.. help
    By hey in forum New To Java
    Replies: 4
    Last Post: 12-14-2007, 09:48 PM
  4. Short/Integer
    By mew in forum New To Java
    Replies: 3
    Last Post: 12-06-2007, 09:28 PM
  5. Help with Integer in java
    By susan in forum New To Java
    Replies: 1
    Last Post: 07-14-2007, 05:25 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
  •