Java 中 == 的奇怪行为

发布于 2025-01-03 12:09:12 字数 603 浏览 2 评论 0原文

我在 java 中观察到一个奇怪的行为 == 运算符。我正在尝试打印输出,如下所示

String str1 = "Rajesh";
String str2 = "Rajesh";
System.out.println("Using equals() str1 and str2 Equals :"
            + str1.equals(str2));
System.out.println("Using == str1 and str2 Equals :" 
            + str1 == str2);

第一个 SOP 语句打印

使用 equals() str1 和 str2 等于 :true

并且下一个 SOP 仅打印 false 。

我尝试在 Eclipse 和 Net Beans 中进行编译,但结果是相同的。 我很困惑为什么

使用 == str1 和 str2 等于:

未打印

帮我解决这个问题

提前致谢,

Raj

I observed a strange behavior == operator in java. I am trying to print the out put as follows

String str1 = "Rajesh";
String str2 = "Rajesh";
System.out.println("Using equals() str1 and str2 Equals :"
            + str1.equals(str2));
System.out.println("Using == str1 and str2 Equals :" 
            + str1 == str2);

The first SOP statement printing

Using equals() str1 and str2 Equals :true

and the next SOP printing only false .

I tried compiling in both eclipse and Net Beans but result is the same .
I am so confused why

Using == str1 and str2 Equals :

is not printing

Help me out in this

Thanks in advance,

Raj

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

望她远 2025-01-10 12:09:12

它与 ("Using == str1 and str2 Equals :" + str1) == str2 相同,当然这是错误的。表达式从左到右解析,因此首先连接 "Using == str1 and str2 Equals :"str1,然后应用 == 运算符。

it's the same as ("Using == str1 and str2 Equals :" + str1) == str2 and this is false, of course. Expression is parsed from left to right and so at first it concatenates "Using == str1 and str2 Equals :" and str1, then applies == operator.

半山落雨半山空 2025-01-10 12:09:12

有关运算符优先级表,请参阅 http://bmanolov.free.fr/javaoperators.php爪哇。

+ 运算符的优先级高于 == 运算符。

因此,实际上,您的代码等效于以下内容:

System.out.println( ("Using == str1 and str2 Equals :" + str1) == str2);

请注意我添加的括号的位置。它的计算结果如下:

System.out.println( (str_x + str1) == str2);

然后是这样:

System.out.println( str_y == str2 );

然后是这样:

System.out.println( false );

为了获得您想要的结果,您必须使用括号来指定您希望在 + 运算符之前解析 == 运算符:

System.out.println( "Using == str1 and str2 Equals :" + (str1 == str2));

注意新的放置位置括号。

See http://bmanolov.free.fr/javaoperators.php for a table of operator precedence in Java.

The + operator is higher precedence than the == operator.

So, in effect, your code is equivalent to the following:

System.out.println( ("Using == str1 and str2 Equals :" + str1) == str2);

Note the placement of the parentheses that I added. It evaluates to this:

System.out.println( (str_x + str1) == str2);

And then to this:

System.out.println( str_y == str2 );

And then to this:

System.out.println( false );

In order to get the result you want, you must use parentheses to specify that you want the == operator to be resolved BEFORE the + operator:

System.out.println( "Using == str1 and str2 Equals :" + (str1 == str2));

Notice the new placement of the parentheses.

箜明 2025-01-10 12:09:12

