Core Java

Java not equal Example

In this article, we will show you a Java not equal Example. Firstly we will talk about what is != in Java general, and for what reason we use it. After that, we will do some examples of how we use it.

1. Introduction

Java has a rich set of operators which is used to manipulate variables. This set we can divide them into six groups :

  • Arithmetic Operators
  • Relational Operators
  • Bitwise Operators
  • Logical Operators
  • Assignment Operators
  • Misc Operators

The operator that we will analyze in this article is a relational operator. It is symbolized "!=" or "(!a.equals(b))" and checks if the values of two operands are equal or not, in case that values are not equal then condition becomes true. After the comparison, this operator returns a boolean value(true or false). The type of each operand that we use can be anything, for example, String, integer, double, short etc but when we compare them the type must be the same.

2. Technologies used

The example code in this article was built and run using:

  • Java 1.8.231(1.8.x will do fine)
  • Eclipse IDE for Enterprise Java Developers- Photon

3. Difference between != and !a.equals(b).

The main difference between == and equals is that “==” is used to compare primitives while equals() method is recommended to check equality of objects. The goes for not equal.

4. Java not equal Examples

Here we show you some examples about != Java to understand better the use of this operator. First, we do some examples with some primitive types.

Not_Equal.java

public class Not_Equal {
public static void main (String args[]) {
	byte b1=1;
	byte b2=2;
	int in1=3;
	int in2=3;
    short sh1=5;
    short sh2=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="Good ";
    String s2="Morning";
	
	if(b1!=b2) {
		System.out.println("true");
	}else {
		System.out.println("false");
	}
	
	if(in1!=in2) {
		System.out.println("true");
	}else {
		System.out.println("false");
	}
	
	if(sh1!=sh2) {
		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
false
true
true
true
false
true

Here we do one example for not equal with objects.

Not_Equal_Objects.java

public class Not_Equal_Objects {
	static class Car {

        private String name ;
        private int cc;
        private int speed;

        public Car(String name, int cc, int speed) {
            this.name = name;
            this.cc = cc;
            this.speed = speed;
        }
	}
	public static void main (String args[]) {
		Car a=new Car("Mercedes-Benz", 1400, 250);
		Car b=new Car("Fiat", 1200, 220);
		
		
		if(!a.equals(b)) {
			System.out.println("It is not the same car");
		}
		if(a!=b) {
			System.out.println("It is not the same car");
		}
	}
}

Output

 It is not the same car
It is not the same car 

As you can see in lines 19 and 22 we can use both versions.

Here you can see an example with an override method. Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes. This new method has the same name, same parameters or signature and same return type as a method in its super-class.

Not_Equal_Override.java

	public class Not_equal_override {
		static class Car {

	        private static String name ;
	        private int cc;
	        private int speed;

	        public Car(String name, int cc, int speed) {
	            this.name = name;
	            this.cc = cc;
	            this.speed = speed;
	        }
		
		@Override
		public boolean equals (Object o) {
		Car car= (Car) o;
		if(this.name.equals(car.name)) {
			return true;
		}
		return false;
		}	
		}
		
		public static void main (String args[]) {
			
			Car b=new Car("Fiat", 1200, 220);
			Car c=new Car("Fiat", 1234, 144);
			
			if(!b.equals(c)) {
				System.out.println("The cars have not the same name");
			}else {
				System.out.println("The cars have the same name");
			}
		}
	}

Output

 The cars have the same name 

In the above code, you can see how to do an override not equal method so that we can compare only the name of the cars.

6. Download the Complete Source Code

Download
You can download the full source code of this example here: Java not equal Example

Last updated on Aug. 10th, 2021

Ioannis Makrygiannakis

John is an undergraduate student at the Department of Informatics of Athens University of Economics and Business. He is specialized in Databases, Knowledge Management, Information Systems and Information Security. In his free time he loves learning new things with regard to programming and network security.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Daniel Escasa
4 years ago

You can print Boolean values directly, so that instead of

if(b1!=b2) {
System.out.println(“true”);
}else {
System.out.println(“false”);

you can do:

System.out.println(b1 != b2);

Back to top button