Java Operators

OPERATORS:

Operator is a symbol that tells the compiler to perform mathematical and logical tasks.


Java provides a rich operator Environment.Most of its operators can be divided into the following Five groups:


1. Arithmetic operators

2. Relational operators

3. Logical operators

4. Assignment operators

5. Ternary operators

6. Bitwise operators


1. Arithmetic Operators:

Arithmetic Operators are used to perform mathematical operations.Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra.The arithmetic operators perform addition, subtraction, multiplication, division, exponentiation, and modulus operations.


Note: The operands of the arithmetic operators must be of numeric type.You cannot use them on boolean types,But you can use them on char type.


Note: In java modulus operator(%) can be applied to floating-point types as well as integer types.In contrast to C/C++, where it is restricted to integer types.


Note: In Pre-Increment , you increase the value of a variable before using it, while in post-increment, you use the current value and then increase it.
In short, Pre-Increment means “ Increment before using” and Post-Increment means “use before incrementing”.

//A java program to demonstrate arithmetic operators

public class ApnaStudy{

//main method
public static void main(String[] args) {

//Declaration of integer variables
int num1=10,num2=20;
//Addition
int sum=num1+num2;
System.out.println("Addition is "+sum);

//Subtraction
int sub=num1-num2;
System.out.println("Subtraction is "+sub);

//Multiplication
int mul=num1*num2;
System.out.println("Multiplication is "+mul);

//Division
int divide=num1/num2;
System.out.println("Division is "+divide);

//Modulus
int mod=num1%num2;
System.out.println("Modulus is "+mod);

//Increment
int post_increment=num1++;
int pre_increment=++num1;
System.out.println("post_increment is "+post_increment);
System.out.println("pre_increment is "+pre_increment);

//Decrement
int post_decrement=--num1;
int pre_decrement=num1--;
System.out.println("post_increment is "+ post_decrement);
System.out.println("pre_decrement is "+pre_decrement);
}
}


/*OUTPUT*/

Addition is 30
Subtraction is -10
Multiplication is 200
Division is 0
Modulus is 10
post_increment is 10
pre_increment is 12
post_increment is 11
pre_decrement is 11


2. Relational operators:

Relational operators are also known as comparison operators.
Comparison operators are essential for making decisions in programs.They are widely used in conditional statements (like if-else)and loops to control program flow based on certain conditions.


  • Purpose: Relational operators are used to compare the values of two operands(Variables or expression) and show the relationship between them.
  • Purpose: Relational operator returns a boolean value(true or false) as output.

//A java program to demonstrate Relational operators

import java.util.Scanner;

public class ApnaStudy{

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter First number");
int num1 = sc.nextInt();

System.out.println("Enter Second number");
int num2 = sc.nextInt();
// Equality
System.out.println(num1 + " == " + num2 + " " + (num1 == num2));
//Strict Equality
System.out.println(num1 + " === " + num2+" "+ (num1 == num2));
//Strict inequality
System.out.println(num1 + " !== " + num2+" "+ ( num1 != num2));
// Inequality
System.out.println(num1 + " != " + num2 + " " + (num1 != num2));
// Greater Than
System.out.println(num1 + " > " + num2 + " " + (num1 > num2));
// Less Than
System.out.println(num1 + " < " + num2 + " " + (num1 < num2));
// Greater Than or Equal To
System.out.println(num1 + " >= " + num2 + " " + (num1 >= num2));
// Less Than or Equal To
System.out.println(num1 + " <= " + num2 + " " + (num1 <= num2)); } }


/*OUTPUT*/

Enter First number
8
Enter Second number
9
8 == 9 false
8 === 9 false
8 != 9 true
8 !== 9 true
8 > 9 false
8 < 9 true
8 >= 9 false
8 <= 9 true


3. Logical operators:

Logical operators in java code are building blocks of decision-making.Logical operators in java are tools that help you make decision based on logical conditions(true or false).

//A java program to demonstrate Logical operators

public class ApnaStudy{
public static void main(String[] args) {
    int age=18;
    boolean isCitizen=true;
   
//Logical and-(Both age and citizenship is required)
    boolean canVote=age>=18 && isCitizen;
    System.out.println("can he vote "+canVote);    
//Logical OR-(only one condition is needed)
    boolean eligibleForPoliceJob=age>18||isCitizen;
    System.out.println("Is he eligible "+eligibleForPoliceJob);    
//Logical NOT
    boolean notMinor=age>18;
    System.out.println("Not a minor "+notMinor);    } }


/*OUTPUT*/

can he vote true
Is he eligible true
Not a minor false


Note:
Short-Circuit Logical Operators:Java provides two interesting Boolean operators not found in many other programming languages.


If you use the || and && forms ,rather than the | and & forms of these operators,Java will not bother to evaluate the right-hand operand when the outcome of the expression can be determined by the left-hand operand alone.


For And, The AND operator evaluates its expression from left to right. If the left condition is false,there is no point in checking the second one(right operator) because the entire condition will be false anyway.


For OR,The OR operator evaluates its expression from left to right. If the left condition is true,there is no need to evaluate the second one(right operator) because the entire condition will be false anyway.


4.Assignment operators:

Assignment operators are used to assign values to the variables.These are the workhorses of your code when it comes to storing and manipulating data.


Note: Nested Assignment Operators: This operator is used to assign single value to multi variables.
eg: a=b=c=d=20;


5.Ternary operators(?):

Java includes a special ternary operator that can replace certain types of if-then-else statements.The ternary operator is a shortcut in java that lets you write an if-else statement in just one line.


Syntax: Condition? expression2 : expression3
eg:a>b?a:b


Note: condition is true expression1 is printed otherwise expression2 is printed.

A> B: This is the condition being evaluated. If A is greater than B, the expression evaluates to true. Otherwise, it evaluates to false.


?: This is the separator between the condition and the two possible outcomes.


A: This is the value assigned if the condition (A > B) is true.


B: This is the value assigned if the condition is false.


So, the entire expression can be rephrased as "If A is greater than B, then assign A, otherwise assign B."

//A java program to demonstrate Logical operators

import java.util.Scanner;
public class ApnaStudy{
    public static void main(String[] args) {
    Scanner sc =new Scanner(System.in);
    System.out.println("enter marks of students");
    int marks=sc.nextInt();
    String ReportCard= marks>=33?"pass":"fail";
    System.out.println(ReportCard);
  }
}


/*OUTPUT*/

Enter marks of students
45
pass


6. Bitwise operators:

bitwise operators in Java are special operators that work directly on the individual operators that can be applied to the integer types: long, int, short, char, and byte.

Bitwise operators manipulate each bit(0 or 1) of the binary representation of the integer.



Here are some common types of bitwise operators in java:

//A java program to demonstrate Bitwise operators

import java.util.Scanner;
public class Operators {
public static void main(String[] args) {
    int a;
    int b;
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter the value of a");
    a=sc.nextInt();
    System.out.println("Enter the value of b");
    b=sc.nextInt();
    System.out.println(~a);
    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);
    System.out.println(a>>>b);
    sc.close();
   } }


/*output*/

Enter the value of a
5
Enter the value of b
9
-6
1
13
12
2560
0
0

Previous Next