Skip navigation

Lu's Notes

down to bottom of page

Java Operators

Warning: These notes have been collected from several different sources - each uses different terminology... they may even be contradictory...

The left operand must be a variable, as: x = 5, not: 5 = x

Boolean Expressions (often make comparisons between numbers)

OperatorMeaning
A = B   value B is assigned to A
A = = B
(two equal signs in a row)
  is A equal to B ?
A < B   is A less than B ?
A <= B   is A less than or equal to B ?
A > B   is A Greater than B ?
A >= B   is A Greater than or equal to B ?
A != B   is A not equal to B ?
! ( A < B )   not (A less than B), becomes A greater than B
Table of Relational Operators


OperatorMeaning
A = = B
(two equal signs in a row)
  is String A exactly equal to String B ?
  are they the same object, do they have the same identity?
A.equals(B)   compares the values held in each object
Table of String comparisons


OperatorMeaning
+   addition
-   subtraction
*   multiplication
/   division
%   modulus (the remainder from a division)
  10 % 3 is 1,  and  10 % 2 is 0
=   means only assignment (not equals)
+ +
(two plus signs in a row)
  increments, adds one to a variable
++counter   means increment before using
counter++   means increment after using
- -
(two minus signs in a row)
  decrements, subtracts one from a variable
--counter   use the value, then subtract 1
counter--   subtract 1, then use the value
=   means assignment (not equals)
+=   addition with assignment, ( increment sum by 5 )
  sum += 5;   is the same as   sum = sum + 5;
-=   subtraction with assignment, ( decrement sum by 5 )
  sum -= 5;   is the same as   sum = sum - 5;
*=   multiplication with assignment, ( multiply sum by 5 )
  sum *= 5;   is the same as   sum = sum * 5;
/=   division with assignment, ( divide sum by 5 )
  sum /= 5;   is the same as   sum = sum / 5;
%=   modulus with assignment, ( modulate sum by 5 )
  sum %= 5;   is the same as   sum = sum % 5;
  sum/5 has a modulus(remainder) of whatever, then sum becomes the whatever
Table of Other Operators


ExpressionResultEvaluation Order
true && true   true   both operands evaluated
false && true   false   only first operand evaluated
true && false   false   both operands evaluated
false && false   false   only first operand evaluated
true & true   true   both operands evaluated
false & true   false   both operands evaluated
true & false   false   both operands evaluated
false & false   false   both operands evaluated
true || true   true   only first operand evaluated
false || true   true   both operands evaluated
true || false  true   only first operand evaluated
false || false  false  both operands evaluated
true | true   true   both operands evaluated
false | true   true   both operands evaluated
true | false   true   both operands evaluated
false | false   false  both operands evaluated
Table of And/Or Operators


OperatorMeaning
A ? B : C
  makes no changes to any of the three operands,
  it is like a simple if statement:
      if A is true, then B, else C
The Conditional Operator

See Java Links for more info, Object & Classes , Data Types , Operators , Conditionals , Definitions , Keywords , File Structure , Code Examples , Class Skeleton , Java Notes

up to top of page   Return to Top of Page   up to top of page up to top of page