Results 1 to 5 of 5
  1. #1
    Shayy is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Default casting from int to Integer question

    Hi All,
    I'm newbie to Java so please execue me if my question is a bit lame.

    I have the following code in the main section of my program:

    int num = 100;

    Object[] anArray = new Object[10];

    for (int i = 0; i < anArray.length; i++) {
    if(num % (i+1) == 0){
    anArray[i] = num / (i+1);
    }
    else{
    anArray[i] = (double)num / (i+1);
    }
    }
    System.out.println("-------Integers-------");
    for (int i = 0; i < anArray.length; i++) {
    if(anArray[i] instanceof Integer){
    System.out.println(anArray[i]);
    }
    }
    System.out.println("-------Doubles-------");
    for (int i = 0; i < anArray.length; i++) {
    if(anArray[i] instanceof Double){
    System.out.println(anArray[i]);
    }
    }

    What I don't understand is the 'Integer' of this line "if(anArray[i] instanceof Integer)".
    When and when did I cast from int Integer?

    Thanks you all in advance

    Shay

  2. #2
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default Re: casting from int to Integer question

    ^Difference between 'int' and 'Integer' is that you can use 'Integer' like object, but 'int' is just primitive type.

    "if(anArray[i] instanceof Integer)"
    In this line we are asking if object of anArray at index i is Integer object. If it's Integer object (int type can't be object it's just primitive type) then we are printing result.

  3. #3
    Shayy is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Default Re: casting from int to Integer question

    Quote Originally Posted by cselic View Post
    ^Difference between 'int' and 'Integer' is that you can use 'Integer' like object, but 'int' is just primitive type.

    "if(anArray[i] instanceof Integer)"
    In this line we are asking if object of anArray at index i is Integer object. If it's Integer object (int type can't be object it's just primitive type) then we are printing result.
    Hello cselic and thank you for you answer.



    So the anArray @ i is of type Integer ("if(anArray[i] instanceof Integer)") because of this line (Object[] anArray = new Object[10];)?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default Re: casting from int to Integer question

    Quote Originally Posted by Shayy View Post
    When and when did I cast from int Integer?

    Thanks you all in advance

    Shay
    When you added the 'int' to the array.
    It's called autoboxing.
    The compiler saw you wanted to add an int to something that holds Objects so autoboxed the int into an Integer.

    Same with the Doubles.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Shayy is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Default Re: casting from int to Integer question

    Thanks for your explanation.

    I understand now.

Similar Threads

  1. Integer parseInt(str) question
    By prasads in forum New To Java
    Replies: 3
    Last Post: 03-07-2011, 10:10 PM
  2. Replies: 2
    Last Post: 12-14-2010, 08:01 PM
  3. Why is there (Integer) casting?
    By hitesh_public in forum New To Java
    Replies: 1
    Last Post: 11-15-2010, 10:43 AM
  4. fundamental question about Integer (for example)
    By kmansj in forum New To Java
    Replies: 10
    Last Post: 06-21-2010, 12:40 PM
  5. Replies: 45
    Last Post: 05-25-2010, 08:32 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
  •