Not equals java example

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).

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).

Читайте также:  Css разделитель в горизонтальном меню

!= 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

Источник

Not Equals in Java

Not Equals in Java

This article shows how to use the != operator that we also call the not equals operator. We can also use ! with the equals() method to check the non-equality of the data.

Using the Not Equals Operator in Java

The most basic way to use the not equals operator is to check for equality between two variables.

The program has two int variables, num1 and num2 . Here, num1 contains the value 123 , and the num2 variable has 321 .

We create an if condition to check if the variables match or not. In the condition, write num1 != num2 where the variable on the left side of the operator is compared. The variable is on the right side of the operator.

The true block of the if condition executes when the condition is not met (when num1 is not equal to num2 ), and if they match, then the false block is executed.

As both the variables have different values, the true block of the condition executes.

public class JavaExample   public static void main(String[] args)    int num1 = 123;  int num2 = 321;   if (num1 != num2)   System.out.println("str1 and str2 are not equal");  > else   System.out.println("str1 and str2 are equal");  >   >  > 
str1 and str2 are not equal 

Using the Not Equals Operator With equals()

We can use the ! operator with the equals() method to check if the contents of the variables match or not.

In the example, we take two String variables. In the if condition, we check the str1.equals(str2) with a ! operator at the beginning.

The ! operator makes the result opposite, which means if the str1.equals(str2) statement returns true as a result, the operator ! makes it false.

So, in our cases, we check if the str1.equals(str2) throws true , and if yes, we use the operator, which proves that the variables are not the same.

public class JavaExample   public static void main(String[] args)    String str1 = "String A";  String str2 = "String B";   if (!str1.equals(str2))   System.out.println("str1 and str2 are not equal");  > else   System.out.println("str1 and str2 are equal");  >   >  > 
str1 and str2 are not equal 

Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.

Related Article — Java Operator

Copyright © 2023. All right reserved

Источник

Not equals java example

java not equal example

Not Equal Example in Java

  • Post author: Sagar Paliwal
  • Post published: November 10, 2021
  • Post category: Java
  • Post comments: 6 Comments

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"); >> >
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)) < if(s!=s1) < > >

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

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.

You Might Also Like

to reverse the Array

Java Program to reverse the Array with Example

Program to find Quotient and Remainder

December 26, 2021

Java Program to Find Quotient and Remainder with Example

Java Program to Add Two Complex Numbers

December 25, 2021

Java Program to Add Two Complex Numbers with Example

This Post Has 6 Comments

  1. Pingback: Java Polymorphism with Example — Developers Dome
  2. Pingback: Encapsulation in Java with Example — Developers Dome
  3. Pingback: Singleton Class in Java with Implementation and Example
  4. Pingback: Java Exception Handling with Example — Developers Dome
  5. Pingback: How To Compare Characters in Java — Developers Dome
  6. Pingback: Difference Between Float And Double Datatypes In Java

Источник

Оцените статью