Operators in java

Operators in java:


java,java operators,operators in java,java tutorial,java (programming language),learn java,operators,operator,java programming,15 - operators in java,shift operators in java,unary operators in java,java logical operators,bitwise operator in java,bitwise operators,java relational operators,operators in java telugu part 1,operators and assignments in java,java operator precedence,arithmetic operators

Java provides different types of operators to perform operation in data types.
  • Arithmetic operators
  • Unary operators
  • Assignment operators
  • Relational operators
  • Shift operators
  • Bitwise operators
  • Ternary operator
  • Logical operators


Arithmetic operators:

This operator is used perform some mathematical operations like addition, subtraction, multiplication, division, modules.
Arithmetic operators
Example:

public class  Example
{
public static void main(String[] args)
{
int a = 20, b = 10,add,sub,multi,div,mod;
add = a+b;
sub = a-b;
multi = a*b;
div = a/b;
mod = a%b;
System.out.println("Addition: " + add);
System.out.println("Subtraction: " + sub);
System.out.println("Multiplication: " + multi);
System.out.println("Division: " + div);
System.out.println("Modulus: " + mod);
}
}

Output:

Addition: 30
Subtraction: 10
Multiplication: 200
Division: 2
Modulus: 0



Unary operators:

This operator is used to increment or decrement value by one (one unit value), negating the expression and inverting the boolean value.

  • + Unary plus: This is used to assign the values by positive. By default, all values are positive.
  • - Unary minus: This is used to assign the values by negative.
  • ! Logical NOT operator is used to invert the boolean value.
  • ++ Unary increment operator: This is used to increment the values by one. There are two types of expression:
§ Pre Increment:
This is used to increment the value first and then compute the result.
§ Post increment: 
This is used to compute the value first and the increment the values.
  • -- Unary decrement operator: This is used to decrement the values by one. There are two types of expression:
§  Pre decrement:This is used to decrement the value first and then compute the result.


§  Post decrement:
This is used to compute the value first and the decrement the values.


Example:

public class  Example
{
public static void main(String[] args)
{
int a = 10, b = -20, c = 30;
boolean bl = true;
System.out.println("Unary plus: " + a);
System.out.println("Unary minus: " + b);
System.out.println("=========== Post Increment ==========");
// compute the value for result. where c is same as 30
System.out.println("Post increment before: " + c++);
// incremented now the value increase by one. Now c = 31
System.out.println("Post increment after: " + c);
// compute the value and the c = 31
System.out.println("Post decrement before: " + c--);
// decrement now the value decrease by one. Now c = 30
System.out.println("Post decrement after: " + c);
System.out.println("======================");
System.out.println("=========== Pre Increment ==========");
// increase the value by one now the value of c is 31
System.out.println("Pre increment before: " + ++c);
// result c = 31
System.out.println("Pre increment after: " + c);
// increase the value by one now the value of c is 30
System.out.println("Pre decrement before: " + --c);
// result c = 30
System.out.println("Pre decrement after: " + c);
System.out.println("======================");
System.out.println("inversion operator/Not operator: " + !bl);
}
}

Output:

Unary plus: 10
Unary minus: -20
=========== Post Increment ==========
Post increment before: 30
Post increment after: 31
Post decrement before: 31
Post decrement after: 30
======================
=========== Pre Increment ==========
Pre increment before: 31
Pre increment after: 31
Pre decrement before: 30
Pre decrement after: 30
======================
inversion operator/Not operator: false



Assigment operators:
This operator is used to assign the value to the variable depending on its data types.

Example:
int a = 5;

In some cases, this assignment operators can be combined with other operator to form shorten statement.In other words, it is compound two operators to shorten the statement. Hence, it is also known as compound operator

Example:
inr a+=5;  // which a = a+5;

Some  of the assignment operators are shown in the table below:

Assignment operators

Example:
public class Example
{
public static void main(String[] args)
{
System.out.println("Assignment operator");
int a = 4, b = 5, s1 = -4, s2 = 4;
System.out.print("Addition assignment operator: ");
System.out.println(a+=b); // now a=9
System.out.print("Subtraction assignment operator: ");
System.out.println(a-=b); // now a=4
System.out.print("Multiplication assignment operator: ");
System.out.println(a*=b); // now a=20
System.out.print("Division assignment operator: ");
System.out.println(a/=b); // now a=4
System.out.print("Modulus assignment operator: ");
System.out.println(a%=b); // now a=4
System.out.print("Bitwise AND assignment operator: ");
System.out.println(a&=b); // now a=4;
System.out.print("Bitwise OR assignment operator: ");
System.out.println(a|=b); // now a=5
System.out.print("Bitwise X-OR assignment operator: ");
System.out.println(a^=b); // now a=0
System.out.print("Right shift assignment operator: ");
System.out.println(s1>>=2); // s1= -1
System.out.print("Left shift assignment operator: ");
System.out.println(s1<<=2); //s1 = -4
System.out.print("Right shift unsigned assignment operator: ");
System.out.println(s1>>>=2); //s1 = 073741823
}

}

