| Start | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| The Increment/Decrement Operators (++/--) Variables can be incremented or decremented by 1 using the When the When the If the operator precedes the variable, the value of the expression is the value of the variable after being incremented or decremented. If the operator follows the variable, the value of the expression is the value of the variable prior to being incremented or decremented. Be careful not to overwrite post-increments or decrements. This happens if you use a post-in/decrement operator at the end of an expression which is reassigned to the in/decremented variable itself. The in/decrement will not have an effect. Even though the variable on the left hand side is incremented correctly, its value will be immediately overwritten with the previously evaluated result from the right hand side of the expression: Correct: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The Conditional Operator (? ???? Syntax{condition-to-evaluate} ? {statement-executed-on-true} : {statement-executed-on-false} As shown in the syntax, the Conditional Operator (also known as the Ternary Operator1) uses the Is equivalent to It can be read as “If testCondition is true, set result to value1; otherwise, set result to value2”. For example: Is equivalent to Common UsageYou can use the conditional operator for conditional assignments (like null checking). This example is equivalent to: Since the Conditional Operator has the second-lowest precedence, above the Assignment Operators (opens new window), there is rarely a need for use parenthesis around the condition, but parenthesis is required around the entire Conditional Operator construct when combined with other operators: Conditional operators nesting can also be done in the third part, where it works more like chaining or like a switch statement. Footnote: 1 - Both the Java Language Specification (opens new window) and the Java Tutorial (opens new window) call the ( |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| The Bitwise and Logical Operators (~, &, |, ^) The Java language provides 4 operators that perform bitwise or logical operations on integer or boolean operands.
The logical operations performed by these operators when the operands are booleans can be summarized as follows:
Note that for integer operands, the above table describes what happens for individual bits. The operators actually operate on all 32 or 64 bits of the operand or operands in parallel. Operand types and result types.The usual arithmetic conversions apply when the operands are integers. Common use-cases for the bitwise operators The The The The For more examples of the use of the bitwise operators, see Bit Manipulation (opens new window) |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| The String Concatenation Operator (+) The
In the simple case, the Concatenation operator joins two strings to give a third string. For example: When one of the two operands is not a string, it is converted to a For example: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| The Arithmetic Operators (+, -, *, /, %) The Java language provides 7 operators that perform arithmetic on integer and floating point values.
1. This is often incorrectly referred to as the "modulus" operator. "Remainder" is the term that is used by the JLS. "Modulus" and "remainder" are not the same thing. Operand and result types, and numeric promotionThe operators require numeric operands and produce numeric results. The operand types can be any primitive numeric type (i.e. The result type is determined base on the types of the operand or operands, as follows: The result type of the operation determines how the arithmetic operation is performed, and how the operands are handled Promotion is performed in two stages: The meaning of divisionThe / operator divides the left-hand operand Java integer division rounds towards zero. The JLS Section 15.17.2 (opens new window) specifies the behavior of Java integer division as follows: The quotient produced for operands There are a couple of special cases: Java floating point division has more edge cases to consider. However the basic idea is that the result Floating point division will never result in an exception. Instead, operations that divide by zero result in an INF and NaN values; see below. The meaning of remainderUnlike C and C++, the remainder operator in Java works with both integer and floating point operations. For integer cases, the result of It follows from the above definition that Floating point remainder operation is a generalization of the integer case. The result of Floating point remainder can produce Important note: The result of a floating-point remainder operation as computed by Integer OverflowJava 32 and 64 bit integer values are signed and use twos-complement binary representation. For example, the range of numbers representable as (32 bit) When you add, subtract or multiple two N bit integers (N == 32 or 64), the result of the operation may be too large to represent as an N bit integer. In this case, the operation leads to integer overflow, and the result can be computed as follows: It should be noted that integer overflow does not result in exceptions under any circumstances. Floating point INF and NAN valuesJava uses IEE 754 floating point representations for The INF values are produced by floating operations that cause overflow, or by division by zero. The NaN values are produced by dividing zero by zero, or computing zero remainder zero. Surprisingly, it is possible perform arithmetic using INF and NaN operands without triggering exceptions. For example: For full details, please refer to the relevant subsections of JLS 15 (opens new window). Note that this is largely "academic". For typical calculations, an |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| The Instanceof Operator This operator checks whether the object is of a particular class/interface type. instanceof operator is written as: Example: This would produce the following result: This operator will still return true if the object being compared is the assignment compatible with the type on the right. Example: This would produce the following result: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| The Assignment Operators (=, +=, -=, *=, /=, %=, <<=, >>= , >>>=, &=, |= and ^=) The left hand operand for these operators must be a either a non-final variable or an element of an array. The right hand operand must be assignment compatible with the left hand operand. This means that either the types must be the same, or the right operand type must be convertible to the left operands type by a combination of boxing, unboxing or widening. (For complete details refer to JLS 5.2 (opens new window).) The precise meaning of the "operation and assign" operators is specified by JLS 15.26.2 (opens new window) as: A compound assignment expression of the form Note that there is an implicit type-cast before the final assignment. 1. The simple assignment operator: assigns the value of the right hand operand to the left hand operand. Example: 2. The "add and assign" operator: adds the value of right hand operand to the value of the left hand operand and assigns the result to left hand operand. If the left hand operand has type Example: 3. The "subtract and assign" operator: subtracts the value of the right operand from the value of the left hand operand and assign the result to left hand operand. Example: 4. The "multiply and assign" operator: multiplies the value of the right hand operand by the value of the left hand operand and assign the result to left hand operand. . Example: 5. The "divide and assign" operator: divides the value of the right hand operand by the value of the left hand operand and assign the result to left hand operand. Example: 6. The "modulus and assign" operator: calculates the modulus of the value of the right hand operand by the value of the left hand operand and assign the result to left hand operand. Example: 7. The "left shift and assign" operator. Example: 8. The "arithmetic right shift and assign" operator. Example: 9. The "logical right shift and assign" operator. Example: 10. The "bitwise and and assign" operator. Example: 11. The "bitwise or and assign" operator. Example: 12. The "bitwise exclusive or and assign" operator. Example: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| The conditional-and and conditional-or Operators ( && and || ) Java provides a conditional-and and a conditional-or operator, that both take one or two operands of type `||` - the conditional-OR operators.
The evaluation of ` The evaluation of As the pseudo-code above illustrates, the behavior of the short-circuit operators are equivalent to using Example - using && as a guard in an expressionThe following example shows the most common usage pattern for the The first version works in most cases, but if the In the second version we have added a "guard" test. The Example - using && to avoid a costly calculationThe following example shows how In the first version, both operands of the |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| The Shift Operators (<<, >> and >>>) The Java language provides three operator for performing bitwise shifting on 32 and 64 bit integer values. These are all binary operators with the first operand being the value to be shifted, and the second operand saying how far to shift. Notes: The following table will help you see the effects of the three shift operators. (The numbers have been expressed in binary notation to aid vizualization.)
There examples of the user of shift operators in Bit manipulation (opens new window) |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| The Equality Operators (==, !=) The These operators can be used operands with primitive and reference types, but the behavior is significantly different. According to the JLS, there are actually three distinct sets of these operators:
However, in all cases, the result type of the The Numeric |
| A | B | A == B | A != B |
|---|---|---|---|
| false | false | true | false |
| false | true | false | true |
| true | false | false | true |
| true | true | true | false |
There are two "pitfalls" that make it advisable to use == and != sparingly with truth values:
== and != operatorsIf both operands are object references, the == and != operators test if the two operands refer to the same object. This often not what you want. To test if two objects are equal by value, the .equals() method should be used instead.
String s1 = "We are equal";
String s2 = new String("We are equal");
s1.equals(s2); // true
// WARNING - don't use == or != with String values
s1 == s2; // false
Warning: using == and != to compare String values is incorrect in most cases; see http://stackoverflow.com/documentation/java/4388/java-pitfalls/16290/using-to-compare-strings (opens new window) . A similar problem applies to primitive wrapper types; see http://stackoverflow.com/documentation/java/4388/java-pitfalls/8996/using-to-compare-primitive-wrappers-objects-such-as-integer (opens new window) .
JLS 15.21.1 (opens new window) states the following:
If either operand is NaN, then the result of == is false but the result of != is true. Indeed, the test x != x is true if and only if the value of x is NaN.
This behavior is (to most programmers) unexpected. If you test if a NaN value is equal to itself, the answer is "No it isn't!". In other words, == is not reflexive for NaN values.
However, this is not a Java "oddity", this behavior is specified in the IEEE 754 floating-point standards, and you will find that it is implemented by most modern programming languages. (For more information, see http://stackoverflow.com/a/1573715/139985 (opens new window) ... noting that this is written by someone who was "in the room when the decisions were made"!)
From Java 8 onwards, the Lambda operator ( -> ) is the operator used to introduce a Lambda Expression. There are two common syntaxes, as illustrated by these examples:
a -> a + 1 // a lambda that adds one to its argument
a -> { return a + 1; } // an equivalent lambda using a block.
A lambda expression defines an anonymous function, or more correctly an instance of an anonymous class that implements a functional interface.
(This example is included here for completeness. Refer to the Lambda Expressions (opens new window) topic for the full treatment.)
The operators <, <=, > and >= are binary operators for comparing numeric types. The meaning of the operators is as you would expect. For example, if a and b are declared as any of byte, short, char, int, long, float, double or the corresponding boxed types:
- `a < b` tests if the value of `a` is less than the value of `b`.
- `a <= b` tests if the value of `a` is less than or equal to the value of `b`.
- `a > b` tests if the value of `a` is greater than the value of `b`.
- `a >= b` tests if the value of `a` is greater than or equal to the value of `b`.
The result type for these operators is boolean in all cases.
Relational operators can be used to compare numbers with different types. For example:
int i = 1;
long l = 2;
if (i < l) {
System.out.println("i is smaller");
}
Relational operators can be used when either or both numbers are instances of boxed numeric types. For example:
Integer i = 1; // 1 is autoboxed to an Integer
Integer j = 2; // 2 is autoboxed to an Integer
if (i < j) {
System.out.println("i is smaller");
}
The precise behavior is summarized as follows:
byte, short or char, it is promoted to an int.int, long, float or double values.You need to be careful with relational comparisons that involve floating point numbers:
Finally, Java does bit support the use of relational operators with any types other than the ones listed above. For example, you cannot use these operators to compare strings, arrays of numbers, and so on.
An operator is a symbol (or symbols) that tells a Java program to perform an operation on one, two or three operands. An operator and its operands form an expression (see the Expressions topic). The operands of an operator are themselves expressions.
This topic describes the 40 or so distinct operators defined by Java. The separate Expressions topic explains:
Read Article
https://develop.horje.com/learn/1434/reference