Jan 4, 2022

Java Operators - Androidpro

Operator in Java is a symbol that is used to perform operations. Operators are symbols that perform operations on variables and values. For example, + is an operator used for addition, while * is also an operator used for multiplication.

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

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

 

11.   Java Unary Operator

The Java unary operators require only one operand. Unary operators are used to perform various operations i.e.:

·       incrementing/decrementing a value by one

·       negating an expression

·       inverting the value of a boolean

Example Of Unary Java Operator


public class Main {

   
public static void main(String[] args) {
       
 int a=10;
       
 System.out.println(a++);//10 (11) 
       
 System.out.println(++a);//12 
       
 System.out.println(a--);//12 (11) 
       
 System.out.println(--a);//10 

   
}

 

 

Example 2 Of Unary Java Operator

 int a=10;
 int b=10;
 System.out.println(a++ + ++a);//10+12=22 
 System.out.println(b++ + b++);//10+11=21 

 

2. Java Arithmetic Operators

Java arithmetic operators are used to arithmetic operations  perform addition, subtraction, multiplication, and division. They act as basic mathematical operations. For example,

public static void main(String[] args) {

   
long a=10;
    long
b=5;
   
System.out.println(a+b);//15
   
System.out.println(a-b);//5
   
System.out.println(a*b);//50
   
System.out.println(a/b);//2
   
System.out.println(a%b);//0



}

 

Here, the +, *, - and /  operator is used to add , multiply , subtract , divide two variables a and b. Similarly, there are various other arithmetic operators in Java.

 

Example 02

public static void main(String[] args) {



       System.
out.println(100*110/50+3-1*4/2);

}

 

Java Arithmetic Operators Symbols

 

Operator

Operation

+

Addition

-

Subtraction

*

Multiplication

/

Division

%

Modulo Operation (Remainder after division)

 

Example 03

public static void main(String[] args) {

   
// declare variables
   
int a = 12, b = 5;

   
// addition operator
   
System.out.println("a + b = " + (a + b));

   
// subtraction operator
   
System.out.println("a - b = " + (a - b));

   
// multiplication operator
   
System.out.println("a * b = " + (a * b));

   
// division operator
   
System.out.println("a / b = " + (a / b));

   
// modulo operator
   
System.out.println("a % b = " + (a % b));


}

 

Output

a + b = 17

a - b = 7

a * b = 60

a / b = 2

a % b = 2

 

In the above example, we have used +-, and * operators to compute addition, subtraction, and multiplication operations.

/ Division Operator

Note the operation, a / b in our program. The / operator is the division operator.

If we use the division operator with two integers, then the resulting quotient will also be an integer. And, if one of the operands is a floating-point number, we will get the result will also be in floating-point.

In Java,

 

(9 / 2) is 4

(9.0 / 2) is 4.5

(9 / 2.0) is 4.5

(9.0 / 2.0) is 4.5

% Modulo Operator

The modulo operator % computes the remainder. When a = 7 is divided by b = 4, the remainder is 3

 

3.  Java Assignment Operator

Java assignment operator is one of the most common operators. Assignment operators are used in Java to assign values to variables. For example,

int Age;

Age = 51;

Here, = is the assignment operator. It assigns the value on its right to the variable on its left. That is, 51 is assigned to the variable Age.

Example 02

int a=10;
int
b=20;
a+=4;//a=a+4 (a=10+4) 
b-=4;//b=b-4 (b=20-4) 
System.out.println(a);
System.out.println(b);

Let's see some more assignment operators available in Java.

Operator

Example

Equivalent to

=

a = b;

a = b;

+=

a += b;

a = a + b;

-=

a -= b;

a = a - b;

*=

a *= b;

a = a * b;

/=

a /= b;

a = a / b;

%=

a %= b;

a = a % b;

 

 

 

public static void main(String[] args) {

   
// create variables
   
int a = 4;
    int
var;

   
// assign value using =
   
var = a;
   
System.out.println("var using =: " + var);

   
// assign value using =+
   
var += a;
   
System.out.println("var using +=: " + var);

   
// assign value using =*
   
var *= a;
   
System.out.println("var using *=: " + var);


}

Output

var using =: 4

var using +=: 8

var using *=: 32

 

4. Java Relational Operators

Relational operators are used to check the relationship between two operands. For example,

// check if a is less than b

A > B;

Here, > operator is the relational operator. It checks if A is greater than B or not.

 

Operator

Description

Example

==

Is Equal To

3 == 5 returns false

!=

Not Equal To

3 != 5 returns true

> 

Greater Than

3 > 5 returns false

< 

Less Than

3 < 5 returns true

>=

Greater Than or Equal To

3 >= 5 returns false

<=

Less Than or Equal To

3 <= 5 returns true

 

Example Java Relational Operators

