Solved

# Java: Output 10%3...

Posted on 2007-10-19
189 Views
Hi,

I have 2 questions like this:

System.out.println(10.0/3.0+5.0*2.0) where the answer is 13.33333......4

My question is how long would 33333333 be  and why? coz i printed out and it was pretty long without truncating..

My second question is that :

System.out.println(10%3+5%2) where the answer printed was 2.0

I thought it should be 2 instead of 2.0 as both are integers and there is no floats or doubles that allowed it to be promoted.

Pls clarify. thanks
0
Question by:zizi21

LVL 86

Accepted Solution

>>My question is how long would 33333333 be  and why? coz i printed out and it was pretty long without

That would be as long as the precision of double allows

>>System.out.println(10%3+5%2) where the answer printed was 2.0

That can't be right. It would print 2
0

Author Comment

this is the answer given by my teacher and it says 2.0

System.out.println(10 % 3 + 5 % 2);     .................     2.0
0

Author Comment

could she be wrong ?
0

LVL 86

Expert Comment

>>could she be wrong ?

Could be a misprint. Any of those numbers written as a decimal would produce 2.0 for instance
0

LVL 3

Expert Comment

Well, if you execute this testing code you'll see by yourself that System.out.println(10 % 3 + 5 % 2); prints 2, while System.out.println(10.0 % 3.0 + 5.0 % 2.0); prints 2.0. That's because the first numbers are integers, so the result is an integer, while the second option has double, so the result is a double

package test;

public class Test {
public static void main(String[] args) {
System.out.println(10 % 3 + 5 % 2);
System.out.println(10.0 % 3.0 + 5.0 % 2.0);
}
}
0

Author Comment

thank you..btw, what is the precision for double ?
0

LVL 86

Expert Comment

0

LVL 86

Expert Comment

:-)
0

## Featured Post

### Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.