“abc”之间的差异+“de”和“abc” Java中的+ de(de =" de")? [英] Differences between "abc" + "de" and "abc" + de (de = "de") in Java?

查看:193
本文介绍了“abc”之间的差异+“de”和“abc” Java中的+ de(de =" de")?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行以下代码并获得评论中显示的结果。我知道 == .equals()之间的区别。我不明白的是为什么我的第二行代码与第三行代码的结果不同。

I run the following code and get the results shown in the comments. I know the differences between == and .equals(). What I don't understand is why my code in the second line has different results from the code in the third line.

    String de = "de";
//  String abcde = "abc" + "de"; // abcde == "abcde" reture true
    String abcde = "abc" + de;   // abcde == "abcde" reture false;
    System.out.println();
    System.out.println(abcde=="abcde");
    System.out.println(de=="de");

在尝试调试时,我使用了javap -c命令并得到以下输出'code'for第一个字符串连接:

In trying to debug this I used the javap -c command and got the following output 'code' for the first string concatenation:

         Code:
 0:   ldc     #9; //String de
 2:   astore_1
 3:   new     #10; //class java/lang/StringBuilder
 6:   dup
 7:   invokespecial   #11; //Method java/lang/StringBuilder."<init>":()V
 10:  ldc     #4; //String abc
 12:  invokevirtual   #12; //Method java/lang/StringBuilder.append:(Ljava/lang
 String;)Ljava/lang/StringBuilder;
 15:  aload_1
 16:  invokevirtual   #12; //Method java/lang/StringBuilder.append:(Ljava/lang
 String;)Ljava/lang/StringBuilder;
   19:  invokevirtual   #13; //Method java/lang/StringBuilder.toString:()Ljava/l
 ng/String;
   22:  astore_2
   23:  getstatic       #14; //Field java/lang/System.out:Ljava/io/PrintStream;
   26:  invokevirtual   #15; //Method java/io/PrintStream.println:()V
   29:  getstatic       #14; //Field java/lang/System.out:Ljava/io/PrintStream;
   32:  aload_2
   33:  ldc     #16; //String abcde
   35:  if_acmpne       42
   38:  iconst_1
   39:  goto    43
   42:  iconst_0
   43:  invokevirtual   #17; //Method java/io/PrintStream.println:(Z)V
   46:  getstatic       #14; //Field java/lang/System.out:Ljava/io/PrintStream;
   49:  aload_1
   50:  ldc     #9; //String de
   52:  if_acmpne       59
   55:  iconst_1
   56:  goto    60
   59:  iconst_0
   60:  invokevirtual   #17; //Method java/io/PrintStream.println:(Z)V
   63:  return

以及第二个字符串连接的输出:

And the output for the second string concatenation:

我对这个代码并不熟悉,也看不出存在这些差异的原因。那么有人可以解释为什么会出现这些差异吗?

I am not so familiar with this 'code' and can't see any reason why these differences exists. So could anyone explain why those differences happen?

相关发布

推荐答案

问题只是编译器太聪明了。当它看到abc+de时,它会立即将其连接到文字abcde中。但是当它看到abc+ de 时,不允许(根据Java规则)将其优化为文字,但必须改为实现 + 函数,创建一个新的String对象。

The "problem" is simply that the compiler is too smart for you. When it sees "abc" + "de" it immediately concatenates that into the literal "abcde". But when it sees "abc" + de it's not allowed (per Java rules) to "optimize" that to a literal but must instead implement the + function, creating a new String object.

字符串文字总是作为实习字符串处理,所以 == 将适用于他们。

String literals are always handled as interned Strings, so == will work on them.

这篇关于“abc”之间的差异+“de”和“abc” Java中的+ de(de =&quot; de&quot;)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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