Java Tutorial

What is Java


Control Statements


Java Object Class


Java Inheritance


Java Polymorphism


Java Abstraction


Java Encapsulation


Java Array

Java OOPs Misc


Operators in java

Operator in java is a symbol that is used to perform operations. For example: +, -, *, / etc.

There are many types of operators in java which are given below:

  1. Unary Operator,
  2. Arithmetic Operator,
  3. shift Operator,
  4. Relational Operator,
  5. Bitwise Operator,
  6. Logical Operator,
  7. Ternary Operator and
  8. Assignment Operator.

Java Operator Precedence

Operator Type Category Precedence
Unary postfix
Prefix
expr++ expr--
++expr --expr +expr -expr ~ !
Arithmetic Multiplication
additive
* / %
+ -
Shift Shift << >> >>>
Relational comparison
equality
< > <= >= instanceof
== !=
Bitwise bitwise AND
bitwise exclusive OR
bitwise inclusive OR
&
^
|
Logical logical AND
logical OR
&&
||
Ternary ternary ? :
Assignment assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=

Java Increment(++) and Decrement (--) Example:


package com.ashu;
public class Java64bit {
public static void main(String args[]){
int x=5;
System.out.println(x++); // without Increment print x=5
System.out.println(+x); // x = 6
System.out.println(++x); // x = 7
System.out.println(x--); // without decrement print x =7
System.out.println(--x); // with decrement print x =6
}
}


output: 5 6 7 7 6

Java Increment(++) and Decrement (--) Example:


package com.ashu;
public class Java64bit {
int x =10;
int y=++x;
// int z=++10; we can apply increment and decrement oprator only for variables but not for constant values.
public static void main(String[] args) {
Java64bit t= new Java64bit();
System.out.println(t.y);
}
}


output: 11

Java Bitwise complement operators(~)


package com.ashu;
public class Java64bit {
int x=5;
int y=-10;
boolean z=true;
boolean z1=false;
public static void main(String args[]){
Java64bit t =new Java64bit();
System.out.println(~t.x); // 5 is positive then start from 0 to negative values.
System.out.println(~t.y); // -10 is negative then start from 0 to positive values.
System.out.println(!t.z);
System.out.println(!t.z1);
}
}


output: -6 9 false true

Java Arithmetic Operator Example


package com.ashu;
public class Java64bit {
int a=5;
int b=5;
public static void main(String args[]){
Java64bit t = new Java64bit();
System.out.println(t.a+t.b);
System.out.println(t.a-t.b);
System.out.println(t.a*t.b);
System.out.println(t.a/t.b);
System.out.println(t.a%t.b);
System.out.println(13*12/6+4-2*5/5);
}
}


output: 10 0 25 1 0 28

Java Relational Operator Ex.

We can apply relational Operators for every primity type expect to boolean.


package com.ashu;
public class Java64bit {
public static void main(String[] args) {
System.out.println(5>10);
System.out.println('x'>10);
System.out.println('x'<98.7);
System.out.println('x'>'X');
// System.out.println(true>false); can not apply boolean to boolean.
}
}


output: false, true, false, true

Java Relational Operator Ex.

package com.ashu;
public class Java64bit {
public static void main(String[] args) {
System.out.println("javabit123">"javabit"); // we can not apply relational operators for object types.
}
}


output : error

1. SOP(15<20<25); = true<25; can not be applied to boolean , int.

Java Shift Operator Ex.:

cal -> 10<<5=10*2(bit)^5=10*32=320 -Left Shift

cal ->20>>3=20/2(bit)^3=20/8=2 -Right Shift


package com.ashu;
class Java64bit{
public static void main(String args[]){
System.out.println(10<<5); // Left shift
System.out.println(10<<4);
System.out.println(20<<3);
System.out.println(10>>5); //Right shift
System.out.println(20>>4);
System.out.println(30>>3);
}
}


output: 320 ,160,160,0,1,3

Java Operator Ex:

package com.ashu;
class Java64bit{
public static void main(String[] args) {
int x =5;
int y=6;
System.out.println(++x + x++);
System.out.println(x++ + ++x );
System.out.println(++y + ++y);
System.out.println(y++ + y++);
}
}


output:12,16,15,17

Java Shift Operator Ex: >> vs >>>