    // create variables

    int a = 7, b = 11;

 

    // value of a and b

    System.out.println("a is " + a + " and b is " + b);

 

    // == operator

    System.out.println(a == b);  // false

 

    // != operator

    System.out.println(a != b);  // true

 

    // > operator

    System.out.println(a > b);  // false

 

    // < operator

    System.out.println(a < b);  // true

 

    // >= operator

    System.out.println(a >= b);  // false

 

    // <= operator

    System.out.println(a <= b);  // true

 

5. Java Logical Operators

Logical operators are used to check whether an expression is true or false. They are used in decision making.

 

Operator

Example

Meaning

&& (Logical AND)

expression1 && expression2

true only if both expression1 and expression2 are true

|| (Logical OR)

expression1 || expression2

true if either expression1 or expression2 is true

! (Logical NOT)

!expression

true if expression is false and vice versa

 

Example 4: Logical Operators

class Main {

  public static void main(String[] args) {

 

    // && operator

    System.out.println((5 > 3) && (8 > 5));  // true

    System.out.println((5 > 3) && (8 < 5));  // false

 

    // || operator

    System.out.println((5 < 3) || (8 > 5));  // true

    System.out.println((5 > 3) || (8 < 5));  // true

    System.out.println((5 < 3) || (8 < 5));  // false

 

    // ! operator

    System.out.println(!(5 == 3));  // true

    System.out.println(!(5 > 3));  // false

  }

}

 

6. Java Unary Operators

Unary operators are used with only one operand. For example, ++ is a unary operator that increases the value of a variable by 1. That is, ++15 will return 16.

Different types of unary operators are:

Operator

Meaning

+

Unary plus: not necessary to use since numbers are positive without using it

-

Unary minus: inverts the sign of an expression

++

Increment operator: increments value by 1

--

Decrement operator: decrements value by 1

!

Logical complement operator: inverts the value of a boolean

 

Increment and Decrement Operators

Java also provides increment and decrement operators: ++ and -- respectively. ++ increases the value of the operand by 1, while -- decrease it by 1. For example,

 

int num = 15;

 

// increase num by 1

++num;

Here, the value of num gets increased to 16 from its initial value of 15.

Example 5: Increment and Decrement Operators

 

class Main {

  public static void main(String[] args) {

   

    // declare variables

    int a = 12, b = 12;

    int result1, result2;

 

    // original value

    System.out.println("Value of a: " + a);

 

    // increment operator

    result1 = ++a;

    System.out.println("After increment: " + result1);

 

    System.out.println("Value of b: " + b);

 

    // decrement operator

    result2 = --b;

    System.out.println("After decrement: " + result2);

  }

}

 

7. Java Bitwise Operators

Bitwise operators in Java are used to perform operations on individual bits. For example,

Bitwise complement Operation of 35

 

35 = 00100011 (In Binary)

 

~ 00100011

  ________

   11011100  = 220 (In decimal)

Here, ~ is a bitwise operator. It inverts the value of each bit (0 to 1 and 1 to 0).

The various bitwise operators present in Java are:

 

Operator

Description

~

Bitwise Complement

<< 

Left Shift

>> 

Right Shift

>>> 

Unsigned Right Shift

&

Bitwise AND

^

Bitwise exclusive OR

These operators are not generally used in Java.

Whatsapp Button works on Mobile Device only