简体   繁体   English

为什么System.out.println()不打印字符串

[英]Why System.out.println() doesn't print the string

public class Main {
    public static void main(String[] args) {
        String str1 = new String("Haseeb"); 
        String str2 = new String("Haseeb");
        System.out.println("str1==str2" + str1==str2  );
    }
}
  • output is "false" 输出为“ false”
  • I am expecting "str1==str2 false" 我期望“ str1 == str2 false”

The == operator is of lower precedence than + , so + gets execute first. ==运算符的优先级低于+ ,因此+首先执行。

"str1==str2" + str1 yields "str1==str2Haseeb" . "str1==str2" + str1产生"str1==str2Haseeb"

Then == executes, and "str1==str2Haseeb" is not the same object as "Haseeb" ( str2 ), so false is printed. 然后==执行,并且"str1==str2Haseeb""Haseeb"str2 )不是同一对象,因此打印false

You can add parentheses to clarify the desired order of operations. 您可以添加括号以阐明所需的操作顺序。

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

This should print str1==str2 false . 这应该显示str1==str2 false

(a + b == c) evaluates as (a + b) == c , not a + (b==c) , because + has higher precedence than == . (a + b == c)求值为(a + b) == c ,而不是a + (b==c) ,因为+优先级高于== Otherwise arithmetic wouldn't work. 否则算术将不起作用。

What you have there is equivalent to: 您所拥有的等同于:

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

And ("str1==str2" + str1) is not equal to str2 , so you print false . 并且("str1==str2" + str1)不等于str2 ,因此您输出false

What you probably mean is: 您可能的意思是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.out.println 不打印没有参数的行? - System.out.println doesn't print a line with no parameters? 为什么“System.out.println”在Android 中不起作用? - Why doesn't “System.out.println” work in Android? 为什么System.out.println()没有抛出NullPointerException? - Why doesn't System.out.println() throw NullPointerException? 为什么System.out.println()中的字符不增加? - Why doesn't a character increment in System.out.println()? System.out.println不在Windows Linux子系统bash中打印 - System.out.println doesn't print in Windows Linux Subsystem bash 如何使用System.out.println打印报告错误的消息,该消息显示无效参数但不存储该参数? - How to print a message reporting an error with System.out.println that shows the invalid parameter but doesn't store it? 为什么 Java 中的 System.out.println() 会打印到控制台? - Why does System.out.println() in Java print to the console? 在使用System.out.println()打印集时为什么要并发修改异常? - Why concurrentmodificationexception when print a set with System.out.println()? java swing它没有system.out.println - java swing it doesn't without system.out.println System.out.println() 快捷方式不起作用 - System.out.println() shortcut doesn't work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM