|
|
Start of Tutorial > Start of Trail > Start of Lesson | Search |
The following table lists the basic arithmetic operators provided by the Java programming language. These operators can be used only on numeric values.
Operator Use Description +op1 + op2Adds op1andop2-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 of op before it was incremented++++opIncrements opby 1; evaluates to the value of op after it was incremented--op--Decrements opby 1; evaluates to the value of op before it was decremented----opDecrements opby 1; evaluates to the value of op after 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 Returns trueif>op1 > op2op1is greater thanop2>=op1 >= op2op1is greater than or equal toop2<op1 < op2op1is less thanop2<=op1 <= op2op1is less than or equal toop2==op1 == op2op1andop2are equal!=op1 != op2op1andop2are not equalYou can use the following conditional operators to form multi-part decisions.
Operator Use Returns trueif&&op1 && op2op1andop2are bothtrue, conditionally evaluatesop2||op1 || op2either op1orop2istrue, conditionally evaluatesop2!! opopisfalse&op1 & op2op1andop2are bothtrue, always evaluatesop1andop2|op1 | op2either op1orop2istrue, always evaluatesop1andop2^op1 ^ op2if op1 and op2 are different--that is if one or the other of the operands is true but not both
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 Operation >>op1 >> op2shift bits of op1right by distanceop2<<op1 << op2shift bits of op1left by distanceop2>>>op1 >>> op2shift bits of op1right by distanceop2(unsigned)These operators perform logical functions on their operands.
Operator Use Operation &op1 & op2bitwise and|op1 | op2bitwise or^op1 ^ op2bitwise 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 form of the?:operator is shown below. This operator returnsop2ifop1is true or returnsop3ifop1is false.op1 ? op2 : op3
|
|
Start of Tutorial > Start of Trail > Start of Lesson | Search |