?
Solved

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

Posted on 2005-03-25
8
Medium Priority
?
288 Views
Last Modified: 2010-03-31
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
Comment
Question by:BrianMc1958
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 

Author Comment

by:BrianMc1958
ID: 13630619
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
 
LVL 6

Assisted Solution

by:guitaristx
guitaristx earned 200 total points
ID: 13630642
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
 

Author Comment

by:BrianMc1958
ID: 13630650
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 37

Accepted Solution

by:
zzynx earned 300 total points
ID: 13630694
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13630712
It has add(), subtract(), multiply(), divide() and more
0
 

Author Comment

by:BrianMc1958
ID: 13630861
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 13630877
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
 
LVL 37

Expert Comment

by:zzynx
ID: 13630935
Thank you
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month12 days, 10 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question