Operators in Java
Operators in Java
OPERATOR
produce a result.
3. Arithmetic Operators
4. Bitwise Operators
5. Relational Operators
6. Logical Operators
7. Ternary Operators
8. Comma Operators
9. Instanceof Operators
ASSIGNMENT OPERATORS
<variable> = <expression>
ASSIGNING VALUES EXAMPLE
INCREMENT AND DECREMENT OPERATORS
++ AND --
variable by one.
increment operator:
decrement operator: --
INCREMENT AND DECREMENT OPERATORS
++ AND --
Common Shorthand
a = a + 1; a++; or ++a;
a = a - 1; a--; or --a;
EXAMPLE OF ++ AND -- OPERATORS
public class Example
{
public static void main(String[] args)
{
int j, p, q, r, s;
j = 5;
p = ++j; // j = j + 1; p = j;
System.out.println("p = " + p);
q = j++; // q = j; j = j + 1;
System.out.println("q = " + q);
System.out.println("j = " + j);
r = --j; // j = j -1; r = j;
System.out.println("r = " + r);
s = j--; // s = j; j = j - 1;
System.out.println("s = " + s);
}
> java example
}
p = 6
q = 6
j = 7
r = 6
s = 6
>
ARITHMETIC OPERATORS
expressions as in algebra.
long) values.
c = a | b; /* 61 = 0011 1101 */
System.out.println("a | b = " + c );
EXAMPLE CONT.,
c = a ^ b; /* 49 = 0011 0001 */
System.out.println("a ^ b = " + c );
Relational operators are used to test whether two values are equal,
Operator Description
Java has a short hand way by using ?: the ternary aka conditional
Java has an often look past feature within it’s for loop and this is the
comma operator.
Usually when people think about commas in the java language they
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
public class CommaOperator {
public static void main(String[] args) {
for(int i = 1, j = i + 10; i < 5;
i++, j = i * 2) {
System.out.println("i= " + i + " j= " + j);
}
}
} ///:~
INSTANCEOF OPERATORS
This operator is used only for object reference variables. The operator
type).
class Vehicle {}
public class Car extends Vehicle
{
public static void main(String args[])
{
Vehicle a = new Car();
boolean result = a instanceof Car;
System.out.println( result);
}
}
THE END