This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Math Operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Math Operators" Watch "Math Operators" New topic
Author

Math Operators

Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
Okay.. so here's the question...
When doing multiplication and you have 2 ints, will you always get an INT as an anwer, and if not, what does it change to and how do I know the differences.. when to change the type and when not to.
Thanks
Dale
------------------
What's this H2SO4 doing in my fridge?? ( thud )


By failing to prepare, you are preparing to fail.<br />Benjamin Franklin (1706 - 1790)
Dennis Hicks
Greenhorn

Joined: Jul 09, 2001
Posts: 12
I believe that you always get an int result (or a larger data type) as a result of an arithmetic operation.
If your operation involves data types narrower than an int, they are implictly converted to ints - that's what I believe anyway.
Say:
byte a = 2;
byte b = 3;
byte c = a * b; // will not work, as result is int type
but....
byte c = (byte) a * b; // will work...

But say you did:

double a = 2.0;
double b = 3.0;
double c = 0.0;
c = a * b;
System.out.println ("Result: " + c);
This would return a doube result ok.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4442
    
    5

The result of any arithmetic operation on 2 ints will always be an int. The result value may or may not be mathematically correct though as any overflow / underflow is ignored and you simply get back the low order 32 bits of the result.
You can convert the integer result to another type by assigning it to a variable of that type, using a cast if necessary. However, any overflow or underflow will already be lost. Consider the following code:

<pre>
int i = (Integer.MAX_VALUE) + 5;
System.out.println(i); // -2147483644
long l = (Integer.MAX_VALUE) + 5;
System.out.println(l); // -2147483644 - same result as before!
</pre>
The result is the same even if you assign it to a long because the overflow was already lost. However, if you cast one of the operands to long, the whole result will be long (64 bits) and you won't lose any precision.
<pre>
long l = (long)(Integer.MAX_VALUE) + 5;
System.out.println(l); // 2147483652 - correct result
</pre>

[This message has been edited by JUNILU LACAR (edited July 09, 2001).]


Junilu - [How to Ask Questions] [How to Answer Questions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Math Operators
 
Similar Threads
grouping labels and buttons in a JPanel with other JPanels
Event handling
Why final object value can be change whereas final variable value can't?
can jvmarg parameter be input in the command line.
Getting used to Maven