• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 300
  • Last Modified:

Know of a short tutorial on Integer (not int) math?

Dear Experts,

Can anyone point me to a short tutorial on doing math with Integers (not int).  I know it can't be done directly.  Unfortunately, I can think of twenty million easy ways to do it myself.  But I'm looking for the "standard Java" way.

Thanks!
--BrianMc1958
0
BrianMc1958
Asked:
BrianMc1958
2 Solutions
 
BrianMc1958Author Commented:
For instance, given:

    Integer thisI = new Integer(1);
    Integer thatI = new Integer(2);

You could write:

    Integer result = new Integer(thisI.intValue() + thatI.intValue());

I would immediately write a method something like:

  public Integer addInt(Integer int1, Integer int2)
    {
      return new Integer(int1.intValue() + int2.intValue());
    }

and then just do:

Integer result = new Integer(thisI.intValue() + thatI.intValue());



0
 
guitaristxCommented:
Start here:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Integer.html

It appears as though you'd have to convert any value stored in an Integer to an int to be able to do most arithmetic operations anyway.  Perhaps you're over-complicating the problem?
0
 
BrianMc1958Author Commented:
SORRY!  I HIT THE WRONG KEY ON MY LAST COMMENT!  THIS IS WHAT I MEANT TO SAY:

For instance, given:

    Integer thisI = new Integer(1);
    Integer thatI = new Integer(2);

You could write:

    Integer result = new Integer(thisI.intValue() + thatI.intValue());

I would immediately write a method something like:

  public Integer addInt(Integer int1, Integer int2)
    {
      return new Integer(int1.intValue() + int2.intValue());
    }

and then just do:

  Integer result = addInt(thisI, thatI);

However, it seems like this solution should already be pretty much part of the language!  

Does the Math library solve this?

Thanks,
BrianMc1958
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
zzynxSoftware engineerCommented:
0
 
zzynxSoftware engineerCommented:
It has add(), subtract(), multiply(), divide() and more
0
 
BrianMc1958Author Commented:
Thanks again, folks.  I think guitaristx is correct that I'm overcomplicating.  However, I might go to zzynx's suggestion for BigInteger later.  It's very close to what I wanted.

--BrianMc1958
0
 
CEHJCommented:
You wouldn't normally want to be doing that sort of thing. BigInteger is a special case, for handling, er ... big integers ;-)

You wanted the Integer to preserve your three 'db states' (+ve, -ve and null). That fits. Anything else and you should probably just get the primitives from the wrapper and use them
0
 
zzynxSoftware engineerCommented:
Thank you
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now