Expressions: Types and Operators
EXPRESSIONS: TYPES AND OPERATORS
Every expression in a Java program has a type. In Java, there are two kinds of types: primitive types and objects. Examples of primitive types include:
int double char Boolean
Objects are instances of classes, which are basically encapsulated data and directions. Strings are a special kind of object with their own set of tools.
Arithmetic Operators:
Addition +
Subtraction -
Multiplication *
Division /
Modulus (remainder) %
All of the arithmetic operators can be applied to expressions of type int or double. The addition operator can also be used to perform string concatenation (joining). If at least one of its operands is a String, then the result is the concatenation of that String with the String representation of the other operand. For example:
Expression Value of the Expression
“book” + “worm” “bookworm”
“version” + 3 “version3”
.5 + “baked” “.5baked”
Integer division (when both the numerator and the denominator are integers) results in truncation (cutting off), not rounding. For example, 2/3 is zero, not one; -2/3 is also zero, not minus one. If you want to round a double variable x to the nearest integer (instead of truncating it), you can use
(int)(x+.5)
when x is positive, and
(int)(x-.5)
when x is negative.
Casting can be used to convert an int to a double (or vice versa). For example:
Expression Value of the Expression
(int)3.6 3
(double)3 3.0
(double)2/3 .667
(int)2.0/3 0
Assignment Operators
Assignment means to give a value to something. The assignment operators are:
Plain assignment =
Add-then-assign +=
Subtract-then-assign -=
Multiply-then-assign *=
Divide-then-assign /=
Modulus-then-assign %=
The types of the left- and right-hand sides of an assignment must be compatible, and the left-hand side must be a memory location. Variables, for example, are memory locations.
The last five assignment operators listed above are called compound assignments; a compound assignment takes the form:
a operator= b
And it is equivalent to
a = a operator b
For example:
Compound Assignment Equivalent Noncompound Assignment
a += 2 a = a + 2
a -=b a = a – b
a *= 5.5 a = a *5.5
Assignments are expressions, not statements; the value of an assignment expression is the value of its right-hand side. This means that assignments can be “chained”. For example, the following is perfectly legal:
int j, k, n;
j = k = n = 0; // all three variables are set to zero
Increment/decrement operators
increment ++
decrement –
The increment operator adds one to its operand; the decrement operator subtracts one from its operand. For example:
Using increment/decrement operator Equivalent assignment expression
a++ a += 1 or a = a + 1
a-- a -= 1 or a = a - 1
Equality, Relational, and Logical operators
Equal to ==
Not equal to !=
Less than <
Less than or equal to <=
Greater than >
Greater than or equal to >=
Logical NOT !
Logical AND &&
Logical OR ||
The equality and relational operators must be applied to expressions with compatible types. The logical operators must be applied to expressions with type boolean. An expression involving equality, relational, or logical operators evaluates to either true or false (so the type of the whole expression is Boolean). Expressions involving the logical AND and OR operators are guaranteed to be evaluated from left to right, and the evaluation stops as soon as the final value is known. This is called short-circuit evaluation. For example, when the expression
(5 > 0) || (3 < 2)
is evaluated, the expression (3<2) is not evaluated. The subexpression
(5 > 0)
Is evaluated first, and it evaluates to true. Since logical OR applied to true and any other expression always evaluates to true, there is no need to evaluate the other expression. Similarly, the logical AND applied to false and any other expression always evaluates to false, the expression (3 < 2) is not evaluated in this case:
(5 < 0) && (3<2)
Every expression in a Java program has a type. In Java, there are two kinds of types: primitive types and objects. Examples of primitive types include:
int double char Boolean
Objects are instances of classes, which are basically encapsulated data and directions. Strings are a special kind of object with their own set of tools.
Arithmetic Operators:
Addition +
Subtraction -
Multiplication *
Division /
Modulus (remainder) %
All of the arithmetic operators can be applied to expressions of type int or double. The addition operator can also be used to perform string concatenation (joining). If at least one of its operands is a String, then the result is the concatenation of that String with the String representation of the other operand. For example:
Expression Value of the Expression
“book” + “worm” “bookworm”
“version” + 3 “version3”
.5 + “baked” “.5baked”
Integer division (when both the numerator and the denominator are integers) results in truncation (cutting off), not rounding. For example, 2/3 is zero, not one; -2/3 is also zero, not minus one. If you want to round a double variable x to the nearest integer (instead of truncating it), you can use
(int)(x+.5)
when x is positive, and
(int)(x-.5)
when x is negative.
Casting can be used to convert an int to a double (or vice versa). For example:
Expression Value of the Expression
(int)3.6 3
(double)3 3.0
(double)2/3 .667
(int)2.0/3 0
Assignment Operators
Assignment means to give a value to something. The assignment operators are:
Plain assignment =
Add-then-assign +=
Subtract-then-assign -=
Multiply-then-assign *=
Divide-then-assign /=
Modulus-then-assign %=
The types of the left- and right-hand sides of an assignment must be compatible, and the left-hand side must be a memory location. Variables, for example, are memory locations.
The last five assignment operators listed above are called compound assignments; a compound assignment takes the form:
a operator= b
And it is equivalent to
a = a operator b
For example:
Compound Assignment Equivalent Noncompound Assignment
a += 2 a = a + 2
a -=b a = a – b
a *= 5.5 a = a *5.5
Assignments are expressions, not statements; the value of an assignment expression is the value of its right-hand side. This means that assignments can be “chained”. For example, the following is perfectly legal:
int j, k, n;
j = k = n = 0; // all three variables are set to zero
Increment/decrement operators
increment ++
decrement –
The increment operator adds one to its operand; the decrement operator subtracts one from its operand. For example:
Using increment/decrement operator Equivalent assignment expression
a++ a += 1 or a = a + 1
a-- a -= 1 or a = a - 1
Equality, Relational, and Logical operators
Equal to ==
Not equal to !=
Less than <
Less than or equal to <=
Greater than >
Greater than or equal to >=
Logical NOT !
Logical AND &&
Logical OR ||
The equality and relational operators must be applied to expressions with compatible types. The logical operators must be applied to expressions with type boolean. An expression involving equality, relational, or logical operators evaluates to either true or false (so the type of the whole expression is Boolean). Expressions involving the logical AND and OR operators are guaranteed to be evaluated from left to right, and the evaluation stops as soon as the final value is known. This is called short-circuit evaluation. For example, when the expression
(5 > 0) || (3 < 2)
is evaluated, the expression (3<2) is not evaluated. The subexpression
(5 > 0)
Is evaluated first, and it evaluates to true. Since logical OR applied to true and any other expression always evaluates to true, there is no need to evaluate the other expression. Similarly, the logical AND applied to false and any other expression always evaluates to false, the expression (3 < 2) is not evaluated in this case:
(5 < 0) && (3<2)