Java - Operators, Types of Operators - best tricks forever

BEGINNER GUIDE (Hindi)

test banner

Post Top Ad

Responsive Ads Here

Monday, 14 May 2018

Java - Operators, Types of Operators

Java - Operators, Types of Operators

what is an operator?

An operator is a character that represents an action.

what is an operator in java ?

Operators are termed as special symbols that perform specific operations on one, two, or three operands, and then returns the result.

what are the types of operators ?

there are 8 types of operators in java 


  • Arithmetic Operators
  • Unary Operators
  • Relational Operators
  • Assignment Operators
  • Logical Operators
  • Ternary Operators
  • Bitwise Operators
  • Shift Operator


1. Arithmetic operators 

what is an arithmetic operator ?

Java supports various arithimatic operations such as Addition / subtraction / multiplication / division 

1.Addition :

Addition of two numbers is done by Addition operator "+" .

Example 1 :

public class AdditionEg{

public static void main(String args[])
{
int a = 10;
int b = 20;
int c = a+b; // Addition operator
System.out.println("The value of a = " +a);
System.out.println("The value of b = "+ b);
System.out.println("The addition value of c = "+c);
}
}

Output :

The value of a = 10
The value of b = 20
The addition value of c = 30

2. Subtraction :

Subtraction of two numbers is done by Subtraction operator "-" .

Example 1:

public class SubtractEg{

public static void main(String args[])
{
int a = 10;
int b = 5;
int c = a-b; // Subtraction operator
System.out.println("The value of a = " +a);
System.out.println("The value of b = "+ b);
System.out.println("The subtracted value of c = "+c);
}
}

Output :

The value of a = 10
The value of b = 5
The addition value of c = 5

3. Multiplication :

Multiplication of two numbers is done by Multiplication operator "*" .

Example 1 :

public class MultiplyEg{

public static void main(String args[])
{
int a = 10;
int b = 5;
int c = a*b; // Multiplication operator
System.out.println("The value of a = " +a);
System.out.println("The value of b = "+ b);
System.out.println("The multiply value of c = "+c);
}
}

Output :

The value of a = 10
The value of b = 5
The addition value of c = 50

4. Division :

Division of two numbers is done by Division operator "/" .

Example 1 :

public class DivisionEg{

public static void main(String args[])
{
int a = 10;
int b = 5;
int c = a/b; // Division operator
System.out.println("The value of a = " +a);
System.out.println("The value of b = "+ b);
System.out.println("The division value of c = "+c);
}
}

Output :

The value of a = 10
The value of b = 5
The addition value of c = 2

2. Unary Operators

what is unary operators ?

These operators requires only one operand to perform different kind of operations such as increasing/decreasing a value, negating an expression, or inverting a boolean value. 

1. Unary plus operator:

Example 1 :

public class Eg1{

public static void main(String args[])
{
int a = +1;
System.out.println("The value of a = " + a);
}
}

Output :

The value of a = 1

2. Unary minus operator:

This will negates the value, example shown below.

Example 1 :

public class Eg1{

public static void main(String args[])
{
int a = -1;
System.out.println("The value of a = " + a);
}
}

Output :

The value of a = -1

3. Unary Increment operator :

++ is the increment operator, this can be added in prefix or postfix of the variable.

if ++ operator is used by default 1 will be added to the actual value of the variable.

Example 1 :

public class Eg1{

public static void main(String args[])
{
int a = 10;
System.out.println("The value of a = " + a);
a = ++a;
System.out.println("The value of a = " + a);
}
}

Output :

The value of a = 10
The value of a = 11

4. Unary Decrement operator :

-- is the decrement operator, this can be added in prefix or postfix of the variable.

if -- operator is used by default 1 will be subtracted to the actual value of the variable.

Example 1 :

public class Eg1{

public static void main(String args[])
{
int a = 10;
System.out.println("The value of a = " + a);
a = --a;
System.out.println("The value of a = " + a);
}
}

Output :

The value of a = 10
The value of a = 9

5. Unary Logical complement operator :

This "!" operator inverts the value of a boolean

Example 1 :

public class Eg1{

public static void main(String args[])
{
boolean success = true;
System.out.println("The value of success = " + success);
System.out.println("The inverted value of success = " + !success);
}
}

Output :

The value of success = true
The inverted value of success = false

3. Relational Operators 

what is Relational Operators in Java ?

