operators

 

            bitwise shift operators

            conditional operators

            conditional (ternary) operator

            logical operators

            modulus (remainder) operator

            prefix and postfix operators

            relational operators

            shortcut operators

            short circuit operators

            unary complement operator

 

See also assignments

 

 

bitwise shift operators

 

Operator

Usage

Operation Performed

>>

op1 >> op2

Shift the bits of op1 to the right by the distance of  op2

<<

op1 << op2

Shift the bits of op1 to the left by the distance of op2

>>>

op1 >>> op2

Shift bits of op1 to the right by the distance of op2 (unsigned)  

 

  You cannot use bit operators on Strings, objects, or Wrappers. Bit operators work only on expressions with integer types byte, short, int, long, and char.  Plus some also apply to boolean. i.e.

 

( true | false ) returns false

 

  The operator for the number of bits to be shifted can be any of the integer primitive types, including long and char. 

 

  The integer operand is always promoted to int before the shifting.

 

   >> right shift, always propogates the sign bit (meaning it fills in the left side with whatever the sign was.) 

Thus when you are using  >> negative numbers always stay negative and positive numbers always stay positive.

 

   >>>  right shift zero fill always fills the left side with zeroes. So negative numbers will become positive.

 

   <<    left shift zero fill  always fills the right side with zeroes.  Numbers can become variously positive or negative depending on whatever bit lands in the sign bit position.

 

■ Note that there  is no  <<<  operator.

 

  Also legal are  &=,  |=,  ^=,  <<=,  >>=, and  >>>=  which combine assignments.

 

  If you try to shift more than the number of bits in the variable's type (i.e. int >> 33) the actual number of bits shifted is: the requested length of the shift modulo the type's length.  i.e.

 

int >> 35 would shift just 3 bits, because 35 % 32 is 3.

int >> 32  wouldn't shift any, because 32 % 32 is 0.

 

Note that byte >> 10 or (byte) byte >> 10 will both actually shift the 10 bits even though the byte type has only 8, because the operand is promoted to int first.

 

 

conditional operators

 

Operator

Name

Usage

Returns true if

&&

Conditional AND

op1 && op2

op1 and op2 are both true. Conditionally evaluates op2 if necessary.

||

Conditional OR

op1 || op2

Either op1 or op2 is true. Conditionally evaluates op2 if necessary.

 

 

conditional (ternary) operator

 

  This is the only Java operator taking three operands.

 

  Its form is:  a = b > c ? d : e;  It means: If the first operand ( here b > c) is boolean true, then assign the second ( d ). Else assign the third ( e ).

 

  The variables b and c must either match a’s type or be automatically promotable to it. i.e. (assuming all Strings here)

 

greeting = (name == null) ? "Hello" : "Hi " + name;

 

 

logical operators

 

Operator

Name

Usage

Returns true if

!

Negation

! op1

op1 is false.

&

AND

op1 & op2

Both op1 and op2 are true.  Always evaluates both op1 and op2.

|

OR

op1 | op2

Either op1 or op2 is true.  Always evaluates both op1 and op2.

^

Exclusive OR

op1 ^ op2

op1 and op2 are different. 

Meaning one or the other of the operands is true, but not both.  

 

 

  &  AND   Here both bits must be on to make a 1.   &= is legal.

 

  |   OR   Here both bits or either bit can be on to make a 1.   |= is legal. 

 

  ^  EXCLUSIVE OR (XOR)   Here only either bit can be on to make a 1, not both.   ^= is legal.

 

  You can apply logical operators to boolean primitives. When doing so, true always acts like a one and false like a zero.  i.e. 

 

( true & false ) and ( false & false )    both return false because both aren't on.

 

  AND ( & ), OR ( | ), and XOR ( ^ ) also work with double-boolean expressions. You get a compile error if there are not two boolean operands present. 

 

 

  For AND ( & ), OR ( | ), and XOR ( ^ ), both tests are always conducted, no matter what the outcome of the first test..

 

  Legal are  &=,  |=,  and  ^=.   

 

 

