-1

A simple question about notation.

For what reason do I have to write "int" in parentheses after the equals sign since otherwise compiling fails?

    int number = (int)Math.random();

Why does not compiler recognize second "int" as the first one in the same line?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Fred Lang
  • 3
  • 1
  • The Math.random() function returns a floating-point, pseudo-random number in the range 0–1 (inclusive of 0, but not 1). The 2nd int "(int)" coerces the return type into a type that corresponds to the left side. [see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random] – donPablo Dec 13 '19 at 21:24
  • 1
    Does this answer your question? [In C, why are there parentheses around (int) in this example?](https://stackoverflow.com/questions/30790649/in-c-why-are-there-parentheses-around-int-in-this-example) – Kenneth K. Dec 13 '19 at 21:28
  • thx! Does it mean that int, double, char, String etc. must be set in parentheses if they do not stay at the begin of a line? (for otherwise compiler won't interpret them as key words?) – Fred Lang Dec 13 '19 at 21:53
  • 1
    Related: https://stackoverflow.com/questions/24709284/type-casting-math-random – Mark Rotteveel Dec 14 '19 at 08:18

2 Answers2

2

This is dependent on your language, but Math.random() likely returns a float type, and you're working in a language that does not do implicit type casts. The language says "You're trying to assign a float value to a variable declared as an int, that's a possible bug, since converting an int to a float will lose information!"

By using (int) you're performing a type cast, which instructs the compiler to take the float result from Math.random(), but to interpret it as an int.

Chris Heald
  • 61,439
  • 10
  • 123
  • 137
  • thx! I'm just curious why compiler won't interpret the second int as a key word if it's not put in brackets. – Fred Lang Dec 13 '19 at 21:57
  • That's just how the language defined the syntax for type casts. The (int) typecasting convention goes back at least as far as C, and many languages have emulated it since. From a human perspective, it's helpful to differentiate a type cast from a type declaration. – Chris Heald Dec 13 '19 at 22:00
  • Strictly speaking, Java does some implicit casts, for example from `int` to `double`, and even does some potentially lossy implicit casts such as from `long` to `float`. It's just that `double` to `int` is not one of the allowed implicit casts. – kaya3 Dec 14 '19 at 02:20
  • thx. so the key words like int, double must be put in () brackets if they do not stand at the begin of the line, right? – Fred Lang Dec 14 '19 at 08:10
2

Result of Math.random() has type double. So you must narrow double type to int. After that your program will work, but number value always will be 0, because Math.random value is bigger than 0, but smaller than 1.

JoWyN
  • 21
  • 2
  • thx! That's the reason we put *10 to increase the results and restrict it to integer only by using int. Still I do not know why compiler needs int to be set in (), but no need for the first int. – Fred Lang Dec 13 '19 at 22:02