Output:
Assignment operator
Addition assignment operator: 9
Subtraction assignment operator: 4
Multiplication assignment operator: 20
Division assignment operator: 4
Modulus assignment operator: 4
Bitwise AND assignment operator: 4
Bitwise OR assignment operator: 5
Bitwise X-OR assignment operator: 0
Right shift assignment operator: -1
Left shift assignment operator: -4
Right shift unsigned assignment operator: 1073741823

Bitwise and shift operator are discussed later in this session.

Relational operators:

This operators is used to compare the two values of same data type or find two values are equal or not by its address. In short, it is used to relate the two values of same data types. Some of the operators are <,>,instanceof,==,!=

Relational operators

Example:

public class Example
{
public static void main(String[] args)
{
int a=10,b=20,c=10,d=20;
System.out.println("----------------------------------");
System.out.println("Is a is greater than b: " + (a>b));
System.out.println("Is b is greater than c: " + (b>c));
System.out.println("----------------------------------");
System.out.println("Is b is lesser than c: " + (b<c));
System.out.println("Is c is lesser than d: " + (c<d));
System.out.println("----------------------------------");
System.out.println("Is a is greater than or equal to b: " + (a>=b));
System.out.println("Is b is greater than or equal to d: " + (b>=d));
System.out.println("----------------------------------");
System.out.println("Is b is lesser than or equal to c: " + (b<=c));
System.out.println("Is b is lesser than or equal to d: " + (b<=d));
System.out.println("----------------------------------");
System.out.println("Is a is equals to b: " + (a==b));
System.out.println("Is a is equals to c: " + (a==c));
System.out.println("----------------------------------");
System.out.println("Is a is not equal to b: " + (a!=b));
System.out.println("Is a is not equal to c: " + (a!=c));
System.out.println("----------------------------------");


}
}

Output:
----------------------------------
Is a is greater than b: false
Is b is greater than c: true
----------------------------------
Is b is lesser than c: false
Is c is lesser than d: true
----------------------------------
Is a is greater than or equal to b: false
Is b is greater than or equal to d: true
----------------------------------
Is b is lesser than or equal to c: false
Is b is lesser than or equal to d: true
----------------------------------
Is a is equals to b: false
Is a is equals to c: true
----------------------------------
Is a is not equal to b: true
Is a is not equal to c: false
----------------------------------

Example for  "instanceof operator":
Note: In order to use instanceof operator the object1 should have some relational to object2. if it does not have any relational then it will returns compile time error saying inconvertible types.
if any null value is try to instanceof the object then it will return false.

class Animal
{
}
class Dog extends Animal
{
// class dog is extends from the class animal. inherits from animal
}

class Cat extends Animal
{
// class dog is extends from the class animal. inherits from animal
}
class Horse
{
// it does not extends animal class
}
public class Example
{
public static void main(String[] args)
{
Dog d = new Dog();
Cat c = new Cat();
String s = "Hello world";
Horse h = null;
System.out.println(d instanceof Animal);
System.out.println(c instanceof Animal);
System.out.println(s instanceof String);
System.out.println(h instanceof Horse);
System.out.println(null instanceof Animal);
}
}

Output:
true
true
true
false
false

Shift operator:

This operator is used to shift the bits of number either left or right.

Shift operators

>> - Signed right shift:
This operator is used to shift the bits towards right by the specified value distance. It will fills 1 to the left if the number is negative and fills 0 to the left if the number is positive.

Example:

public class Example
{
public static void main(String[] args)
{
int a = 4, b = -5;
System.out.println(a>>2);
System.out.println(b>>2);
}
}

Output:
1
-2

Explanation:
For positive value: the binary value of  'a'  is 00000000000000000000000000000100 after shifting to 00000000000000000000000000000001 (1)
For negative value: the binary value of  'b'  is 11111111111111111111111111111011 after shifting to 11111111111111111111111111111110 (-2)

<< - Signed left shift:
This operator is used to shift the bits towards left by the specified value. it will fill 0 to the right.

Example:

public class Example
{
public static void main(String[] args)
{
int a = 4, b = -5;
System.out.println(a<<2);
System.out.println(b<<2);
}
}

Output:
16
-20

Explanation:
Positive 'a' value: The binary format of 'a' is 00000000000000000000000000000100. After shifting 00000000000000000000000000010000 (16)
Negative 'a' value: the binary format of 'b is 11111111111111111111111111111011. After shifting 11111111111111111111111111101100 (-20)

>>> - Unsigned right shift:
This operator is similar to signed right operator. But it will fills 0 to the left irrespective to the sign of the number.

Example:

public class Example
{
public static void main(String[] args)
{
int a = 4, b = -5;
System.out.println(a>>>2);
System.out.println(b>>>2);
}
}

