Operators in Java


Operators

In Java Operators are symbols that are used to perform some operations on the operands.They are used to manipulate primitive Data types.Combination of operands and operators are known as expression.Java provides a rich set of operators to manipulate the variables.There  are three types of operators in Java.

1. Unary operators

2. Binary operators

3. Ternary operators

operator

 

 

 

 

 

 

 

 

 

 

 

 

1.Unary Operator:

In which we use one operand is called unary operator.It has two types.

1. Increment Unary operator

2.Decrement Unary operator

1. Increment Unary operator:

This is used to increase the value by one.It has two types.

1. Post-fix Increment operator

2. Pre-fix Increment operator

1. Post-fix Increment operator:

In this operator “++” symbol is used.This symbol is used after the operand.

In this operator value is first assign the value to a variable and then incremented the value.

“++” symbol is not used over “constant” and “final variable”.

Example

class postincre

{

public static void main(String aa[])

{

int a,b;

a=10;

b=a++;

System.out.println(“b=”+b);

System.out.println(“a=”+a);

}

}

output:

inc1

 

 

 

 

 

 

 

 

In above example first the value of  “a”  is assign to the variable “b” .Then increment the value.So the value of b variable is “10”.

1. Pre-fix Increment operator:

In this operator “++” symbol is used.This symbol is used after the operand.

In this operator value is incremented first and then incremented value is used in expression.

“++” symbol is not used over “constant” and “final variable”.

Example

class preincre

{

public static void main(String aa[])

{

int a,b;

a=10;

b=++a;

System.out.println(“b=”+b);

System.out.println(“a=”+a);

}

}

output:

preinc

 

 

 

 

 

 

 

 

In above example first the increment is done then the value of “a” variable is assigned to the variable “b”.So the value of “b” variable is 11.

1. Post-fix decrement operator:

In this operator “–“ symbol is used.This symbol is used after the operand.

In this operator value is first assign the value to a variable and then decremented the value.

“–“ symbol is not used over “constant” and “final variable”.

Example

class postdecre

{

public static void main(String aa[])

{

int a,b;

a=10;

b=a–;

System.out.println(“b=”+b);

System.out.println(“a=”+a);

}

}

output:

postde

 

 

 

 

 

 

 

 

In above example first the value of  “a”  is assign to the variable “b” .Then decrement the value.So the value of b variable is “10”.

1. Pre-fix decrement operator:

In this operator “–“ symbol is used.This symbol is used after the operand.

In this operator value is decremented first and then decremented value is used in expression.

“++” symbol is not used over “constant” and “final variable”.

Example

class predecre

{

public static void main(String aa[])

{

int a,b;

a=10;

b=–a;

System.out.println(“b=”+b);

System.out.println(“a=”+a);

}

}

output:

predec

 

 

 

 

 

 

 

In above example first the value of  “a”  is decrement then assign to the variable “b” .So the value of b variable is “9”.

2.Binary operators :

In which we use two operand is called Binary operator. Java supports many types of Binary Operator.

1. Assignment Operator

2. Arithmetic Operator

3. Logical  Operator

4. Comparison Operator

 1. Assignment Operator :

This Operator is used to assign the value. This symbol “=” is used to assign the value . e.g

int a=12;

2. Arithmetic Operator :

This Operator is used to perform mathematical operation on operand .Arithmetic operator are

 

Operators Description Use
1. Additional operator (“+”) : Used to add the value of two operand. a+b
2. Subtract operator (“-“) : Used to subtract the value of two operand. a-b
3. Multiply operator (“*”) : Used to multiply the value of two operand. a*b
4. Division  operator (“/”) : Used to divide the value of two operand. a/b
5. Modulus operator(“%”) : Used returns the remainder of a division operation. a%b

 Example :

class ArithOp
{
public static void main (String[] args)
{
   // answer is now 6
int answer = 4 + 2;
System.out.println(“Addition is =” +answer);

        // answer is now 5
answer = answer – 1;
System.out.println(“Subtraction is =” +answer);

   // answer is now 10
answer = answer * 2;
System.out.println(“Multiply is = ” +answer);

// answer is now 5
answer = answer / 2;
System.out.println(“Division is = ” +answer);

  // answer is now 1
answer = answer % 2;
System.out.println(“Reminder is = ” +answer);
}
}

output :

arith1

 

 

 

 

 

 

 

 

 

 

 

 

3. Logical operator :

Logical operator are used when we want to check multiple condition together.

A logical operator in Java programming is an operator that returns a Boolean result that’s based on the Boolean result of one or two other expressions.So, some time it is called Boolean operator in java.

Sometimes, expressions that use logical operators are called “compound expressions” because the effect of the logical operators is to let you combine two or more condition tests into a single expression.

It has three types.

1. Logical AND Operator
2. Logical OR Operator
3. Logical NOT Operator

