Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Monday, November 23, 2020

Java - String not equals Examples

1. Overview

In this article, You'll learn how to compare the string contents with == and does not compare with != operator.

How to fix != comparison in two ways. Read till end of this tutorial, you will get better understanding for sure.

string-does-not-equal-java


2. Java String !=

If you are new to programming, you might have written the code for string comparison as below and does not work properly.

we have added getValidationStatus() method that returns "Success" if the given age greater or equals to 18 else should return "Failure".

Let us see the output of this program.

package com.javaprogramto.strings.compare;

public class StringDoesnotEqual {

	public static void main(String[] args) {
		
		String status = getValidationStatus(10);
		System.out.println("status :  "+status);
		
		if (status != "Failure") {
			System.out.println("Valid age");
		} else {
			System.out.println("Invalid age");
		}

	}

	public static String getValidationStatus(int age) {

		if (age < 18) {
			return new String("Failure");
		}

		return new String ("Success");
	}
}
 

Output:

status :  Failure
Valid age
 

See the output. status value is returned "Failure" but it went inside the if condition and printed the valid age string.

But, actually execution should go inside the else block.

3. How to fix status != "Failure" and use the right string comparison

First, understand the issue with the above approach. There when we use != operator with String, it compares the memory address of these two string. In our case, both strings are having different addresses. So, the condition is becoming true and printing the irrelevant onto the console.

!= checks only the string address are different or not but not string contents.

To fix the string comparison issue, you should use the method which does the comparing the contents.

Correct method: equals()

Let us correct the above problematic example using equals() method.

if (!status.equals("Failure")) {
	System.out.println("Valid age");
} else {
	System.out.println("Invalid age");
}
 

Output:

Rerun the sample code and see the output.

status :  Failure
Invalid age
 

Now, !status.equals("Failure") compares the contents using equals() method  and returns a boolean value. Next, negation ! operator is applied on boolean result.

So, status value is "Failure" and condition becomes false. Hence, it has executed the else block of if condition.

4. How fix String != java? in another way using != operator

As compared to the above method using equals() method, there is another approach to resolve the issue.

Manu of the developers do not know the deep understanding of String. If you want to surprise them, you must try out this way.

Instead of equals() method use the intern() method on status string along with the !=.

When intern() method is called it checks the same value is present in the string constant pool. If present, it fetches the address ref from there and use it for comparison.

Make the change on the string.intern() method.

if (status.intern() != "Failure") {
	System.out.println("Valid age");
} else {
	System.out.println("Invalid age");
}
 

Output:

status :  Failure
Invalid age
 

This solution works amazingly and impress your team fellow programmers.

5. Conclusion

In this tutorial, you've seen how to compare strings using != operator. If this != does not work, how to compare the contents using equals() and intern() methods.

GitHub

Read Next:

What is the difference between == and equals() method with String Objects comparison?

String equals() method examples

String intern() method examples

Stackoverflow ref

No comments:

Post a Comment

Please do not add any spam links in the comments section.