Java question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Java question

Why is Integer.min_value - integer.max_value /2= 0?

20th Sep 2019, 7:56 PM
R.J
R.J - avatar
5 Answers
+ 2
Try this System.out.print((double)(Integer.MIN_VALUE-Integer.MAX_VALUE)/2);
20th Sep 2019, 9:05 PM
D_Stark
D_Stark - avatar
+ 4
Simply primitive datatypes will have weird behavior when their values cross the limits.
20th Sep 2019, 9:00 PM
Seb TheS
Seb TheS - avatar
+ 3
I assume it's because crossed the integer minimum size limit. Smallest possible primitive integer in Java is -2^31, and biggest possible primitive integer in Java is 2^31-1. -2^31 - 2^31-1 = -2^32-1 -2^32-1 < -2^31
20th Sep 2019, 8:46 PM
Seb TheS
Seb TheS - avatar
+ 2
~ swim ~ yes that's right 👍,I thought he might of been looking for somthing diffrent becuase 0 is the correct answer... who knows 😅
20th Sep 2019, 10:20 PM
D_Stark
D_Stark - avatar
+ 1
~ swim ~ I got the value 0.5 but was converted to int 0 so I needed to cast it back to double? also due to order of operation / has higher precedence then -
20th Sep 2019, 9:51 PM
D_Stark
D_Stark - avatar