Java has six relational operators that compare two variables and return a boolean value.

1. Equal to (==)

This checks if the values of two operands are equal or not, if yes then condition becomes true.

Example 1 :

public class Eg2 {

public static void main(String args[]) {
int a = 10;
int b = 20;

System.out.println("The value of a == b = " + (a == b) );
}
}

Output :

The value of a == b = false

2. Not equal to (!=)

This checks if the values of two operands are equal or not, if values are not equal then condition becomes true.

Example 1 :

public class Eg2 {

public static void main(String args[]) {
int a = 10;
int b = 20;

System.out.println("The value of a != b = " + (a != b) );
}
}

Output :

The value of a != b = true

3. Greater than (>)

This checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.

Example 1 :

public class Eg2 {

public static void main(String args[]) {
int a = 10;
int b = 20;

System.out.println("The value of a > b = " + (a > b) );
}
}

Output :

The value of a > b = false

4. Less than (<)

This checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.

Example 1 :

public class Eg2 {

public static void main(String args[]) {
int a = 10;
int b = 20;

System.out.println("The value of a < b = " + (a < b) );
}
}

Output :

The value of a < b = true

5. Greater than or equal to (>=)

This checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.

Example 1 :

public class Eg2 {

public static void main(String args[]) {
int a = 10;
int b = 20;

System.out.println("The value of a >= b = " + (a >= b) );
}
}

Output :

The value of a >= b = false

6. Less than or equal to (<=) 

This checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.

Example 1 :

public class Eg2 {

public static void main(String args[]) {
int a = 10;
int b = 20;

System.out.println("The value of a <= b = " + (a <= b) );
}
}

Output :

The value of a <= b = true

4 . Assignment Operators

what is Assignment Operators ?

Assignment operator is used to assign value to a variable.

1. "=" - Assignment Operator

This operator assigns values from right side operands to left side operand.

Example 1 :

class Eg3{

public static void main(String args[])
{
int a = 10;
int b = a;
System.out.println("The value of a = " +a);
System.out.println("The value of b = " +b);
}
}

Output :

The value of a = 10
The value of b = 10

2. "+=" - Assignment Operator

It adds right operand to the left operand and assign the result to left operand.

Example 1:

class Eg3{

public static void main(String args[])
{
int a = 10;
int b = 20;
b += a; // b = b+a
System.out.println("The value of a = " +a);
System.out.println("The value of b = " +b);
}
}

Output :

The value of a = 10
The value of b = 30

3. "-=" - Assignment Operator

It subtracts right operand from the left operand and assign the result to left operand.

Example 1 :

class Eg3{

public static void main(String args[])
{
int a = 10;
int b = 20;
b -= a; // b = b-a
System.out.println("The value of a = " +a);
System.out.println("The value of b = " +b);
}
}

Output :

The value of a = 10
The value of b = 10

4. "*=" - Assignment Operator 

It multiplies right operand with the left operand and assign the result to left operand.

Example 1 :

class Eg3{

public static void main(String args[])
{
int a = 10;
int b = 20;
b *= a; // b = b*a
System.out.println("The value of a = " +a);
System.out.println("The value of b = " +b);
}
}

Output :

The value of a = 10
The value of b = 200

5. "/=" - Assignment Operator

It divides left operand with the right operand and assign the result to left operand.

Example 1 :

class Eg3{

public static void main(String args[])
{
int a = 10;
int b = 20;
b /= a; // b = b/a
System.out.println("The value of a = " +a);
System.out.println("The value of b = " +b);
}
}

Output :

The value of a = 10
The value of b = 2

6. "%=" - Assignment Operator

It takes modulus using two operands and assign the result to left operand.

Example 1 :

class Eg3{

public static void main(String args[])
{
int a = 10;
int b = 20;
b %= a; // b = b%a
System.out.println("The value of a = " +a);
System.out.println("The value of b = " +b);
}
}

Output :

The value of a = 10
The value of b = 0

5 . Logical Operators

what is logical Operators in java ?

They allow a program to make a decision based on multiple conditions. Each operand is considered a condition that can be evaluated to a true or false value.

1. Logical AND Operator (&&)

If both the operands are non-zero and true, then the condition becomes true.

Example 1 :

public class Eg{

public static void main(String args[])
{
boolean a = true;
boolean b = false;
System.out.println("The value of a = " +a);
System.out.println("The value of b = " +b);
System.out.println("The value of a&&b = " + (a&&b));
}
}

