not equal example : (opposite of .equals java)

In this post, I will be sharing not equal example in Java. Before moving on to the examples, first, we will understand how do you write the not equals sign in Java, what is != operator, the difference between != and !a.equals(b).

Read Also: Difference between == and equals method in Java

How do you write the not equals sign in Java?


!=

What is != operator?


!=(pronounced not equal to) is the opposite of the equality(==) operator. It will evaluate to true if the values of the two operands are different. It is a relational operator. != operator always returns the boolean value (true or false).

!= operator when operands are primitives(int, long, float, double)


When you are using primitive data types (int, long, float, double) then use the operator != to test that the primitive x is not equal to the another primitive y as shown below in the example:

public class PrimitiveNotEqualToExample {
    public static void main(String args[]) {
      int x=10;
      int y=25;
      System.out.println( x!=y );// true
      long l1 = 24l;
      long l2 = 26l;
      System.out.println( l1!=l2 );// true
      double d1 = 23.0d;
      double d2 = 23.0d;
      System.out.println( d1!=d2 );// false
      byte b1 = 2;
      byte b2 = 5;
      System.out.println( b1!=b2 );// true
      short s1 = 12;
      short s2 = 13;
      System.out.println( s1!=s2 );// true
      float f1 = 12.0f;
      float f2 = 12.0f;
      System.out.println( f1!=f2 );// false
    }
}

When operands are objects (e.g String)


String class contains equals() method to compare one string to another. equals() method returns true if the strings compared are equal, otherwise false.
To do the opposite just put an exclamation mark at the start of the statement, for example !str1.equals(str2)

      String  str1 = "Java";
      String  str2 = "Hungry";
      boolean notEqual = !str1.equals(str2); 
      System.out.println( notEqual ); //true

Given below is the example to compare objects

public class ObjectNotEqualExample {
    public static void main(String args[]) {
        Student s1 = new Student("John", 123, "Male");
        Student s2 = new Student("Alexa", 234, "Female");
        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 gender) {
            this.name = name;
            this.rollNo = rollNo;
            this.gender = gender;
        }
}

Difference between != and !x.equals(y) method

The main difference between the != relational operator and !x.equals(y) method is that != is used for primitives whereas the equals method is used to compare Objects.

That's all for today, please mention in the comments in case you have any questions related to not equal example in Java.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry