Search

Tuesday, March 30, 2021

Java Operators

 



Java Operators.


 In Java, sometimes we have to perform lots of operations like addition, subtraction, multiplication, division etc on variables as well as on values. These operations can be performed with the help of Operators in Java language.

Following are different types of operators in Java

1.   Arithmetic

2.   Assignment

3.   Relational

4.   Logical

5.   Unary

6.   Bitwise

 

Arithmetic

If you want to perform some mathematical or arithmetic operations on your variables or on values, then we have this category . We have lots of operators in this category like +,-,*,/,% etc


For Example

int a=10, b=2;

then

System.out.println(a+b);

System.out.println(a-b);

System.out.println(a*b);

System.out.println(a/b);

System.out.println(a%b);

 

Assignment

In Java, we have an operator which is used to assign some value to a variable. This operator is known as assignment operator.

For Example

int a = 10;

or

int a;

a = 10;

Here, “=” is the assignment operator and it is assigning the value 10 to variable a

 

Other assignment operators in Java can be

+=, -=, *=, /= etc

 

For Example

int a = 2;

int b;

b = a;

System.out.println(b); // Ans 2

 

int a = 2;

a+=2;

System.out.println(a); // Ans 4

 

int a=2;

a*=2;

System.out.println(a); // Ans 4

 

int a=2;

a/=2;

System.out.println(a); // Ans 1

  

Relational

These operators are used to check the relation of operands. For example, one operand is greater than second operand.

int a=10;

int b=20;

a>b

so here “>” is the operator

We have following Relational operators in Java

·         == (Equal)

·         ! = (Not equal)

·         > (Greater)

·         < (Less)

·         >= (Greater than or Equal)

·         <= (Less than or Equal)  

 

Logical

Logical operators in Java language are used to check some conditions. The answer of condition will be true or false. So with the help of Logical operators, we can check whether the result of the condition is true or false.

We have following types of Logical operators

1. Logical AND (&&) // used when every condition answer is true

2. Logical OR (||) // used when if any of the condition answer is true

3. Logical NOT (!) // Converts true to false and false to true


Example

class LogicalOperatorTest

{

    public static void main(String[] ar)

    {

        System.out.println( (1<2)&&(2<3)); // Answer is true

        System.out.println( (1>2) | | (2<3)); // Answer is true

        System.out.println( !(1<2)); // Answer is true

    }


}

 

Unary 

These operators are used when we have only one operand. This operand can be a value or a variable. 

We have following important Unary operators

1. ++ (Increment Operator)

2. -- (Decrement Operator)


Increment operator is used to add 1 with the operand. For example

int a = 10;

++a; // this will add 1 in variable a and value will be 11


Decrement operator is used to decrement by 1. For Example

int a = 10;

--a; // this will decrease the value of variable a by 1 and answer will be 9  


Video Lecture


No comments:

Post a Comment