0

What is the value assigned to num

What is the value assigned to num in the statement: double num = 5/2;? Some websites say 2 some say 2.5. Help please.

14th Apr 2017, 8:54 PM
NewToCodingTexas
3 Answers
+ 5
2 in JAVA Because 5/2 is 2.5 ,Round off 2. For 2.5, double x=5/2f;
14th Apr 2017, 9:01 PM
Meharban Singh
Meharban Singh - avatar
+ 3
It will be 2. This is because 5 and 2 are both integer literals, so integer division will be performed. As per the definition of integers, there won't be any floating point component (any remainder will just be dropped). To get 2.5, at least one of them will need to be floating point literal, and the other will be implicitly converted. E.g., 5.0 or 2.0
14th Apr 2017, 11:25 PM
Squidy
Squidy - avatar
0
You can test it on the code playground.
14th Apr 2017, 8:58 PM
merkrafter