Rule: Binary Operators Require Operands of the Same Type.
Operands of standard mathematical operators must have the same type, even if Java must automatically promote one operand type to match the other operand type.
jshell> 9 + 5; // Addition $1 ==> 14 | created scratch variable $1 : int jshell> 9 - 5; // Subtraction $2 ==> 4 | created scratch variable $2 : int jshell> 9 * 5; // Multiplication $3 ==> 45 | created scratch variable $3 : int jshell> 9 / 5; // Integer division $4 ==> 1 | created scratch variable $4 : int jshell> 9 % 5; // Modulus (remainder of division) $5 ==> 4 | created scratch variable $5 : int jshell>
Operator | Type | Description |
---|---|---|
+ |
binary, infix | addition |
- |
binary, infix | subtraction |
- |
unary, prefix | negation |
* |
binary, infix | multiplication |
/ |
binary, infix | division |
% |
binary, infix | modulus |
-
in Java is negation, which is used to compute the additive inverse of a numeric value or variable. In other words, it turns a positive value negative, and a negative value positive.int
, long
, float
, and double
, and implies the following.jshell> int x = 3, y = 2; // Declare and init two ints x ==> 3 | created variable x : int y ==> 2 | created variable y : int jshell> x / y; // Divide ints $3 ==> 1 // Is this a mistake? | created scratch variable $3 : int jshell>
Math
and a dot, for example Math.pow(2,3)
evaluates 8
. Technically, when using this notation we are accessing static methods of the Math
class. The following table lists many of the common mathematical functions implemented by Math
. These may be included in any Java expression and will be invoked during expression evaluation.Math
class static methodsMethod/Constant | Description |
---|---|
Math.PI |
The mathematical constant |
Math.E |
Eulerβs mathematical constant |
Math.pow(x, y) |
Raise x to the power of e and return result |
Math.abs(x) |
Return the absolute value of x. |
Math.sqrt(x) |
Compute the square root of x. |
Math.min(x, y) |
Return the minimum of x and y. |
Math.max(x, y) |
Return the maximum of x and y. |
Math.ceil(x) |
Return the next integer greater than x . |
Math.floor(x) |
Return the next integer below than x . |
Math.round(x) |
Round x to the nearest integer. |
Math.exp(x) |
Raise Eulerβs number ( |
Math.log(x) |
Compute the natural logarithm of x ( |
Math.log10(x) |
Compute the base-10 logarithm of x ( |
Math.sin(x) |
Compute the sine function of x. |
Math.cos(x) |
Compute the cosine function of x. |
Math.tan(x) |
Compute the tangent function of x. |
Math.sinh(x) |
Compute the hyperbolic sine function of x. |
Math.cosh(x) |
Compute the hyperbolic cosine function of x. |
Math.tanh(x) |
Compute the hyperbolic tangent function of x. |
Math.asin(x) |
Compute the inverse (arc) sine function of x. |
Math.acos(x) |
Compute the inverse (arc) cosine function of x. |
Math.atan(x) |
Compute the inverse (arc) tangent function of x. |
Math.atan2(x, y) |
An alternative inverse tangent function taking rectilinear coordinates x,y and returning the angle |
jshell> double a = 3, b = 4; a ==> 3.0 | created variable a : double b ==> 4.0 | created variable b : double jshell> double c = Math.sqrt( Math.pow(a,2) + Math.pow(b,2) ); c ==> 5.0 | created variable c : double jshell>
()
(4)*
(2)/
(2)+
(1)-
(1)*
operator takes precedence over +
, the multiplication is performed before the addition. Surrounding the first subexpression with parentheses causes the subexpression to be evaluated first due to the higher precedence of parentheses relative to *
.jshell> 2 + 3 * 4; $1 ==> 14 | created scratch variable $1 : int jshell> (2 + 3) * 4; $2 ==> 20 | created scratch variable $2 : int jshell>
1 / 3
. Why did you get the answer you did?int
so the /
operator is integer division. The resulting value is one-third (~0.333β¦), but the fractional part is dropped, so the result is the int 0.14 % 12
. Where did that answer come from?%
) computes the remainder after integer division./
and %
?jshell> int nbagels = 100; nbagels ==> 100 | created variable nbagels : int jshell> int bdozens = nbagels / 13; bdozens ==> 7 | created variable bdozens : int jshell> int nsingle = nbagels % 13; nsingle ==> 9 | created variable nsingle : int jshell> "Charge for " + bdozens + " baker's dozens and " + nsingle + " single bagels" $4 ==> "Charge for 7 baker's dozens and 9 single bagels" | created scratch variable $4 : String
jshell> Math.sqrt(5*5 + 12*12); $1 ==> 13.0 | created scratch variable $1 : double