因为 + 与 = 相比具有更高的优先级,并且如果您使用括号(str1 == str2) 那么
这个结果为 true,因为最高优先级是 (.
所以首先它检查括号内的数据。

String str1 = "Rajesh";
        String str2 = "Rajesh";
        System.out.println("Using equals() str1 and str2 Equals :"
                + str1.equals(str2));
        System.out.println("Using == str1 and str2 Equals :" 
                + (str1 == str2));

输出:

Using equals() str1 and str2 Equals :true
Using == str1 and str2 Equals :true

Because + has higher priority compare to = and if you use bracket(str1 == str2) then
this result give true because highest priority is (.
So First it checks bracket inside data.

String str1 = "Rajesh";
        String str2 = "Rajesh";
        System.out.println("Using equals() str1 and str2 Equals :"
                + str1.equals(str2));
        System.out.println("Using == str1 and str2 Equals :" 
                + (str1 == str2));

Output:

Using equals() str1 and str2 Equals :true
Using == str1 and str2 Equals :true
想你的星星会说话 2025-01-10 12:09:12

也许是操作顺序的问题?尝试:

System.out.println("Using == str1 and str2 Equals :" + (str1 == str2));

Maybe an order of operations thing? Try:

System.out.println("Using == str1 and str2 Equals :" + (str1 == str2));
冷弦 2025-01-10 12:09:12

尝试用 () 包围它,如下所示:

System.out.println("Using == str1 and str2 Equals :" + (str1 == str2));

Try surrounding it with () like this:

System.out.println("Using == str1 and str2 Equals :" + (str1 == str2));
宁愿没拥抱 2025-01-10 12:09:12

equals 方法当且仅当 x 和 y 引用同一个对象时才返回 true。Follwoing 是 equals 方法的 Object 类实现。

public boolean equals(Object obj) {
    return (this == obj);
    }

在 String 类中,此方法已被重写,如下所示。

public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    if (anObject instanceof String) {
        String anotherString = (String)anObject;
        int n = count;
        if (n == anotherString.count) {
        char v1[] = value;
        char v2[] = anotherString.value;
        int i = offset;
        int j = anotherString.offset;
        while (n-- != 0) {
            if (v1[i++] != v2[j++])
            return false;
        }
        return true;
        }
    }
    return false;
    } 

如果您使用 == 运算符,它只需检查两个引用是否具有相同的对象。类似于Object类的equals方法。

equals method returns true if and only if x and y refer to the same object.Follwoing is the Object class implementation of equals method.

public boolean equals(Object obj) {
    return (this == obj);
    }

In String class this method has overridden as following.

public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    if (anObject instanceof String) {
        String anotherString = (String)anObject;
        int n = count;
        if (n == anotherString.count) {
        char v1[] = value;
        char v2[] = anotherString.value;
        int i = offset;
        int j = anotherString.offset;
        while (n-- != 0) {
            if (v1[i++] != v2[j++])
            return false;
        }
        return true;
        }
    }
    return false;
    } 

And if you use == operator it just check both references are having same object. Similar to the Object class equals method.

ヅ她的身影、若隐若现 2025-01-10 12:09:12

原因是 Java 中不能使用 == 来比较字符串。

在 C++ 或 C#(或支持运算符重定义的其他语言)中,您可以覆盖 == 运算符以提供该功能。 Java 不支持这一点。

The reason is that you cannot compare strings in Java using ==.

In C++ or C# (or other languages supporting operator redefinition), you can overwrite the == operator to provide that functionality. Java does not support that.

花想c 2025-01-10 12:09:12

str1.equals(str2) 返回 true 因为 equals() 函数比较字符串变量的内容,其中 == 运算符比较实例。由于 str1str2String 类实例的两个差异,因此它返回 false

str1.equals(str2) returns true because the equals() function compares the content of the string variables, where as == operator compares the instances. Since str1 and str2 are two differences of instances of String class, it returns false

哎呦我呸! 2025-01-10 12:09:12

在Java中,==运算符匹配两个对象,即它们的地址,而.equals()方法对两个对象的值进行数学计算,这就是为什么你得到true 代表 equals()false 代表 ==,因为两者是不同的对象。

In Java == operator matches the two objects i.e their address while .equals() method mathces the values of both objects, thats why you are getting true for equals() and false for == as both are different objects.

姜生凉生 2025-01-10 12:09:12

== 只能用于比较原始数据类型。要比较对象,您需要使用 equals 方法。在对象上使用 == 运算符实际上是比较地址而不是值。

== can only be used to compare primitive datatypes. To compare objects you need to use equals method. Using a == operator on objects actually compares there addresses instead of values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文