JAVATM MASTER

Operators

Back to Table of Contents

Here is a list of the operators of the JavaTM programming language. The Precedence indicates which operator will be acted upon first if two operators are in the same scope. To make a certain operator acted on before another, surround it with this operator () as in 4*(5+3) will evaluate to 32 while 4*5+3 will evaluate to 60. When two operators are in the same scope the operator with the least precedence will be done first. If the two operators have the same precedence, then the operator to act on first is chosen by Associativity (left to right or right to left).

The JVM ignores white space before and after operators so you can have an expression on multiple lines as in the following example:

String A = "This is how to "+
	"make a long sentence in the java language. ";

Operator Precedence Associativity Description
( ) 1 Left to Right Parentheses: Makes what is between the parantheses acted upon before any outside operators are acted upon. Do not mistake this operator for the Casting operator described below.
[ ] 1 Left to Right Used with arrays. An array can be declared in the following ways:
int[] myintA = new int[]{1,2,3};

int[] myintB = new int[3];

int myintC[] = new int[3];

When accessing an element in an array you can put an expression inbetween the [] as in myint[a-b] but the expression must evaluate to an integer or be an integer (int, short, long, byte, or char). Also the integer must be positive or zero.
. 1 Left to Right Called "dot", this operator allows access to a classes methods and class variables. The useage is to put the Object to the left of the dot operator and the method or variable to the right of the dot operator.
String MyString = "Hi There.";

String MyUpper = MyString.toUpperCase(); //method toUpperCase of String

//Now MyUpper is "HI THERE." Note: do not confuse the dot operator with the period in a String, which uses the same character.
++ 2 Right to Left Unary post and preincrement. Used only on integers (int, short, long, byte, or char) making the integer increase by one. If the operator occurs before the variable, the variable is increased by one before its value is used in the expression. If the operator occurs after the variable, the initial value is used and then the variable is increased by one.
-- 2 Right to Left Unary post and predecrement. Used only on integers (int, short, long, byte, or char) making the integer decrease by one. If the operator occurs before the variable, the variable is decreased by one before its value is used in the expression. If the operator occurs after the variable, the initial value is used and then the variable is decreased by one.
+ 2 Right to Left Unary Plus: : Indicates Positive value for a literal number to its right. Do not confuse this operator with the the Addition Operator described below or the String Concatenation operator described below. By default a literal number is positive.
- 2 Right to Left Unary Minus: : Indicates a negative value for a literal number to its right and reverses the sign of the number in a variable to its right. Do not confuse this operator with the the Subtraction Operator described below
~ 2 Right to Left One's complement: Evaluates the number to the right to a new number in which all the bits are reversed (1 becomes 0 and 0 becomes 1) and if the number is a signed number, its sign bit is also reversed. The only unsigned number in the Java language is char.
! 2 Right to Left Boolean Complement. Reverses the boolean value of the expression to its right.
( ) 2 Right to Left Casting of Types: converts an Object from one of its possible types to another. Possible types include all its interfaces, all its parent classes, and all the interfaces of all its parent classes. The Object to cast is placed on the right of the operator and the class or interface to cast it to is placed inbetween the two parentheses of the casting operator. Do not mistake this operator for the Parentheses operator described above.
* 3 Left to Right Multiplication: Multiplies the numbers on either side of this operator. If both numbers are integers it evaluates to an integer.
/ 3 Left to Right Division: Divides the number on the left of this operator by the number on the right and returns the result. If both numbers are integers it evaluates to an integer, ignoring the remainder (see modulo)
% 3 Left to Right Modulo: Evalutes to the remainder of integer division where the integer on the left of this operator is divided by the integer on the right.
+ 4 Left to Right Addition: Evalutates to the sum of the two numbers on either side of this operator. The expressions on both sides must evalute to a number (int, short, long, byte, char, float, or double). If the two numbers are both integers the result will be an integer, otherwise it will be a floating point number (float or double). Do not confuse this operator with the Unary Plus operator described above or with the String Concatenation operator described below.
+ 4 Left to Right String Concatenation: Concats One String to another. The Object on the left must be a String, but the expression on the right can be any object or primitive type. If the expression to the right is an Object the String added will be the derived from its toString method. Do not confuse this operator with the Addition operator described above or with the Unary Plus operator described above.
- 4 Left to Right Subtraction: Evalutates to the difference of the two numbers on either side of this operator by subtracting the value on the right from the value on the left. The expressions on both sides must evalute to a number (int, short, long, byte, char, float, or double). If the two numbers are both integers the result will be an integer, otherwise it will be a floating point number (float or double). Do not confuse this operator with the Unary Minus operator described above.
<< 5 Left to Right Bitwise Shift Left: shift bits in the number on the left of this operator a number of times to the left determined by the integer on the right of this operator. The bit to the far right is replaced by zero.
>> 5 Left to Right Bitwise Shift Right: shift bits in the number on the left of this operator a number of times to the right determined by the integer on the right of this operator. The bit to the far left is replaced by zero.
>>> 5 Left to Right Bitwise Shift Right without Sign Extension: shift bits in the number on the left of this operator a number of times to the right determined by the integer on the right of this operator. The bit to the far right representing sign is not shifted and the second bit to the right of the sign bit is replaced by zero.
< 6 Left to Right Less Than: Evalutes to boolean: true if the number value on the left is less than the value on the right.
> 6 Left to Right Greater Than: Evalutes to boolean: true if the number value on the left is greater than the value on the right.
<= 6 Left to Right Less Than or Equal To: Evalutes to boolean: true if the number value on the left is less than or equal to the value on the right.
>= 6 Left to Right Greater Than or Equal To: Evalutes to boolean: true if the number value on the left is greater than or equal to the value on the right.
instanceof 6 Left to Right The only keyword operator, Evaluates to boolean: true if the Object on the left can be cast to the class or interface on the right, essentially whether the Object on the left is an instance of the class or interface on the right.
== 7 Left to Right Equals: Evaluates to boolean: for primitive types, true if the values on either side are equal. For Objects, true if the two Objects are the same instance. example: if (a == b) ...; To compare two different instaces for holding the same value, like two Strings, use the equals method. example: if (MyString.equals(YourString)) doSomething();
!= 7 Left to Right Not Equal: Evaluates to boolean: for primitive types, true if the values on either side are not equal. For Objects, true if the two Objects are not the same instance. example: if (a != b) ...; To compare two different instaces for holding the same value, like two Strings, use the equals method with a ! before it. example: if (! MyString.equals(YourString)) doSomething();
& 8 Left to Right Boolean AND and Bitwise AND: Evaluates the bits of the two numbers on either side to make a new number in which each bit will be on only if both of the corresponding bits of both numbers is also on. Returns boolean if both expressions are boolean and a number if both expressions are numbers.
^ 9 Left to Right Boolean XOR and Bitwise XOR: Evaluates the bits of the two numbers on either side to make a new number in which each bit will be on only if the two corresponding bits of both numbers are different. Returns boolean if both expressions are boolean and a number if both expressions are numbers.
| 10 Left to Right Boolean OR and Bitwise OR: Evaluates the bits of the two numbers on either side to make a new number in which each bit will be on only if at least one of two the corresponding bits of both numbers is on. Returns boolean if both expressions are boolean and a number if both expressions are numbers.
&& 11 Left to Right Conditional AND: Evaluates to boolean: true if both boolean expressions on either side of this operator are true, otherwise it evaluates to false.
|| 12 Left to Right Conditional OR: Evaluates to boolean: true if one or both of the boolean expressions on either side of this operator are true, otherwise it evaluates to false.
? : 13 Right to Left Conditional Ternary Operator: Takes three values. If the boolean expression on the left of the question mark (?) is true, it evaluates to the expression between the question mark and the colon (:). Otherwise, it evaluates to the expression after the colon. Example:
public String NoNull(String S){
   return S == null ? "" : S;
}
= 14 Right to Left Assignment: Assigns the value on the right to the variable on the left.
*= 14 Right to Left Assignment: Perfoms the Multiplication operation between the values on both sides and then assigns it to the variable on the left.
/= 14 Right to Left Assignment: Perfoms the Division operation between the values on both sides and then assigns it to the variable on the left.
%= 14 Right to Left Assignment: Perfoms the Modulo operation between the values on both sides and then assigns it to the variable on the left.
+= 14 Right to Left Assignment: Perfoms the Addition operation between the values on both sides and then assigns it to the variable on the left.
-= 14 Right to Left Assignment: Perfoms the Subtraction operation between the values on both sides and then assigns it to the variable on the left.
<<= 14 Right to Left Assignment: Perfoms the Bitwise Shift Left operation between the values on both sides and then assigns it to the variable on the left.
>>= 14 Right to Left Assignment: Perfoms the Bitwise Shift Right operation between the values on both sides and then assigns it to the variable on the left.
>>>= 14 Right to Left Assignment: Perfoms the Bitwise Shift Right without Sign Extension operation between the values on both sides and then assigns it to the variable on the left.
&= 14 Right to Left Assignment: Perfoms the Bitwise or Boolean AND operation between the values on both sides and then assigns it to the variable on the left.
^= 14 Right to Left Assignment: Perfoms the Bitwise or Boolean XOR operation between the values on both sides and then assigns it to the variable on the left.
~= 14 Right to Left Assignment: Perfoms the One's Complement on the value on the right and then assigns it to the variable on the left.

