You are currently viewing Not Equal Example in Java

Not Equal Example in Java

In this tutorial, you will learn about java example that is not equal. Before we get into the examples, we’ll go over how to write the not equals sign in Java, what the!= operator is?

In Java, how do you write the not equals sign?

The not equal sign is represented as !=

What is the not equals operator (!=)?

The not-equal operator: !=, is the opposite, evaluating to true if the values are not equal. == and!= are typically used with primitives like int and boolean, but not with objects like String and Color.

The != (not equal to) operator is the complete opposite of the equality(==) operator. If the values of the two operands differ, it will evaluate to be true. It’s a relational operator, as the name implies. The boolean value is always returned by the!= operator (true or false).

What is the difference between!= and !a.equals?(b).

The primary difference between != and equals is that “==” is used to compare primitives, whereas the equals() method is recommended for checking object equality. The same is true for not being equal.

1: Java not Equal to example

Here are some!= Java examples to help you better understand how to use this operator. We’ll start with some simple examples using primitive types. Not Equal.java

public class NotEqualExample {
public static void main (String args[]) {
    byte b1=1;
    byte b2=2;
    int i1=3;
    int i2=3;
    short s1=5;
    short s2=6;
    long l1=7;
    long l2=8;
    float f1=9.1f;
    float f2=10.1f;
    double d1=11.01;
    double d2=11.01;
    String s1="Developers ";
    String s2="Dome";
     
    if(b1!=b2) {
        System.out.println("true");
    }else {
        System.out.println("false");
    }
     
    if(i1!=i2) {
        System.out.println("true");
    }else {
        System.out.println("false");
    }
     
    if(s1!=s2) {
        System.out.println("true");
    }else {
        System.out.println("false");
    }
     
    if(l1!=l2) {
        System.out.println("true");
    }else {
        System.out.println("false");
    }
     
    if(f1!=f2) {
        System.out.println("true");
    }else {
        System.out.println("false");
    }
     
    if(d1!=d2) {
        System.out.println("true");
    }else {
        System.out.println("false");
    }
     
    if(s1!=s2) {
        System.out.println("true");
    }else {
        System.out.println("false");
    }
}
}

Output:

true // byte
false // int
true //short
true //long
true //float
false //double
true //string

2: Another example of not equal operator

Here’s an example of objects that aren’t equal. Not Equal Objects.java

public class NotEqualObjectsExample {
    static class Sample {
 
        private String name ;
        private int X;
        private int Y;
 
        public Car(String name, int X, int Y) {
            this.name = name;
            this.X = X;
            this.Y = Y;
        }
    }
    public static void main (String args[]) {
      Sample s =new Sample("DevelopersDome", 10000, 500);
        Sample s1=new Sample("DevelopersDome1", 20000, 1000);
         
         
        if(!s.equals(s1)) {                               <-------
            System.out.println("There is a difference");
        }
        if(s!=s1) {                                        <-------
            System.out.println("There is a difference");
        }
    }
}

Output:

There is a difference
There is a difference

An example of an override method can be found here. Overriding is a feature that allows a subclass or child class to implement a method that is already provided by one of its super-classes or parent classes. This new method has the same name, parameters or signature, and return type as its super-method. class Not Equal Override.java

3: Example-When the operands are objects, (e.g String)

The equals() method in the String class is used to compare two strings. If the strings being compared are equal, the equals() method returns true; otherwise, it returns false.

Put an exclamation mark at the beginning of the statement to do the opposite, for example:
!str1.equals(str2)

String str1 = “Developers“;
String str2 = “Dome“;
boolean notEqual = !str1.equals(str2);
System.out.println( notEqual ); //true
public class ObjectNotEqualExample {
    public static void main(String args[]) {
        Student s1 = new Student("Louis", 222, "xx");
        Student s2 = new Student("Losii", 333, "xy");
        Student s3 = s1;
        System.out.println(!s1.equals(s2));// true
        System.out.println(!s1.equals(s3));// false
    }
    
}
class Student {
        private String name;
        private int rollNo;
        private String gender;
        
        public Student(String name, int rollNo, String marks) {
            this.name = name;
            this.rollNo = rollNo;
            this.marks = marks;
        }
}

You may like:

java not equal,java not equal,java not equal,java

Java Inheritance with Example | Types of inheritance

Java final Keyword with Example

Hope this article will guide you to recognize all about the Not Equal Example in Java that you needed and still if you have any problem or queries regarding this, post them in the comments section and we will be glad to assist you.

This Post Has 6 Comments

Leave a Reply