Output :

The value of a = true
The value of b = false
The value of a&&b = false

2. Logical OR Operator (||)

If any of the two operands are non-zero and true, then the condition becomes true.

Example 1 :

public class Eg{

public static void main(String args[])
{
boolean a = true;
boolean b = false;
System.out.println("The value of a = " +a);
System.out.println("The value of b = " +b);
System.out.println("The value of a||b = " + (a||b));
}
}

Output :

The value of a = true
The value of b = false
The value of a||b = true

3. Logical NOT Operator (!)

If a condition is true then Logical NOT operator will make false.

Example 1 :

public class Eg{

public static void main(String args[])
{
boolean a = true;
boolean b = false;
System.out.println("The value of a = " +a);
System.out.println("The value of b = " +b);
System.out.println("The value of !(a&&b) = " + !(a&&b));
}
}

Output :

The value of a = true
The value of b = false
The value of !(a&&b) = true

Example 2 :

public class Eg{

public static void main(String args[])
{
boolean a = true;
boolean b = false;
System.out.println("The value of a = " +a);
System.out.println("The value of b = " +b);
System.out.println("The value of !(a||b) = " + !(a||b));
}
}

Output :

The value of a = true
The value of b = false
The value of !(a||b) = false

6. Ternary Operators

what is ternary operators ?

Its is also called as conditional operator, and it takes three arguments.

The first argument is a comparison argument.
The second is the result upon a true comparison.
The third is the result upon a false comparison.

Example 1:

if (expression) {
a = 10;
}
else {
a = -10;
}

Above code can be replaced by
a = (expression) ? expressionTrue : expressinFalse;

Example 2 :

public class Eg4{  public static void main(String args[]){int a = 10;String result = (a<20) ? "a less than 20" : "a greater than 20";System.out.println("The value of a = " +a);System.out.println("The value of result = " +result);
}
}

Output :

The value of a = 10
The value of result = a less than 20


7 . Bitwise Operators 

what is Bitwise operators in Java ?

Bitwise operators operate on individual bits of integer (int and long) values.

Bitwise operators can be applied to the integer types, long, int, short, char, and byte.

1. Bitwise OR (|)

Here is an example for Bitwise OR Operator.

Example 1 :

public class Eg5{

public static void main (String args[])
{
int a = 5;
int b = 10;

// decimal value of a = 101
// decimal value of b = 1010
// 00000101
// 00001010
// Applying (|) operator
// 00001111
// Decimal value of 00001111 is 15

System.out.println("The value of a = "+a);
System.out.println("The value of b = "+b);
System.out.println("The value of (a|b) = "+(a|b));

}
}

Output :

The value of a = 5
The value of b = 10
The value of (a|b) = 15

How OR Operator works ?

decimal value of a = 101
decimal value of b = 1010
00000101
00001010
Applying (|) operator 
00001111
Decimal value of 00001111 is 15

Thus output is printed as 15.

2. Bitwise AND (&)

Here is an example for Bitwise AND Operator.

Example 1 :

public class Eg5{

public static void main (String args[])
{
int a = 5;
int b = 10;

// decimal value of a = 101
// decimal value of b = 1010
// 00000101
// 00001010
// Applying (&) operator
// 00000000
// Decimal value of 00000000 is 0
System.out.println("The value of a = "+a);
System.out.println("The value of b = "+b);
System.out.println("The value of (a&b) = "+(a&b));

}
}

Output :

The value of a = 5
The value of b = 10
The value of (a&b) = 0

How AND Operator works ? 

 decimal value of a = 101
 decimal value of b = 1010
 00000101
 00001010
 Applying (&) operator 
 00000000
 Decimal value of 00000000 is 0

3. Bitwise Complement (~)

Here is an example for Bitwise COMPLIMENT Operator.

Example 1 :

public class Eg5{

public static void main (String args[])
{
int a = 5;
int b = 10;

// decimal value of a = 101
// decimal value of b = 1010
// 00000101
// 00001010
// Applying (~) operator
// for a value = -110
// for b value = -1011
// Decimal value of -110 for a = -6
// Decimal value of -1011 for b = -11
System.out.println("The value of a = "+a);
System.out.println("The value of b = "+b);
System.out.println("The value of ~a = "+(~a));
System.out.println("The value of ~b = "+(~b));

}
}