Other Characters Used in the Java Language:

Symbol Description
{ Open Curly Brackets: Indicates the start of a code blocks such as for a class, a method, a loop, or other block of code. Also used to initialize a array:
int[] myarray = {1,2,3};
} Close Curly Brackets: Indicates the end of a code blocks such as for a class, a method, a loop, or other block of code. Also used to initialize a array:
int[] myarray = {1,2,3};
( Open Parenthesis: When not used as an operator, this is used to indicate the start of the parameter holder of both a method declaration and a method call.
) Close Parenthesis: When not used as an operator, this is used to indicate the end of the parameter holder of both a method declaration and a method call.
, Comma: Used to separate values in a list, as in separating the parameters in a method declaration or a method call, separating the intefaces in an implementation clause in a class declaration, separating the initializaions in a single declaration of multiple variables of the same type, separating the multiple incrementations in a for loop declaration
(example: for (int i=0, j=0; i < 10 && j < 20; ++i, j+=2){},

and separating the values in a array declaration.
: Colon: Used in the Ternary Operator and also in the Switch Statement using the switch keyword and also in loop labels: example:
MYLOOPA: while (true){ 
   mynum++; 
   if (mynum >= mylimit) 
   break MYLOOPA; 
}
Also, since java version 5.0, they can be used in for loops that iterate a collection:
List<String> myList = new ArrayList<String>();
myList.add("Java");
myList.add("is");
myList.add("cool!");
for (String val : myList){
  System.out.print(val+" ");
}
System.out.println();
; Semi Colon: All Statements must end with a semicolon. The shortest statement possible is a single semicolon on its own. Semicolon is also used in for loops to separate the initialization from the loop condition and the the loop condition from the increment section.
Example: for (int i=0; i<10; ++i){ ; }

*OracleTM and JavaTM are registered trademarks of Oracle and or its affiliates. Other names may be trademarks of their respective owners.*