Home » Java Programming » Java.lang Class

What will be the output of the program? int i = (int) Math.random();

Correct Answer: i = 0

Explanation:

Math.random() returns a double value greater than or equal to 0 and less than 1. Its value is stored to an int but as this is a narrowing conversion, a cast is needed to tell the compiler that you are aware that there may be a loss of precision.


The value after the decimal point is lost when you cast a double to int and you are left with 0.


← Previous Question Next Question→

More Questions from Java.lang Class

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion