0

people

How come this simple code yield different results?

System.out.println(1000.0/3.0/2.0/4.0/7.0);
System.out.println((double)(1000/3/2/4/7));

the output:

5.9523809523809526
5.0

And how can one perform such a simple arithmetic without casting every single member.

Extra: why does the following piece of code results in "int out of range error", even when I cast it?

long n1 = 291328282135; //Error description: The literal 291328282135 of type int is out of range.

//version with cast:
long n2 = (long) 291328282135; //Error description: The literal 291328282135 of type int is out of range.

Thanks in advance.

Vitor Hugo
  • 17
  • 4
  • 1
    "Extra:" ask one question at a time. – Andy Turner Jan 02 '20 at 19:43
  • A standalone sequence of digits is a 32-bit signed int in Java, so your second line of code does integer math and your ‘extra’ code is defining a number too large to fit in 32 bits. – VGR Jan 02 '20 at 19:45

0 Answers0