Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: doing arithmetic on Integer objects

  1. #1
    Member
    Join Date
    Oct 2010
    Posts
    31
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default doing arithmetic on Integer objects

    Is there a way to do arithmetic on Integer objects? For example, I tried the following without success:

    Integer I = new Integer(1);
    I++;

    I also noticed there is no setValue(int) method in the Integer class, so I can't do this:

    I.setValue(I.intValue() + 1);

    The only way I can think of to change the value of an Integer (or any Number object) through arithmetic is to create a new one based on the value of the old one. So something like this:

    I = new Integer(I.intValue() + 1);

    Is this really the only way to do it?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: doing arithmetic on Integer objects

    Any reason you must rely on an Integer object rather than the primitive? The Integer class is is a wrapper class whose value is final, so to modify it you must create a new Integer object derived from the previous, or use primitives.

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: doing arithmetic on Integer objects

    copeg is absolutely correct, but you could also do this:

    Integer i = new Integer(1); //could also be Integer i = 1;
    i = i + 1;

    Google autoboxing.

  4. #4
    Member
    Join Date
    Oct 2010
    Posts
    31
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: doing arithmetic on Integer objects

    Quote Originally Posted by KevinWorkman View Post
    copeg is absolutely correct, but you could also do this:

    Integer i = new Integer(1); //could also be Integer i = 1;
    i = i + 1;

    Google autoboxing.
    Well, the compiler is happy with this.

    I'm using Integers because I'm placing them in a Vector.

  5. #5
    Member
    Join Date
    Oct 2010
    Posts
    31
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: doing arithmetic on Integer objects

    It seems this *can* work (depending on your application), but one should know this operation creates a new Integer object out of the old. So if you had any other references to the old one, they will not point to the new one.

Similar Threads

  1. Help with ArrayList<Integer>
    By shanklove in forum Collections and Generics
    Replies: 2
    Last Post: September 6th, 2010, 09:15 AM
  2. Arithmetic’s Game ??
    By holy crab in forum Algorithms & Recursion
    Replies: 0
    Last Post: April 12th, 2010, 04:27 AM
  3. Using Integer.parseInt
    By Fraz in forum Object Oriented Programming
    Replies: 1
    Last Post: April 5th, 2010, 07:50 AM
  4. Hex to Integer Problem
    By TempSpectre in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 1st, 2010, 06:01 AM
  5. int and Integer
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: December 31st, 2009, 03:20 AM