I have a line of code in java, but I don't know how to explain how it works, someone knows what the -1+ (int) means
-1+ (int) ((Math.random () * (3)));
I have a line of code in java, but I don't know how to explain how it works, someone knows what the -1+ (int) means
-1+ (int) ((Math.random () * (3)));
The (int)
actually goes with the next portion of code: it casts the result of ((Math.random () * (3)))
to an integer. (This will simply drop the decimal portion; it will not round).
Math.random()
returns a number that is greater than or equal to 0.0 and less than 1.0.
((Math.random () * (3)))
simply returns a double
that is greater than or equal to 0.0 and less than 3.0, which will, as I just mentioned, subsequently be cast to an int
. (This will result in a number between 0 and 2; 3 isn't possible).
Adding -1 to something is equivalent to subtracting 1.
So, this will result in a random integer between -1 and 1 (inclusive).
someone knows what the -1+ (int) means
This is the wrong way to look at it. Instead, you should break it down this way:
The +
is adding two things together. On the left we have -1
a literal integer. On the right we have (int) ((Math.random () * (3)))
. Now we can break down what's in the parentheses. First we multiple two values, the result of Math.random()
and the value (3). The result is a floating point number and the
(int)` casts it to an integer.
What does -1+ (int) mean?
You are having problems understanding this is because you haven't grasped Java precedence rules and (apparently) what a type-cast looks like.
Lets start with this part:
((Math.random () * (3)))
That means:
Math.random()
methodNote that there some extra parentheses here that aren't necessary. We could just write it as:
(Math.random() * 3)
Now lets look at the original expression:
-1 + (int) ((Math.random() * (3)));
In the above (int)
means type-cast. Cast the type of the "following" to the type in the parentheses. In this case, it says
"cast the random number multiplied by 3 to an int
". That will perform a primitive conversion of that value to int
.
Next, the -1
means the number "minus one", and the +
means addition. So the whole thing means
Add -1 to a random number multiplied by 3 and converted to an integer.
What is the conversion actually there for?
Well Math.random()
actually produces a double
value between 0.0
(inclusive) and 1.0
(exclusive). And multiplying that by 3
gives another double
value. So the (int)
cast is converting the double
to an int
.
Which tells us the complete meaning of that expression:
Generate a random integer in the range -1 to +1.
I mentioned the precedence rules. These are the rules that (in effect tell you what the subexpressions are. For example
a * b + c
means
(a * b) + c
because *
has higher precedence than +
.
So in our example we have the following operators:
-
in -1
is a unary minus (negation)+
is binary plus (addition)(int)
is a type castMath.random()
is a method call*
is a binary multiplication( ... )
are parentheses.The order of precedence for these operators is
So you can see that we needed the outside parentheses in (Math.random() * 3)
so that the type cast applies to the product rather than (just) result of the method call.
Here is a table showing the precedence of all operators (and similar) in Java 11: