2

Why does this java code

long a4 = 1L;
long a3 = 1;
long a2 = 100L * 1024 * 1024 * 1024;
long a1 = 100 * 1024 * 1024 * 1024;
System.out.println(a4);
System.out.println(a3);
System.out.println(a2);
System.out.println(a1);

when run, output

1
1
107374182400
0

instead of the expected

1
1
107374182400
107374182400

output?

5
  • 1
    That line you actually wrote 100 and not 100L, Hence integer multiplication and resulted > integer capacity and resulted in zero, called as integer overflow. Commented Dec 3, 2013 at 14:19
  • possible duplicate of How does Java handle integer underflows and overflows and how would you check for it? Commented Dec 3, 2013 at 14:22
  • @JeroenVannevel That might be a duplicate, had I known it was an overflow situation before getting your answers. Commented Dec 3, 2013 at 14:29
  • 1
    @dontomaso I think it was bad luck (or by design if this is a class assignment) that it is exactly 50 times the full integer swing otherwise it would have been more obvious that it was an overflow Commented Dec 3, 2013 at 14:35
  • possible duplicate of Why do these two multiplication operations give different results? Commented Dec 3, 2013 at 18:02

9 Answers 9

6
 long a2 = 100L * 1024 * 1024 * 1024;

In this operation however at least one operand is long. hence the operation is carried out using 64-bit precision, and the result of the numerical operator is of type long. The other non-long operand are widened to type long by numeric promotion and resulted value gets stored to to variable a2.

 long a1 = 100 * 1024 * 1024 * 1024;

The constant expression of plain integer, the result of the expression was computed as a type int. The computed value however too large to fit in an integer and hence overflowed, resulting in 0 and gets stored to a1 variable.

Edit: As is asked in the following comment:

Why doesn't it go negative?

Because while in integer computation the second computation is equivalent to 25 * 2^32 where ^ has the power meaning and 2^32 integer value is 0. However, to explain why it's value is 0: In binary:

 100 * 1024 * 1024 * 1024 == 25 * 2^32;

 Integer.MAX_VALUE =  2 ^ 31 -1 = 0 11111111 11111111 11111111 1111111
 Integer.MAX_VALUE + 1 = 2 ^ 31 = 1 00000000 00000000 00000000 0000000 

2 ^ 31 is a negative integer(-2147483648) as the sign bit is 1 And hence 2 ^ 32 is just a multiplication of 2 to 2 ^ 31: a left shift and the sign bit will become 0 and hence the result is 0.

Check out the java language specification: 4.2.2: Integer operation for details.

Sign up to request clarification or add additional context in comments.

16 Comments

Why doesn't it go negative?
@SteveP. Because 107374182400/2^32=25, exactly, it goes around 25 times to sit back at 0. Which I explain in detail here
@RichardTingle you should your comment to your answer | that holds a key point in explanation
@Sage 100 * 1024 * 1024 * 1024 will always be evaluated by JVM from left to right not the way you mentioned 100 * 1024 * 1024 * 1024 == 25 * 2^32 i.e. it is evaluated as (((100 * 1024 ) * 1024) * 1024 ) as it is mentioned in docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.7
@Sage the only reason is the explanation given by you for an answer why it evaluates down to 0 and if you read the link docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.2 the answer got by multiplication of i*i where int i = 1000000; is a negative number -727379968 not 0
|
5

107374182400 is exactly 25 times the full range of an integer (2^32), which means that if you try to fit it into an integer it will overflow. And because it would fit exactly 25 times it ends up precisely on 0 (this is a coincidence and other huge multiplications may end up either positive or negative). And you are using an integer right up to the point you cast to long

long a1 = 100 * 1024 * 1024 * 1024;

is equivalent to

int temp = 100 * 1024 * 1024 * 1024;
long a1 = (long)temp;

If you put a single long in the expression, it is forced to use long maths not integer maths which removes the problem

3 Comments