Output :

The value of a = 5
The value of b = 10
The value of ~a = -6
The value of ~b = -11

How COMPLIMENT Operator works ?

 decimal value of a = 101
 decimal value of b = 1010
 00000101
 00001010
 Applying (~) operator 
 for a value = -110
 for b value = -1011 
 Decimal value of -110 for a = -6
 Decimal value of -1011 for  b = -11

4. Bitwise XOR (^)

Here is an example for Bitwise XOR Operator.

Example 1 :

public class Eg5{

public static void main (String args[])
{
int a = 5;
int b = 10;

// decimal value of a = 101
// decimal value of b = 1010
// 00000101
// 00001010
// Applying (^) operator
// 00001111
// Decimal value of a^b = 15
System.out.println("The value of a = "+a);
System.out.println("The value of b = "+b);
System.out.println("The value of a^b = "+(a^b));

}
}

Output :

The value of a = 5
The value of b = 10
The value of a^b = 15

How XOR Operator works ?

 decimal value of a = 101
 decimal value of b = 1010
 00000101
 00001010
 Applying (^) operator
 00001111
 Decimal value of a^b = 15

8 . Shift Operators

what is Shift Operators ?

Shift operators works on shifting the bits of the left operand by the number of positions specified by the right operand. 

The left shift operator shifts bits to the left, and the right shift operator shifts bits to the right.

1. Left Shift Operator ( << )

The left operands value is moved left by the number of bits specified by the right operand.

Example 1 :


public class Eg6{

public static void main(String args[])
{
int a = 10;
System.out.println("Left shift operator value for a = " + (a << 0));
System.out.println("Left shift operator value for a = " + (a << 1));
System.out.println("Left shift operator value for a = " + (a << 2));
System.out.println("Left shift operator value for a = " + (a << 3));
System.out.println("Left shift operator value for a = " + (a << 4));
System.out.println("Left shift operator value for a = " + (a << 5));
}
}

Output :

Left shift operator value for a = 10
Left shift operator value for a = 20
Left shift operator value for a = 40
Left shift operator value for a = 80
Left shift operator value for a = 160
Left shift operator value for a = 320

2. Right Shift Operator ( >> )

The left operands value is moved right by the number of bits specified by the right operand.

Example 1 :

public class Eg6{

public static void main(String args[])
{
int a = 10;
System.out.println("Right shift operator value for a = " + (a >> 0));
System.out.println("Right shift operator value for a = " + (a >> 1));
System.out.println("Right shift operator value for a = " + (a >> 2));
System.out.println("Right shift operator value for a = " + (a >> 3));
System.out.println("Right shift operator value for a = " + (a >> 4));
System.out.println("Right shift operator value for a = " + (a >> 5));
}
}

Output :

Right shift operator value for a = 10
Right shift operator value for a = 5
Right shift operator value for a = 2
Right shift operator value for a = 1
Right shift operator value for a = 0
Right shift operator value for a = 0

3. Zero Fill Right Shift Operator ( >>> )


The left operands value is moved right by the number of bits specified by the right operand and shifted values are filled up with zeros.

Example 1 :

public class Eg6{

public static void main(String args[])
{
int a = 10;
System.out.println("Zero fill Right shift operator value for a = " + (a >>> 0));
System.out.println("Zero fill Right shift operator value for a = " + (a >>> 1));
System.out.println("Zero fill Right shift operator value for a = " + (a >>> 2));
System.out.println("Zero fill Right shift operator value for a = " + (a >>> 3));
System.out.println("Zero fill Right shift operator value for a = " + (a >>> 4));
System.out.println("Zero fill Right shift operator value for a = " + (a >>> 5));
}
}

Output :

Zero fill Right shift operator value for a = 10
Zero fill Right shift operator value for a = 5
Zero fill Right shift operator value for a = 2
Zero fill Right shift operator value for a = 1
Zero fill Right shift operator value for a = 0
Zero fill Right shift operator value for a = 0








































Note :  Hi i am new for blogging, if you think my blog is useful, kindly SUBSCRIBE  in the website and also share this to your friends. And if anything is wrong in the tutorial please correct me and kindly forgive me. Thanks everyone.



1 comment:

  1. https://www.allhindimehelp.com/downloadhub-2020-300mb-movies-download/

    ReplyDelete

Post Bottom Ad

Responsive Ads Here