modulus (remainder) operator

 

  % is called modulo, also known as modulus.

 

  The result of modulo always takes the sign of the left operand, regardless of the sign of the quotient or right operand.  i.e. 

 

int x = -10;   int y = -3; or int y = 3;    In either case x % y equals –1.

 

So a % b  is always negative only if a is negative. i.e. No matter what b is.

 

  For the int type, a % b  throws an ArithmeticException if b is zero.   i.e. It uses the same exception-throwing rule as for dividing by zero in all integer types.

 

  For floating points, a % b  returns a NaN if b is zero. 

Note that the integer dividing-by-zero rule doesn’t apply to floating point types.  Floating point division by zero always returns Infinity.

 

 

prefix and postfix operators

 

  Post-assignment operators ++ and - - are fulfilled after the operator’s part of the overall expression, not at the end of their statement. i.e.

 

int x = 1;  
int y = 2; 
System.out.println( x + y++  +  y);  This will bump y immediately after adding y to x, so the bump will occur before the second use of y for the addition.  6 is printed here.
 
System.out.println( x++ );   Prints the old (existing) value of x, and then x is bumped prior to the start of the next statement.
 
int x = 1; 
int y = x++;   Will bump x after adding it to y, so y becomes 1 here.
 

x = x++;    This does not ever bump x at all, because the ++ bump occurs after the assignment across the equals sign, and there is no more to the statement.

 

 

relational operators

 

Operator

Usage

Returns true if

>

op1 > op2

op1 is greater than op2

>=

op1 >= op2

op1 is greater than or equal to op2

<

op1 < op2

op1 is less than op2

<=

op1 <= op2

op1 is less than or equal to op2

==

op1 == op2

op1 and op2 are equal

!=

op1 != op2

op1 and op2 are not equal

 

 

shortcut operators
 

  There are 12 shortcut assignment operators:

 

Operator

Is equivalent to doing this:

+=

x = x + y

- =

x = x - y

*=

x = x * y

/=

x = x / y

%=

x = x % y

&=

x = x & y

|=

x = x | y

^=

x = x ^ y

<<=

x = x << y

>>=

x = x >> y

>>>=

x = x >>> y

? :

x ? y : z

 

  There is no <<<= shortcut assignment operator because there is no <<< operator in Java.

 

 

short circuit operators

 

  The && and || conditional (short circuit) operators don’t do the second test if it won’t affect the results of the first one. 

i.e. The example below uses short circuit operators to ensure having a non-null String in hand before actually testing it:

 

if (myString != null && myString.equalsIgnoreCase(“value”) ) {do something}

 

  There’s no short circuit for XOR.  i.e. no ^^ exists

 

  You can only use boolean tests in short circuit operators. 

 

  There are no such things as “short circuit equals” operators like  &&=,  ||=,  or  ^^=. 

 

 

unary complement operator

 

  The  ~   (that's a tilde) unary complement operator flips all bits in the variable to which it is applied.

Its single operand is converted up to int if it is shorter than int.  i.e.

 

byte b1 = 0;

System.out.print( ~  b1 );

byte b2 = b1;                   Prints -1 and b2 becomes -1.

 

byte b1 = 0;

byte b2 = (byte) ~  b1;        Here b2 again becomes -1 or 1111 1111.

 

  The unary complement of a positive integer is always the negative of the number, lowered one more.  Meaning  ~  x == (-x) -1   i.e.

 

~  21 = - 22              Take 21, consider it negative as -21, and then lower it one more to -22.

 

  The unary complement of a negative integer is always the positive of the number, lowered one more.  i.e.

 

~  -21 = 20           Take -21, consider it to be positive 21, then lower it one more to 20.

 

  Note that ~= is illegal.  There's no such shorthand operator.  i.e.

 

y ~ = x;               will not compile.