ooppss I took Sage' answer as yours. yeah now can see change +1
@sansix With it's current +4 I wouldn't mind stealing it, but alas. Thanks for the +1
Sorry was a bit late to respond with every one answer but: which means that if you try to fit it into an integer it will overflow 25 times and end up on precisely on 0 this is not exactly the reason. 2 ^ 32 is 0 while computing in integer as i have mentioned in my answer
4

It might be that the expression on the right of a1 is first calculated as an int and later on converted to long. If it equals 0 as an int it'll stay 0 as a long

Comments

2

As per the documentation of Lexical Literals it is mentioned that,

The type of a literal is determined as follows:
- The type of an integer literal (§3.10.1) that ends with L or l is long (§4.2.1).
- The type of any other integer literal is int (§4.2.1).

Thus your expression, 100 * 1024 * 1024 * 1024 is evaluated as int primitive data type because l or L is not mentioned in any numeric value. and the result is 107374182400 i.e. in Binary it is 1 1001 0000 0000 0000 0000 0000 0000 0000 0000 and int is 32-bit so low 32-bit are taken as mentioned in Example 4.2.2-1. Integer Operations which results to 0

It is also mentioned in same documentation that,

If an integer operator other than a shift operator has at least one operand of type long, then the operation is carried out using 64-bit precision, and the result of the numerical operator is of type long. If the other operand is not long, it is first widened (§5.1.5) to type long by numeric promotion

It means any value in expression contains l or L then all the int values will be expanded to 64 bit.

EDIT In the comment it is also asked

Why doesn't it go negative?

I think it also answers the above question

5 Comments

perfect explanation with correct java specifications why a numeric values is consider int not byte, long or short
@SteveP it answers your question why value does not go negative
because, your answer has been later psoted by following others. You haven't mentioned anything more than other answer has already described. You have edited your answer completely after 16 hours later as your edit history tell us. And your first answer was: The code long a1 = 100 * 1024 * 1024 * 1024; produces 0 because 100 * 1024 * 1024 * 1024 is manipulated by using the output as int and then it caste to long was not meaningful at all. There is no reason to clutter the website with repetitive information
@Sage that is the only reason why I edited my answer to provide correct explanation after a long search in JAVA Specification why expression boils down to int expression not byte, short, long or any other
That specification has already been linked 16 hours before you and the reason was also explained 16 hours before you. Read all the answer which were completely right 16 hours before yours one.
1

long a4 = 1L; //No problem in this

long a3 = 1; //here left side primitive is considered as integer and at the time of assignment it will be casted to long so again result is as expected

long a2 = 100L * 1024 * 1024 * 1024; (here you have used 100L so others will be type casted to Long so expected output)

long a1 = 100 * 1024 * 1024 * 1024; (as by default any primitive digit is considered as int in java, it will consider this as integer multiplication so it goes out of range and results in 0)

Comments

0

Here's what you did : You assigned 100 * 1024 * 1024 * 1024 to a long data type but you didn' said that 100 * 1024 * 1024 * 1024 is a long value

By default java compiler thinks that its an integer. Since integer cannot hold that much value, it will show wrong result. Hope it helps !

Comments

0

reason is integer overflow
as output of 100 * 1024 * 1024 * 1024; is an integer(int) not long

and in long a2 = 100L * 1024 * 1024 * 1024; you are specifying that one of the value is long (here 100L) and multiplication with that value results in long value which gets correctly stored in a2

Comments

0

In Java, if you have int * int, it will compute the output as an int. It only gives the result as a long if you do int * long. In your case, the 100 * 1024 * 1024 * 1024 has a result that overflows int.

So, adding a "L" makes the operand to be a long, and the computation stores the values in long. And of course, no overflow occurs and a correct result can be outputed (i.e. a2).

Comments

0

Number 3 worked because you specified a long type which is 100L. Thats why it is a long multiplication and could be stored. On the other hand number 4 is an integer multiplication with max value 2^32-1 thats why you got an overflow and the zero default valued appeared.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.