The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Learning the Java Language
Lesson: Language Basics

Summary of Operators

The following table lists the operators supported by the Java programming language.

All Operators
Operator Use Description
+ +op Promotes op to int if it's a byte, short, or char
- -op Arithmetically negates op
+ op1 + op2 Adds op1 and op2; also used to concatenate strings
- op1 - op2 Subtracts op2 from op1
* op1 * op2 Multiplies op1 by op2
/ op1 / op2 Divides op1 by op2
% op1 % op2 Computes the remainder of dividing op1 by op2
++ op++ Increments op by 1; evaluates to the value of op before it was incremented
++ ++op Increments op by 1; evaluates to the value of op after it was incremented
-- op-- Decrements op by 1; evaluates to the value of op before it was decremented
-- --op Decrements op by 1; evaluates to the value of op after it was decremented
> op1 > op2 Returns true if op1 is greater than op2
>= op1 >= op2 Returns true if op1 is greater than or equal to op2
< op1 < op2 Returns true if op1 is less than op2
<= op1 <= op2 Returns true if op1 is less than or equal to op2
== op1 == op2 Returns true if op1 and op2 are equal
!= op1 != op2 Returns true if op1 and op2 are not equal
&& op1 && op2 Returns true if op1 and op2 are both true; conditionally evaluates op2
|| op1 || op2 Returns true if either op1 or op2 is true; conditionally evaluates op2
! !op Returns true if op is false
& op1 & op2 Returns true if op1 and op2 are both boolean and both true; always evaluates op1 and op2; if both operands are numbers, performs bitwise AND operation
| op1 | op2 Returns true if both op1 and op2 are boolean and either op1 or op2 is true; always evaluates op1 and op2; if both operands are numbers, performs bitwise inclusive OR operation
^ op1 ^ op2 Returns true if op1 and op2 are different — that is, if one or the other of the operands, but not both, is true
<< op1 << op2 Shifts bits of op1 left by distance op2; fills with 0 bits on the right side
>> op1 >> op2 Shifts bits of op1 right by distance op2; fills with highest (sign) bit on the left side
>>> op1 >>> op2 Shifts bits of op1 right by distance op2; fills with 0 bits on the left side
& op1 & op2 Bitwise AND if both operands are numbers;
conditional AND if both operands are boolean
| op1 | op2 Bitwise OR if both operands are numbers;
conditional OR if both operands are boolean
^ op1 ^ op2 Bitwise exclusive OR (XOR)
~ ~op Bitwise complement
= op1 = op2 Assigns the value of op2 to op1
+= op1 += op2 Equivalent to op1 = op1 + op2
-= op1 -= op2 Equivalent to op1 = op1 - op2
*= op1 *= op2 Equivalent to op1 = op1 * op2
/= op1 /= op2 Equivalent to op1 = op1 / op2
%= op1 %= op2 Equivalent to op1 = op1 % op2
&= op1 &= op2 Equivalent to op1 = op1 & op2
|= op1 |= op2 Equivalent to op1 = op1 | op2
^= op1 ^= op2 Equivalent to op1 = op1 ^ op2
<<= op1 <<= op2 Equivalent to op1 = op1 << op2
>>= op1 >>= op2 Equivalent to op1 = op1 >> op2
>>>= op1 >>>= op2 Equivalent to op1 = op1 >>> op2
?: op1 ? op2 : op3 If op1 is true, returns op2; otherwise, returns op3
[] See Creating and Using Arrays (in the Learning the Java Language trail) Used to declare arrays, to create arrays, and to access array elements
. See Using Objects (in the Learning the Java Language trail) Used to form long names
(params) See Defining Methods (in the Learning the Java Language trail) Delimits a comma-separated list of parameters
(type) (type) op Casts (converts) op to the specified type; an exception is thrown if the type of op is incompatible with type
new See Using Objects (in the Learning the Java Language trail) and Creating and Using Arrays (in the Learning the Java Language trail) Creates a new object or array
instanceof op1 instanceof op2 Returns true if op1 is an instance of op2


Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.