Quick Index
Overview


Recall from algebra:

a + b
50 - 30
2 * z


Where +, -, * are operators.
And a, b, 50, 30, 2 and z are operands.

Simple Assignment Operator


OperatorMeaning
=Simple assignment operator


Arithmetic Operators


OperatorMeaning
+Additive operator (also used for String concatenation)
-Subtraction operator
*Multiplication operator
/Division operator
%Remainder operator


The Equality and Relational Operators


OperatorMeaning
==equal to
!=not equal to
>greater than
>=greater than or equal to
<less than
<=less than or equal to


The Conditional or Logical Operators


OperatorMeaning
&&Conditional-AND
||Conditional-OR


Increment And Decrement Operators (++ and --)


Increment Operator Examples


The "++" operator is shorthand. These are equivalent:

z = z + 1;
z++;


Example
Output:
Before: 0
After: 1
		int z = 0;
		prn("Before: " + z);
		z++;
		prn("After: " + z);


Post vs Pre Options


Post foo++
This is probably the more common way to use the increment operator
foo++
Pre ++foo
but we can also use the pre-operator
++foo
Increment Operator Gotcha
Caution: As the output shows below, we go through the iteration fine with "index++" but cause a bounds exception for "++index". Can you see why? Copy the code into your lab and give it a play.

Output:
Printing using post incrment operator
Name: A
Name: B
Printing using pre incrment operator
Name: B
Exception in thread "main"
ArrayIndexOutOfBoundsException:
		String[] names = {"A", "B"};
		int index;

		prn("Printing using post incrment operator");
		index = 0;
		while (index < names.length)
			prn("Name: " + names[index++]);

		prn("Printing using pre incrment operator");
		index = 0;
		while (index < names.length)
			prn("Name: " + names[++index]);


Decrement Operator (--)


The "--" operator is shorthand. These are equivalent:

z = z - 1;
z--;


The examples and pre/post discussion for the "++" operator apply to the "--" operator as well.

Special Operators


OperatorNameDefinitionExample
?:ternary operatorif condition ? value for true case : value for false casemax = (a < b) ? a : b;