Java Program on Equal To and Not Equal To Operator

In the previous article, we have discussed about Java Program on Modulo Operator

In this article we will see the use of equal to and not equal to operator in Java programming language.

Java Program on Equal To and Not Equal To Operator

Both equal to and not equal to operators are relational operator and returns Boolean value True or False.

Equal To Operator:

Equal To operator is used to check if values of two operands are equal. It is represented by the symbol ==

Syntax: operand == operand

Program:

import java.util.Scanner;

class Main
{
    public static void main(String[] args)
    {
        //Scanner class object created
        Scanner in=new Scanner(System.in);
        //Taking input of 2 numbers
        System.out.println("Enter any two numbers: ");
        int n1=in.nextInt();
        int n2=in.nextInt();
        //checking if two numbers are equal
        if(n1==n2)
        {
        	System.out.println("Two numbers are equal");
        }
        else
        {
          	System.out.println("Two numbers are not equal");  
        }

    }
}
Output:

Case-1
Enter any two numbers: 
123
123
Two numbers are equal

Case-2
Enter any two numbers: 
123
124
Two numbers are not equal

Not Equal To Operator:

Not Equal To operator is used to check if values of two operands are not equal means different. It is represented by the symbol !=

Syntax: operand != operand

Program:

import java.util.Scanner;

class Main
{
    public static void main(String[] args)
    {
        //Scanner class object created
        Scanner in=new Scanner(System.in);
        //Taking input of 2 numbers
        System.out.println("Enter any two numbers: ");
        int n1=in.nextInt();
        int n2=in.nextInt();
        //checking if two numbers are different
        if(n1!=n2)
        {
        	System.out.println("Two numbers are not equal");
        }
        else
        {
          	System.out.println("Two numbers are equal");  
        }

    }
}
Program:

Case-1
Enter any two numbers: 
22
56
Two numbers are not equal

Case-2
Enter any two numbers: 
22
56
Two numbers are not equal

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Related Java Programs: