简体   繁体   中英

What does -1+ (int) mean?

我有一行 java 代码,但我不知道如何解释它是如何工作的,有人知道 -1+ (int) 是什么意思

-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 . 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:

  • call the Math.random() method
  • multiply the result by 3.

Note 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, cast the random number multiplied by 3 to an int .

And the -1 means the number "minus one". So the whole thing means

Add -1 to a random number multiplied by 3 and converted to an integer.

What is the conversion 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:

  • The - in -1 is a unary minus (negation)
  • The + is binary plus (addition)
  • The (int) is a type cast
  • The Math.random() is a method call
  • The * is a binary multiplication
  • The ( ... ) are parentheses.

The order of precedence for these operators is

  • parentheses highest
  • method call
  • negation
  • type cast
  • multiplication
  • addition

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM