|
|
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.
Operator Use Description +op1 + op2Adds op1andop2; also used to concatenate strings-op1 - op2Subtracts op2fromop1*op1 * op2Multiplies op1byop2/op1 / op2Divides op1byop2%op1 % op2Computes the remainder of dividing op1byop2These short cut operators increment or decrement a number by one.
Operator Use Description ++op++Increments opby 1; evaluates to the value ofopbefore it was incremented++++opIncrements opby 1; evaluates to the value ofopafter it was incremented--op--Decrements opby 1; evaluates to the value ofopbefore it was decremented----opDecrements opby 1; evaluates to the value ofopafter it was decrementedHere are the Java programming language's other arithmetic operators.
Operator Use Description ++opPromotes optointif it's abyte,short, orchar--opArithmetically negates op
Use these relational operators to determine the relationship between two values.
Operator Use Description >op1 > op2Returns trueifop1is greater thanop2>=op1 >= op2Returns trueifop1is greater than or equal toop2<op1 < op2Returns trueifop1is less thanop2<=op1 <= op2Returns trueifop1is less than or equal toop2==op1 == op2Returns trueifop1andop2are equal!=op1 != op2Returns trueifop1andop2are not equalYou can use the following conditional operators to form multi-part decisions.
Operator Use Description &&op1 && op2Returns trueifop1andop2are bothtrue; conditionally evaluatesop2||op1 || op2Returns trueif eitherop1orop2istrue; conditionally evaluatesop2!!opReturns trueifopisfalse&op1 & op2Returns trueifop1andop2are both boolean and bothtrue; always evaluatesop1andop2; if both operands are numbers, performs bitwiseANDoperation|op1 | op2Returns trueif bothop1andop2are boolean and eitherop1orop2istrue; always evaluatesop1andop2; if both operands are numbers, performs bitwise inclusiveORoperation^op1 ^ op2Returns trueifop1andop2are 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.
Operator Use Description <<op1 << op2Shift bits of op1left by distanceop2; fills with zero bits on the right-hand side>>op1 >> op2Shift bits of op1right by distanceop2; fills with highest (sign) bit on the left-hand side>>>op1 >>> op2Shift bits of op1right by distanceop2; fills with zero bits on the left-hand sideThese operators perform logical functions on their operands.
Operator Use Operation &op1 & op2Bitwise ANDif both operands are numbers;
conditionalANDif both operands are boolean|op1 | op2Bitwise ORif both operands are numbers;
conditionalORif both operands are boolean^op1 ^ op2Bitwise exclusive OR(XOR)~~op2Bitwise complement
The basic assignment operator looks as follows and assigns the value ofop2toop1.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.
Operator Use Equivalent to +=op1 += op2op1 = op1 + op2-=op1 -= op2op1 = op1 - op2*=op1 *= op2op1 = op1 * op2/=op1 /= op2op1 = op1 / op2%=op1 %= op2op1 = op1 % op2&=op1 &= op2op1 = op1 & op2|=op1 |= op2op1 = op1 | op2^=op1 ^= op2op1 = op1 ^ op2<<=op1 <<= op2op1 = op1 << op2>>=op1 >>= op2op1 = op1 >> op2>>>=op1 >>>= op2op1 = op1 >>> op2
The Java programming language also supports these operators.
Operator Use Description ?:op1 ? op2 : op3If op1is true, returnsop2. Otherwise, returnsop3.[]type []Declares an array of unknown length, which contains type elements. []type[ op1 ]Creates and array with op1elements. Must be used with thenewoperator.[]op1[ op2 ]Accesses the element at op2index within the arrayop1. Indices begin at 0 and extend through the length of the array minus one..op1.op2Is a reference to the op2member ofop1.()op1(params)Declares or calls the method named op1with the specified parameters. The list of parameters can be an empty list. The list is comma-separated.(type)(type) op1Casts (converts) op1 to type. An exception will be thrown if the type ofop1is incompatible withtype.newnew op1Creates a new object or array. op1is either a call to a constructor, or an array specification.instanceofop1 instanceof op2Returns true if op1is an instance ofop2
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.