开发者

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

This question already has answers here: How do I compare strings in Java? (23 answers) Closed 9 years ago.

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 Scann开发者_如何学Cer(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");
        }
    }
}       


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.


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


use

if(realName.equals("Maaz"))

use == with primitive data type like int boolean .... etc

but if you want to compare object in java you should use the equals method


You have to compare objects with realName.equals ("Maaze"), not with ==.


It is best practice to compare Strings using str.equals(str2) and not str == str2. As you observed, the second form doesn't work a lot of the time. By contrast, the first form always works.

The only cases where the == approach will always work are when the strings are being compared are:

  • string literals or references to string literals, or
  • strings that have been "interned" by application-level code calling str = str.intern();.

(And no, strings are not interned by default.)

Since it is generally tricky to write programs that guarantee these preconditions for all strings, it is best practice to use equals unless there is a performance-related imperative to intern your strings and use ==.

Before that you decide that interning is a good idea, you need to compare the benefits of interning with the costs. Those costs include the cost of looking up the string in the string pool's hash table and the space and GC overheads of maintaining the string pool. These are non-trivial compared with the typical costs of just using a regular string and comparing using equals.


You can also use

realname.equalsIgnoreCase("Maaz")

This way you can accept Maaz, maaz, maaZ, mAaZ, etc.


== tests shallow equality. It checks if two objects reference the same location in memory.


Intriguing. Although, as others have stated, the correct way is to use the .equals(...) method, I always thought strings were pooled (irrespective of their creation). It seems this is only true of string literals.

final String str1 = new String("Maaz");
final String str2 = new String("Maaz");
System.out.println(str1 == str2); // Prints false

final String str3 = "Laaz";
final String str4 = "Laaz";
System.out.println(str3 == str4); // Prints true


Since you are working on strings, you should use equals to equalsIngnorecase method of String class. "==" will only compare if the both objects points to same memory location, in your case, both object are different and will not be equal as they dont point to same location. On the other hand, equals method of String class perform a comparison on the basis of the value which objects contains. Hence, if you will use equals method, your if condition will be satisfied.


== compares object references or primitive types (int, char, float ...)

equals(), you can override this method to compare how both objects are equal.

for String class, its method equal() will compare the content inside if they are the same or not.

If your examples, both strings do not have the same object references, so they return false, == are not comparing the characters on both Strings.


It seems nobody yet pointed out that the best practice for comparing an object with a constant in Java is calling the equals method of the constant, not the variable object:

if ("Maaz".equals (realName)) {}

This way you don't need to additionally check if the variable realName is null.


if(realName.compareTo("Maaz") == 0) {
    // I dont think theres a better way do to do this.
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