Results 1 to 8 of 8
Like Tree2Likes
  • 1 Post By pbrockway2
  • 1 Post By Tolls

Thread: Converting primitive int to Intger object?

  1. #1
    NewbieDan is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default Converting primitive int to Intger object?

    Hello, I need to convert a primitive int to an Integer object.

    I know of three ways to do this but was just wondering what the differences are between each way and if I should use one way over the other.

    int num1 = 452;
    int num2 = 455;

    1. Integer integerNum = num;
    2. Integer integerNum = new Integer (num);
    3. Integer integerNum = Integer.valueOf(num);

    I'm needing to use compareTo to compare the values of two primitive int types but it's saying I can't use compareTo on primitive data types so I need to convert one int to an Integer object then autoboxing (I think that's the correct term) will take care of the second int.

    return integerNum.compareTo(num2);

    Thanks for the help!

  2. #2
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default Re: Converting primitive int to Intger object?

    Integer intergerNum = num doesn't compare anything. = is an assigment operator. To compare to ints you need to use the == comparison operator.

    Here's a link to the integer wrapper class in the java Api docs:Java Platform SE 7 It explains the differences. Every primitive type has a wrapper class.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Converting primitive int to Intger object?

    Quote Originally Posted by NewbieDan View Post
    Hello, I need to convert a primitive int to an Integer object.

    I know of three ways to do this but was just wondering what the differences are between each way and if I should use one way over the other.

    int num1 = 452;
    int num2 = 455;

    1. Integer integerNum = num;
    2. Integer integerNum = new Integer (num);
    3. Integer integerNum = Integer.valueOf(num);
    They do the same thing -- they all produce an Integer object with the correct value and which will work well with your compareTo method, but as always, check the API as it will give you guidance in which is best to use, and the answer appears to be to use Integer.valueOf(...). I'll leave it to you to read the API to see why.

    Quote Originally Posted by mwr1976 View Post
    Integer intergerNum = num doesn't compare anything. = is an assigment operator. To compare to ints you need to use the == comparison operator.
    @mwr: It's obvious from the original poster's original post (if you read it well) that he understands what an assignment operator is and that his current code does no comparison.

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

    Default Re: Converting primitive int to Intger object?

    I'm needing to use compareTo to compare the values of two primitive int types but it's saying I can't use compareTo on primitive data types so I need to convert one int to an Integer object
    Personally, I would use the int values as they are, and if I wanted to compare them I would use <,>,== etc. But perhaps you have something specific in mind that lies behind these "needs".

    As Fubarable says valueOf() is preferable to the constructor. But, as far as I know, boxing does the same thing as valueOf(), so if the comparison necessarily had to be between Integer instances (but I still wanted the caching) I would go for option 1 because it's less typing.
    Fubarable likes this.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Converting primitive int to Intger object?

    Yep.
    If you were to look at the byte code you would see that autoboxing does a valueOf().
    And valueOf() (pre-autoboxing) was the recommended method of getting a wrapper object from a primitive.

  6. #6
    NewbieDan is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default Re: Converting primitive int to Intger object?

    Thank for the help. I figured Integer.valueOf() would be better to use and have been going with that so far.

    In my situation I don't think using ==, <= or >= would really work. Well it would but it would just clutter up the code and make it more confusing.

    I am using this in a comparator (to organize a list collection) that is also comparing a bunch of strings before comparing the integers.

    Here is what I have so far and it is working great:

    Java Code:
    public static final Comparator<Student> COMMUNITY_LAST_FIRST_ID_ORDER = new Comparator<Student>()
      {
        public int compare(Student e1, Student e2)
        {
          int result = 0;
          result = e1.getCity().compareTo(e2.getCity());
          if (e1.getCity().equals(e2.getCity()))
          {
            result = e1.getLast().compareTo(e2.getLast());
            if (e1.getLast().equals(e2.getLast()))
            {
              result = e1.getFirst().compareTo(e2.getFirst());
              if (e1.getFirst().equals(e2.getFirst()))
              {
                Integer id1 = Integer.valueOf(e1.getId());
                result = id1.compareTo(e2.getId());
              }
            }
          }
          return result;
        }

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Converting primitive int to Intger object?

    You could skip the equals() calls and just check whether result == 0.
    That would save potentially a lot of work for the JVM as currently it is checking the two things in full twice.
    Fubarable likes this.

  8. #8
    NewbieDan is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default Re: Converting primitive int to Intger object?

    Ah, good call! Thanks!

Similar Threads

  1. converting Image to File object !
    By ron2794 in forum Advanced Java
    Replies: 5
    Last Post: 07-15-2011, 07:59 PM
  2. Replies: 2
    Last Post: 05-27-2011, 05:39 PM
  3. Converting an array in an object
    By fiefa in forum New To Java
    Replies: 1
    Last Post: 01-31-2011, 05:18 PM
  4. stumpt. converting an object to a long int.
    By root_demon in forum New To Java
    Replies: 7
    Last Post: 10-31-2008, 06:32 AM
  5. Converting object to string
    By Preethi in forum New To Java
    Replies: 4
    Last Post: 06-14-2008, 04:29 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
  •