1. Logical AND Operator :

We can combine many relational expressions using “Logical And” operators .The result will be a boolean type.This operator is represented by the symbol “&&”.

Consider the operation “operand1 && operand2”

Here operand1 and operand2 can be relational expressions or boolean types. The result of the operation “operand1 && operand2” will be

  1. “true” only if both operand1 and operand2 are “true”
  2. “false”   if any of the operands (operand1 or operand2) is “false” or both the operands (operand1 or operand2) are “false”.Like below
Operand1 Operand2 Result
True True True
True False False
False False False

Example:

class logi
{
public static void main(String aa[])
{
int a=10;
int b=20;
int c=30;
if ((a>b)&&(a>c))
System.out.println(“a is greater”+a);
else if((b>a)&&(b>c))
System.out.println(“b is greater”+b);
else if((c>a)&&(c>b))
System.out.println(“c is greater”+c);
}
}

output:

logi

 

 

 

 

 

 

 

In above example output is 30 because c is greater than a and b and both condition is true.

2. Logical OR Operator :

We can combine many relational expressions using “Logical OR” operators .The result will be a boolean type.This operator is represented by the symbol “||”.

Consider the operation “operand1 && operand2”

Here operand1 and operand2 can be relational expressions or boolean types. The result of the operation “operand1 || operand2” will be

  1. “false” only if both operand1 and operand2 are “false”
  2. “true”   if any of the operands (operand1 or operand2) is “true” or both the operands (operand1 or operand2) are “true”.Like below
Operand1 Operand2 Result
True True True
True False true
False False False

Example:

class logi
{
public static void main(String aa[])
{
int a=1;
int b=2;
if ((a==1)||(b==2))
System.out.println(“u r win”);
else
System.out.println(“u r not win”);
}
}

output:

logior

 

 

 

 

 

 

In above code the code will run if a is equal to 1 OR b is equal to 2. Only one of them has to be true for that to happen. If both are true, that’s fine also. The only time that if statement will be false is when num1 is not equal to 1 AND num2 is not equal to 2.If a is equal to 1 or b is equal to 3 then if statement is true. or condition is only false when both condition are false otherwise it is true.

3. Logical NOT Operator :

Logical NOT operator is used to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.If a condition is false then Logical NOT operator will make True.

The not operator is probably the easiest to understand. It is simply the opposite of what the condition says.

Example

class loginot

{

public static void main(String aa[])

{

boolean a=true;

if (!a)

System.out.println(“u r win”);

else

System.out.println(“u r not win”);

}

}

output:

loginot

 

 

 

 

 

 

 

 

 

 

In above example “if not true” is asking if the variable  “a” variable  is not true, otherwise known as false. If “a” variable is false, Java will display “u r win”. “a” variable is set to true, so that code will not execute. then the else part is execute shown in output.

 

4. Relational operator:

This operator is used to compare the two values ,so this operator is also known as “comparison operator.”

Conditional symbols and their meanings for comparison operator are below.

 

Operator Condition Description Example
== is equal to Checks if the values of two operands are equal or not, if yes then condition becomes true. (a == b) is not true.
!= is not equal to Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. (a != b) is true.
> is greater than Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (a > b) is not true.
< is less than Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (a < b) is true.

 

Example:

class relat

{

public static void main(String aa[])

{

int a=10;

int b=5;

if (a>b)        // “>” relational operator

system.out.println(“a is greater”);

else if (a==b)    //”==” relational operator

System.out.println(“a is equal to b”);

else

System.out.println(“enter 1 to 10 number”);

}

}

 output:

rela

3. Ternary operator:

In Ternary operator use three operands.It is also called conditional assignment statement because the value assigned to a variable depends upon a logical expression.

syntax is :

variable=(test expression) ? Expression 1 : Expression 2

Example:

c=(a>b)?a:b;

 

c=               (a>b)            ?                       a              :               b        ;

Test Condition            Expression1             Expression2

we can use conditional operator(ternary operator) in nested form as shown below:

Program to find maximum among three numbers:

int  a=15,  b=22,  c=13 ;

max =   (a>b) ?                       (a>c) ? a : c         : (b>c) ? b : c ;

Test condition           Expression 1          Expression 2

The test condition a>b is false. Hence it will operate expression 2.in expression 2,the test condition b>c is true which enables value 12 to be stored in max. Hence  max=22

Example of Ternary operator:

class terna

{

public static void main(String aa[])

{

int a,b,result;

a=10;

b=20;

result=(a>b)?a:b;

System.out.println(“result=”+c);

}

}

output:

terna

 

 

 

 

 

 

 

 

 

In above example result is 2o,which is the value of “b”. if the value of  “a”  variable is greater  the variable result show the value of  “a” variable.

 

 

 

Comments are closed.