For positive >> & >>> , its same works.

For negative >>> changes bit (MSB) to 0.


package com.ashu;
class Java64bit{
public static void main(String[] args) {
System.out.println(10>>>2);
System.out.println(-2>>>8);
System.out.println(-10>>1);
}
}


output:2 , 16777215 , -5

AND Operator Example: Logical && and Bitwise & in java

The logical && operator doesn't check second condition if first condition is false. It checks second condition only if first one is true.
The bitwise & operator always checks both conditions whether first condition is true or false.


package com.ashu;
class Java64bit{
public static void main(String args[]){
int a=14,b=6,c=18;
System.out.println(a < b&&a < c); //false && true = false
System.out.println(a < c&b < c); //true & true = true
}
}


output: false,true

AND Operator Example: Logical && vs Bitwise & in java


package com.ashu;
class Java64bit{
public static void main(String args[]){
int a=14,b=6,c=18;
System.out.println(a < b&&a++ < c); //false && true = false
System.out.println(a);              // didn't use in condition
System.out.println(a < b&a+ < c); //false && true = false
System.out.println(a);
}
}


output: false,14,false,15

OR Operator Example: Logical || vs Bitwise | in java

The logical || operator doesn't check second condition if first condition is true. It checks second condition only if first one is false.
The bitwise | operator always checks both conditions whether first condition is true or false.


package com.ashu;
class UnaryOperator{
public static void main(String args[]){
int x=14,y=6,z=18;
System.out.println(x > y || x < z);    //true || true = true
System.out.println(x > y | x < z);     //true | true = true
System.out.println(x > y || x++ < z);  //true || true = true
System.out.println(y > x || z++ < x);  //false || false = false
System.out.println(x);   // didn't use in condition.
System.out.println(x > y | z++ < y);  //true | false = true
System.out.println(x);
}
}


output:true,true,true,false,14,true,14

Ternary Operator in java Example:

The ternary operator is an operator that takes three arguments.The first operand in java ternary operator should be a boolean or a statement with boolean result. If the first operand is true then java ternary operator returns second operand else it returns third operand.
Syntax: result = testCondition ? value1 : value2;


package com.ashu;
public class Java64bit {
public static void main(String args[]){
     int x=14,y=6,min, max;
   min=(x    max=(x>y)?x:y;
   System.out.println(min);
   System.out.println(max);
}
}


output: 6,14

Ternary Operator in java Example:


public class Java64bit {
public static void main(String[] args) {
String s = "India";
String data = s.contains("I") ? " S is containt 'I'" : "S doesn't contains 'I'";
System.out.println(data);
System.out.println(getMinValue(6,18));
System.out.println(getAbsoluteValue(-18));
System.out.println(invertBoolean(true));
   int i = 18;
switch (i){
case 4:
     System.out.println("i=4");
     break;
case 11:
    System.out.println("i=11");
    break;
default:
    System.out.println("i is not equal to 4 or 11");
}
System.out.println((i==4) ? "i=4":((i==11) ? "i=11":"i is not equal to 4 or 11"));
}
private static boolean invertBoolean(boolean b) {
     return b ? false:true;
}
private static int getAbsoluteValue(int i) {
     return i < 0 ? -i : i;
}
private static int getMinValue(int i, int j) {
     return (i < j) ? i : j;
}
}


output:S is containt 'I' , 6 ,18 ,false ,i is not equal to 4 or 11, i is not equal to 4 or 11

Java Assignment Operator Example

It is used to assign the value on its right to the operand on its left.


package com.ashu;
public class Java64bit {
public static void main(String args[]){
int a=6;
int b=14;
a+=4;
b-=4;
System.out.println(a);
System.out.println(b);
}
}


output: 10,10

Java Assignment Operator Example: Adding short


package com.ashu;
class Java64bit{
public static void main(String[] args){
int a=10;
a+=3;
System.out.println(a);
a-=4;
System.out.println(a);
a*=2;
System.out.println(a);
a/=2;
System.out.println(a);
}
}


output: 13,9,18,9

Java Assignment Operator Example: Adding short after type cast


package com.ashu;
class Java64bit{
public static void main(String args[]){
short a=13;
short b=10;
a=(short)(a-b);
System.out.println(a);
}
}


output: 3

Copyright 2017 Design& Development