#### Java Basic Operators

 Previous Chapter Next Chapter

Note
Here I am using,
OS : Linux (Ubuntu 12.04)
IDE : Eclipse Tool
Eclipse : Juno (Version 4.2.0)
Package : Default package

A. Arithmetic Operators

Operator Description Example
a = 10, b = 2, c = 0
+ Addition c = a + b
c = 10 + 2
c = 12
- Subtraction c = a - b
c = 10 - 2
c = 8
* Multiplication c = a * b
c = 10 * 2
c = 20
/ Division c = a / b
c = 10 / 2
c = 5
% Modulus c = a % b
c = 10 % 2
c = 0
++ Increment c = ++a
c = 10 + 1
c = 11
-- Decrement c = --a
c = 11 - 1
c = 10

Operator_Arithmetic.java

```public class Operator_Arithmetic {

int a;
int b;
int c;

Operator_Arithmetic() {

a = 10;
b = 2;
}

c = a + b;
System.out.println("The Addition is : " + c);
}

void sub() {

c = a - b;
System.out.println("The Subtraction is : " + c);
}

void multi() {

c = a * b;
System.out.println("The Multiplication is : " + c);
}

void div() {

c = a / b;
System.out.println("The Division is : " + c);
}

void mod() {

c = a % b;
System.out.println("The Modulus is : " + c);
}

void inc() {

c = ++a;
System.out.println("The Increment is : " + c);
}

void dec() {

c = --a;
System.out.println("The Decrement is : " + c);
}
}

class MainClass {

public static void main (String args[]) {

Operator_Arithmetic obj = new Operator_Arithmetic();

obj.sub();
obj.multi();
obj.div();
obj.mod();
obj.inc();
obj.dec();
}
}
```

Sample Output

```The Addition is : 12
The Subtraction is : 8
The Multiplication is : 20
The Division is : 5
The Modulus is : 0
The Increment is : 11
The Decrement is : 10
```

B. Relational Operators

Operator Description Example
== Check Equal a == 5
5 == 5
!= Check Not Equal a != 5
6 != 5
> Check Greaterthan a > 5
10 > 5
< Check Lessthan a < 5
3 < 5
>= Check Greaterthan or Equal a >= 5
6 >= 5
<= Check Lessthan or Equal a <= 5
4 <= 5

Operator_Relation.java

```public class Operator_Relation {

int a = 10;

void relation() {

if(a==10) {

System.out.println("Equal Operator : Verified");
}

if(a!=20) {

System.out.println("Not Equal Operator : Verified");
}

if(a>5) {

System.out.println("Greaterthan Operator : Verified");
}

if(a<15) {

System.out.println("Lessthan Operator : Verified");
}

if(a>=9) {

System.out.println("Greaterthan or Equal Operator : Verified");
}

if(a<=11) {

System.out.println("Lessthan or Equal Operator : Verified");
}
}
}

class MainClass {

public static void main (String args[]) {

Operator_Relation obj = new Operator_Relation();

obj.relation();
}
}
```

Sample Output

```Equal Operator : Verified
Not Equal Operator : Verified
Greaterthan Operator : Verified
Lessthan Operator : Verified
Greaterthan or Equal Operator : Verified
Lessthan or Equal Operator : Verified
```

C. Bitwise Operators

Operator Description Example
~ Unary bitwise complement ~ a
& Bitwise AND a & b
| Bitwise inclusive OR a | b
^ Bitwise exclusive OR a ^ b
<< Signed left shift a << b
>> Signed right sift a >> b
>>> Unsigned right shift a >>> b

Operator_Bitwise.java

```public class Operator_Bitwise {

public static void main(String args[]) {

int a = 60;
int b = 30;
int c;

System.out.println("Bitwise Operators");

c = ~a;
System.out.println("\n~ a :" + c);

c = a & b;
System.out.println("\na & b : " + c);

c = a | b;
System.out.println("a | b : " + c);

c = a ^ b;
System.out.println("a ^ b : " + c);

c = a << 2;
System.out.println("\na << 2 : " + c);

c = a >> 2;
System.out.println("a >> 2 : " + c);

c = a >>> 2;
System.out.println("\na >>> 2 : " + c);
}
}
```

Sample Output

```Bitwise Operators

~ a :-61

a & b : 28
a | b : 62
a ^ b : 34

a << 2 : 240
a >> 2 : 15

a >>> 2 : 15
```

D. Logical Operators

Operator Description Example
&& Logical AND Operator a && b
|| Logical OR Operator a || b
! Logical NOT Operator !(a && b)

Operator_Logical.java

```public class Operator_Logical {

public static void main(String args[]) {

boolean a = true;
boolean b = false;

boolean c;

System.out.println("Logical Operators");

c = a && b;
System.out.println("\na && b :" + c);

c = a || b;
System.out.println("a || b : " + c);

c = !(a && b);
System.out.println("\n!(a && b) : " + c);

c = !(a || b);
System.out.println("!(a || b) : " + c);
}
}
```

Sample Output

```Logical Operators

a && b :false
a || b : true

!(a && b) : true
!(a || b) : false
```

E. Assignment Operators

Operator Example Equivalent Expression
= z = x + y z = (x + y)
+= x += y x = (x + y)
-= x -= y x = (x - y)
*= x *= y x = (x * y)
/= x /= y x = (x / y)
%= x %= y x = (x % y)
<<= x <<= y x = (x << y)
>>= x >>= y x = (x >> y)
&= x &= y x = (x & y)
|= x |= y x = (x | y)
^= x ^= y x = x^y

Operator_Assignment.java

```public class Operator_Assignment {

public static void main(String args[]) {

int x = 10;
int y = 20;

System.out.println("Assignment Operators");

x += y;
System.out.println("\nThe addition is : " + x);

x -= y;
System.out.println("The subtraction is : " + x);

x *= y;
System.out.println("The multiplication is : " + x);

x /= y;
System.out.println("The division is : " + x);

x %= y;
System.out.println("The remainder is : " + x);

x &= y;
System.out.println("The result of AND operation : " + x);

x |= y;
System.out.println("The result of Bitwise inclusive OR operation : " + x);

x <<= y;
System.out.println("The result of Signed left shift operation : " + x);
}
}
```

Sample Output

```Assignment Operators

The addition is : 30
The subtraction is : 10
The multiplication is : 200
The division is : 10
The remainder is : 10
The result of AND operation : 0
The result of Bitwise inclusive OR operation : 20
The result of Signed left shift operation : 20971520
```

F. Conditional Operator

Operator_Conditional.java

```public class Operator_Conditional {

public static void main(String args[]) {

int a = 10 , b;

System.out.println("Conditional Operators");

b = (a == 1) ? 100 : 200;
System.out.println( "\nValue of b is : " +  b );

b = (a == 10) ? 100 : 200;
System.out.println( "Value of b is : " + b );
}
}
```

Sample Output

```Conditional Operators

Value of b is : 200
Value of b is : 100
```

 Previous Chapter Next Chapter  