Output:
1
1073741822

Explanation:
For positive value: the binary value of  'a'  is 00000000000000000000000000000100 after shifting to 00000000000000000000000000000001 (1)
For negative value: the binary value of  'b'  is 11111111111111111111111111111011 after shifting to 111111111111111111111111111110 (1073741822)


Bitwise operators:

This operator used to manipulate the bit by bit of a value. Some of the Bitwise operators are &,|,^,~

Bitwise operators

& (AND operator):
For Boolean type it will return if and only if both arguments/condition are true else it will return false
For integral type it will return integer values, by doing AND operation for the binary format and returns that in integral values

Example:

a = 4; // binary format of 4 is  100
b = 5; // binary format of 5 is 101
c = a&b // 100 & 101 = 100 (4)

| (OR operator):
For boolean type it will returns true if either one of the argument/condition is true else it will returns false
For integral types it will return integer values, by doing OR operation for the binary format and return that in integer values.

Example:

a = 4;
b = 5;
c = a|b // 100 | 101 =101 (5)

^ (X-OR operator):
For boolean type it will return true if both argument/condition are different else it will return false
For integral types it will return integer values, by doing X-OR operation for the binary format and return that in integer values.

Example:

a = 4;
b = 5;
c = a^b // 100 ^ 101 =001 (1)

~(Bitwise compliment operator):
This operator is not applicable for boolean type. It is only applicable for integral type. This is used to converting bits from 0 to 1 and vice versa.

Example:
int a = 4; // 100
System.out.println(~4); 011 = 3

But the answer of ~4 is -5. Let me discuss in detail about this:
Bitwise compliment operator, first find the equivalent binary form (one's compliment) and inverting all the values of its binary representation. That is

Number of bits for int data type is 32. Here below represent the binary form of 4

0000 0000 0000 0000 0000 0000 0000 0100  =   =1111 1111 1111 1111 1111 1111 1111 1011

After, inverting first sign bit is converted from 0 to 1 (where 0 is positive and 1 is negative). Now we need to display this binary number to decimal, we need to find the 2's compliment that is done by adding 1 to ones's compliment


Example:
System.out.println(~true); // compile time error
System.out.println(~4); // -5

Program Example:

public class Example
{
public static void main(String[] args)
{
int a=4,b=5;
System.out.println("AND operator: " + (a&b));
System.out.println("OR operator: " + (a|b));
System.out.println("X-OR operator: " + (a^b));
System.out.println("Bitwise compliment operator: " + (~a));
}
}

Output:

AND operator: 4
OR operator: 5
X-OR operator: 1
Bitwise compliment operator: -5


Ternary operator:

It is the shorthand form of if... else statement. It has three operands, known as ternary operator. It is also well known as conditional operator.

Syntax:
(Condition)?true:false
If the condition is true then it will prints true value else it will prints false

Example:
public class Example
{
public static void main(String[] args)
{
System.out.println("Finding maximum and minimum values among 3 values");
int a = 89, b = 50, c = 90, max, min;

max = (a>b)?((a>c)?a:c):((b>c)?b:c);
min = (a>b)?((b>c)?c:b):((a>c)?c:a);
System.out.println("maximum value: " + max);
System.out.println("minimum: " + min);
}
}

Output:
Finding maximum and minimum values among 3 values
maximum value: 90
minimum: 50

Explanation:
In the above program. For maximum, at first it will compare "a" and "b" and the compare the maximum value with remaining variable "c" and finally prints the maximum. Similarly for the minimum. you can to compare the minimum values by
min = (a<b)?((a<c)?a:c):((b<c)?b:c);

Logical operator:

This operators are used to perform logical AND and logical OR which is much more similar to AND gate and OR gate in electrical circuit.

Logical operators
Example:

public class Example
{
public static void main(String[] args)
{
int a = 100, b=10, c=50;
System.out.println("Logical AND operator");
if((a>b)&&(a>c))
{
System.out.println("both conditions are true. hence, executing the statement.");
System.out.println("Eventhough one condition is true. it will check all the condition, prints only all the condition are true");
}
else
{
System.out.println("both conditions are not returning true");
}

System.out.println("Logical OR operator");
if((c>b)||(c>a))
{
System.out.println("Either anyone of the condition is true, hence executing the statement");
System.out.println("Checks till the any one statement is true. if one statement is true then it will avoid checking the other statement, print either anyone condition is true");

}
else
{
System.out.println("all the condition are returning false");
}
}
}

Output:

Logical AND operator
both conditions are true. hence, executing the statement.
Eventhough one condition is true. it will check all the condition, prints only all the condition are true
Logical OR operator
Either anyone of the condition is true, hence executing the statement
Checks till the any one statement is true. if one statement is true then it will avoid checking the other statement, print either anyone condition is true


Comments

Popular posts from this blog

PHP Login and Registration script

Multiple image upload to database and display from it

Java program to display diamond pattern