Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing two identical strings with == returns false [duplicate]

Tags:

java

string

I am making an archive for my family. There are no syntax errors, however whenever I type in "Maaz", it evaluates realName == "Maaz" to false and goes to the else statement.

import java.util.Scanner;  public class MainFamily {     public static void main (String [] args) {          System.out.println("Enter you're name here");         Scanner name = new Scanner(System.in);//Scanner variable = name          String realName;         realName = name.nextLine();//String variable = user input         System.out.println("Name: "+ realName);          if (realName == "Maaz") {              System.out.println("Name: Maaz");          } else {             System.out.println("This person is not in the database");         }     } }        
like image 724
Maaz Mahmood Avatar asked Sep 10 '11 23:09

Maaz Mahmood


People also ask

What happens when you compare two strings with the == operator?

In String, the == operator is used to comparing the reference of the given strings, depending on if they are referring to the same objects. When you compare two strings using == operator, it will return true if the string variables are pointing toward the same java object. Otherwise, it will return false .

Can you use == to compare two strings?

You should not use == (equality operator) to compare these strings because they compare the reference of the string, i.e. whether they are the same object or not. On the other hand, equals() method compares whether the value of the strings is equal, and not the object itself.

Why does == evaluate to false in Java?

This is because the two Integer objects have the same value, but they are not the same object.

When comparing strings Why is == not a good idea?

Using the “==” operator for comparing text values is one of the most common mistakes Java beginners make. This is incorrect because “==” only checks the referential equality of two Strings, meaning if they reference the same object or not.


2 Answers

TL;DR

You wrote (this doesn't work):

realName == "Maaz"

You meant this:

realname.equals("Maaz")

or this:

realname.equalsIgnoreCase("Maaz")

Explanation

In Java (and many other Object-Oriented programming languages), an object is not the same as a data-type. Data-types are recognized by the runtime as a data-type.

Examples of data-types include: int, float, short.

There are no methods or properties associated with a data-type. For example, this would throw an error, because data-types aren't objects:

int x = 5; int y = 5; if (x.equals(y)) {     System.out.println("Equal"); } 

A reference is basically a chunk of memory that explicitly tells the runtime environment what that data-block is. The runtime doesn't know how to interpret this; it assumes that the programmer does.

For example, if we used Integer instead of int in the previous example, this would work:

Integer x = new Integer(5); Integer y = new Integer(5); if (x.equals(y)) {     System.out.println("Equal"); } 

Whereas this would not give the expected result (the if condition would evaluate to false):

Integer x = new Integer(5); Integer y = new Integer(5); if (x == y) {     System.out.println("Equal"); } 

This is because the two Integer objects have the same value, but they are not the same object. The double equals basically checks to see if the two Objects are the same reference (which has its uses).

In your code, you are comparing an Object with a String literal (also an object), which is not the same as comparing the values of both.

Let's look at another example:

String s = "Some string"; if (s == "Some string") {     System.out.println("Equal"); } 

In this instance, the if block will probably evaluate to true. Why is this?

The compiler is optimized to use as little extra memory as is reasonable, although what that means depends on the implementation (and possibly runtime environment).

The String literal, "Some string", in the first line will probably be recognized as equivalent to the String literal in the second line, and will use the same place in memory for each. In simple terms, it will create a String object and plug it into both instances of "Some string". This cannot be relied upon, so using String.equals is always a better method of checking equivalence if you're only concerned with the values.

like image 84
beatgammit Avatar answered Sep 18 '22 12:09

beatgammit


do this instead

if (realName.equals("Maaz")) 

equals() should be used on all non-primitive objects, such as String in this case

'==' should only be used when doing primitive comparisons, such as int and long

like image 34
Shawn Avatar answered Sep 19 '22 12:09

Shawn