使用==比较两个相同的字符串将返回false [英] Comparing two identical strings with == returns false

查看:115
本文介绍了使用==比较两个相同的字符串将返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的家人制作档案。没有语法错误,但每当我输入Maaz时,它会评估 realName ==Maaz to false并转到 else 语句。

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");
        }
    }
}       


推荐答案

TL; DR



你写的(doens't work):

TL;DR

You wrote (doens't work):

realName ==Maaz

你的意思是:

realname.equals(Maaz)

或者:

realname.equalsIgnoreCase(Maaz)

在Java(以及许多其他面向对象的编程语言)中,对象与数据类型不同。数据类型被运行时识别为数据类型。

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.

数据类型的示例包括:int,float,short。

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.

例如,如果我们在前面的例子中使用Integer而不是int,那么这将起作用:

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");
}

然而这不会给出预期的结果(if条件会评估为false ):

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");
}

这是因为两个Integer对象具有相同的值,但它们不是同一个对象。 double equals基本上检查两个对象是否是相同的引用(有其用途)。

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.

让我们看另一个例子:

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

在这个例子中,if块可能会评估为true。这是为什么?

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

第一行中的字符串文字Some string可能会被识别为等效于字符串字面值第二行,并将在内存中使用相同的位置。简单来说,它将创建一个String对象并将其插入Some string的两个实例中。这是不可靠的,因此如果您只关注值,使用String.equals总是更好的检查等价的方法。

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.

这篇关于使用==比较两个相同的字符串将返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