Operators and Statements
- 1.Using Operators and Decision Constructs
- Use Java operators; including parentheses to override operator precedence
- Create if and if/else and ternary constructs
- Use a switch statement
- 2.Using Loop Constructs
- Create and use while loops
- Create and use for loops including the enhanced for loop
- Create and use do/while loops
- Compare loop constructs
- Use break and continue
- Java operator is a special symbol that can be applied to a set of variables, values, of literals - referred to as operands and that return a result.
- Types:
- Unary
- Binary
- Ternary
- Operators are not necessarily evaluated from left-to-right order.
- Unless overridden with parentheses, operators follow order of operation by decreasing order of operator precedence. If two operators have the same level of precedence, Java guarantees left-to-right evaluation.
Operator | Symbols and examples |
Other unary operators | +, -, ! |
Multiplication/Division/Modulus | *,/,% |
Addition/Subtraction | +,- |
Shift operators | <<,>>,>>> |
Relational operators | <,>,<=,>=,instanceof |
Equal to/ not equal to | ==, != |
Logical operators | &,^,| |
Short-circuit logical operators | &&,|| |
Ternary operators | boolean expression ? expression1 : expresssion2 |
Assignment operators | =, +=, -=., *=, /=, %=, &=, ^=, !=, <<=, >>=, >>>= |
- Often encountered in early mathematics and include +,-,*,/, %, ++, --.
- All of the arithmetic operators may be applied to any Java primitives, excepts boolean and String. Furthermore, only the addition operators + and += may be applied to String values, which results in String concatenation.String is a Java primitive?
- Numeric Promotion rules:
- 1.If two values have different data types, Java will automatically promote one of the values to the larger of the two data types (ie. int → long, float → double).
- 2.If one of the values is integral and the other is floating-point, Java will automatically promote the integral value to the floating-point value's data type.
- 3.Smaller data types, namely byte, short, and char, are first promoted to in any time they are used with a Java binary arithmetic operator, even if neither of the operands is int.
- 4.After all promotion has occured and the operands have the same data types, the resulting value will have the same data type as its promoted operands.
- Unary operator is one that requires exactly one operand, or variable, to function
Unary operator | Description |
+ | Indicates a number is positive, although numbers are assumed to be positive in Java unless accompanied by a negative unary operator |
- | Indicates a literal number is negative of negates an expression |
++ | Increments a value by 1 |
-- | Decrements a value by 1 |
! | Inverts a Boolean's logical value |
- Logical complement operator, !, flips the value of a boolean expression.
- Negation operator, -, reverses the sign of a numeric expression.
boolean x = false;
System.out.println(x); // false
x = !x;
System.out println(x); // true
double x = 1.21;
System.out.println(x); // 1.21
x = -x;
System.out.println(x); // -1.21
x = -x;
System.out.println(x); // 1.21
- You can not apply a negation operator (-) to a boolean expression, nor can you apply a logical complement operator (!) to a numeric expression
- Increment and decrement operator (++, --), can be applied to numeric operands and have the higher order of precedence.
- Pre-increment of decrement operator is applied first and the value return is the new value of the expression, the post-increment and decrement operators are applied after the value returned.
- Automatically promote from smaller to larger data types, but it will tthrow a compiler exception if it detects you are trying to convert from larger to smaller data types.
- We can cast the results to a smaller data type. Casting primitives is required any time you are going from a larger numerical data type to a smaller data type, or converting from a floating-point number to an integral value.
int x = (int)1.0;
short y = (short) 1921222; // stored as 20678
int z = (int)9l;
long t = 123124124124124L;
- Comples operators are really just glorified forms of the simple assignment operator, with a built-in arithmetic or logical operation that applies the left-and right-hand side of the statement and stores the resulting value in a variable in the left-hand side of the statement.
- The result of the assignment is an expression in and of itself, equal to the value of the assignment.
long x = 5;
long y = (x=3);
System.out.println(x); // Outputs 3
System.out.println(y); // Also, outputs 3
- Compares two expressions and return a boolean value. The first four relational operators are applied to numeric primitive data types only.
| |
< | Strictly less than |
<= | Less than or equal to |
> | Strictly greater than |
>= | Greater than or equal to |
- The fifth relational operator is applied to object references and classes or interfaces.
| |
a instanceof b | True if the reference that a points to is an instance of a class, subclass or class that implements a particular interface, as named in b |
- The logical operators (&,|,^) may be applied to both numeric and boolean data types. When they are applied to boolean data types, they are referred to as logical operators, otherwise they are bitwise operators.
- & AND is only true if both operands are true.
- | Inclusive OR is only false if both operands are false.
- ^ Exclusive OR is only true if the operands are different.
- Short-circuit operators are nearly identical to the logical operators, & and |, respectively, except that the right-hand side of the expression may never be evaluated if the final result can be determined by the left-hand side of the expression.
if (x!= null && x.getValue() < 5){
// do something
} // no exception