Results 1 to 4 of 4
  1. #1
    Gobi is offline Member
    Join Date
    Jun 2011
    Posts
    13
    Rep Power
    0

    Question What is the diff between int and Integer in terms of array

    Hi ,
    I'm newbie to Java.
    Could anyone pls tell me the diff between int and Integer in terms of Array.

    int[] iarray = new int[10];

    Integer[] iarray = new Interger[10];

    I know that , first one is primitive and second one is class.
    The second one requires 80 bytes of memory in order to complete the task, as it stores the reference in the array.
    Want to know abt the first one , will it behave same as Integer ?
    If I want to store 20 integer value , then which one is best WRT performance?

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default

    Quote Originally Posted by Gobi View Post
    Could anyone pls tell me the diff between int and Integer in terms of Array.
    What do you mean by difference? Arrays are arrays are arrays regardless of the type stored in them. They do not exhibit any different behaviour. You use them exactly the same. However, since Integer is the wrapper class of the primitive int and they introduced autoboxing you can do this:
    Java Code:
            int[] pArray = new int[5];
            Integer[] cArray = new Integer[5];
            pArray[0] = 10;
            pArray[1] = new Integer(20);
            cArray[0] = 30;
            cArray[1] = new Integer(40);
            System.out.println(pArray[0]);
            System.out.println(pArray[1]);
            System.out.println(cArray[0]);
            System.out.println(cArray[1]);
    Notice how you can insert an int into the Integer array and vice versa. What actually happens behind the scenes is the int value is autoboxed into an Integer object and the Integer object is autounboxed into an int.

  3. #3
    Gobi is offline Member
    Join Date
    Jun 2011
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    What do you mean by difference? Arrays are arrays are arrays regardless of the type stored in them. They do not exhibit any different behaviour. You use them exactly the same. However, since Integer is the wrapper class of the primitive int and they introduced autoboxing you can do this:
    Java Code:
            int[] pArray = new int[5];
            Integer[] cArray = new Integer[5];
            pArray[0] = 10;
            pArray[1] = new Integer(20);
            cArray[0] = 30;
            cArray[1] = new Integer(40);
            System.out.println(pArray[0]);
            System.out.println(pArray[1]);
            System.out.println(cArray[0]);
            System.out.println(cArray[1]);
    Notice how you can insert an int into the Integer array and vice versa. What actually happens behind the scenes is the int value is autoboxed into an Integer object and the Integer object is autounboxed into an int.
    lemme try out this . thanks for your reply :)

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    If I want to store 20 integer value , then which one is best WRT performance?
    Try both and use a stopwatch or whatever to time them. I doubt if you will detect a difference.

Similar Threads

  1. Integer value in Character Array
    By AwesomeGuy in forum New To Java
    Replies: 1
    Last Post: 04-09-2011, 07:41 PM
  2. arraylist to integer array
    By monika in forum Advanced Java
    Replies: 3
    Last Post: 04-26-2010, 09:09 PM
  3. An Array of different integer values
    By lithium002 in forum New To Java
    Replies: 7
    Last Post: 12-04-2009, 05:25 AM
  4. Replies: 1
    Last Post: 03-31-2009, 06:40 AM
  5. Java terms
    By Shaolin in forum New To Java
    Replies: 2
    Last Post: 12-27-2007, 05:02 PM

Tags for this Thread

Posting Permissions

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