Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
The following table lists the basic arithmetic operators provided by the Java programming language. Except for+
, which is also used to concatenate strings, these operators can be used only on numeric values.
Binary Arithmetic Operators Operator Use Description +
op1 + op2
Adds op1
andop2
; also used to concatenate strings-
op1 - op2
Subtracts op2
fromop1
*
op1 * op2
Multiplies op1
byop2
/
op1 / op2
Divides op1
byop2
%
op1 % op2
Computes the remainder of dividing op1
byop2
These short cut operators increment or decrement a number by one.
Shortcut Arithmetic Operators Operator Use Description ++
op++
Increments op
by 1; evaluates to the value ofop
before it was incremented++
++op
Increments op
by 1; evaluates to the value ofop
after it was incremented--
op--
Decrements op
by 1; evaluates to the value ofop
before it was decremented--
--op
Decrements op
by 1; evaluates to the value ofop
after it was decrementedHere are the Java programming language's other arithmetic operators.
Unary Arithmetic Operators Operator Use Description +
+op
Promotes op
toint
if it's abyte
,short
, orchar
-
-op
Arithmetically negates op
Use these relational operators to determine the relationship between two values.
Relational Operators Operator Use Description >
op1 > op2
Returns true
ifop1
is greater thanop2
>=
op1 >= op2
Returns true
ifop1
is greater than or equal toop2
<
op1 < op2
Returns true
ifop1
is less thanop2
<=
op1 <= op2
Returns true
ifop1
is less than or equal toop2
==
op1 == op2
Returns true
ifop1
andop2
are equal!=
op1 != op2
Returns true
ifop1
andop2
are not equalYou can use the following conditional operators to form multi-part decisions.
Conditional Operators Operator Use Description &&
op1 && op2
Returns true
ifop1
andop2
are bothtrue
; conditionally evaluatesop2
||
op1 || op2
Returns true
if eitherop1
orop2
istrue
; conditionally evaluatesop2
!
!op
Returns true
ifop
isfalse
&
op1 & op2
Returns true
ifop1
andop2
are both boolean and bothtrue
; always evaluatesop1
andop2
; if both operands are numbers, performs bitwiseAND
operation|
op1 | op2
Returns true
if bothop1
andop2
are boolean and eitherop1
orop2
istrue
; always evaluatesop1
andop2
; if both operands are numbers, performs bitwise inclusiveOR
operation^
op1 ^ op2
Returns true
ifop1
andop2
are different that is, if one or the other of the operands, but not both, istrue
Each shift operator shifts the bits of the left-hand operand over by the number of positions indicated by the right-hand operand. The shift occurs in the direction indicated by the operator itself.
Shift Operators Operator Use Description <<
op1 << op2
Shift bits of op1
left by distanceop2
; fills with zero bits on the right-hand side>>
op1 >> op2
Shift bits of op1
right by distanceop2
; fills with highest (sign) bit on the left-hand side>>>
op1 >>> op2
Shift bits of op1
right by distanceop2
; fills with zero bits on the left-hand sideThese operators perform logical functions on their operands.
Logical Operators Operator Use Operation &
op1 & op2
Bitwise AND
if both operands are numbers;
conditionalAND
if both operands are boolean|
op1 | op2
Bitwise OR
if both operands are numbers;
conditionalOR
if both operands are boolean^
op1 ^ op2
Bitwise exclusive OR
(XOR
)~
~op2
Bitwise complement
The basic assignment operator looks as follows and assigns the value ofop2
toop1
.op1 = op2;In addition to the basic assignment operation, the Java programming language defines these short cut assigment operators that perform an operation and an assignment using one operator.
Shortcut Assignment Operators Operator Use Equivalent to +=
op1 += op2
op1 = op1 + op2
-=
op1 -= op2
op1 = op1 - op2
*=
op1 *= op2
op1 = op1 * op2
/=
op1 /= op2
op1 = op1 / op2
%=
op1 %= op2
op1 = op1 % op2
&=
op1 &= op2
op1 = op1 & op2
|=
op1 |= op2
op1 = op1 | op2
^=
op1 ^= op2
op1 = op1 ^ op2
<<=
op1 <<= op2
op1 = op1 << op2
>>=
op1 >>= op2
op1 = op1 >> op2
>>>=
op1 >>>= op2
op1 = op1 >>> op2
The Java programming language also supports these operators.
Other Operators Operator Use Description ?:
op1 ? op2 : op3
If op1
is true, returnsop2
. Otherwise, returnsop3
.[]
type []
Declares an array of unknown length, which contains type elements. []
type[ op1 ]
Creates and array with op1
elements. Must be used with thenew
operator.[]
op1[ op2 ]
Accesses the element at op2
index within the arrayop1
. Indices begin at 0 and extend through the length of the array minus one..
op1.op2
Is a reference to the op2
member ofop1
.()
op1(params)
Declares or calls the method named op1
with the specified parameters. The list of parameters can be an empty list. The list is comma-separated.(type)
(type) op1
Casts (converts) op1 to type
. An exception will be thrown if the type ofop1
is incompatible withtype
.new
new op1
Creates a new object or array. op1
is either a call to a constructor, or an array specification.instanceof
op1 instanceof op2
Returns true if op1
is an instance ofop2
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.