无法比较java中的字符串

0 java string if-statement boolean

为什么这会回归真实?

String b =  "(5, 5)";
String a =  "(7, 8)" ;
if(a.equals(b));
{

    System.out.printf("Somehow \"%s\" and \"%s\" are the same" ,a,b);
}
Run Code Online (Sandbox Code Playgroud)

输出:

Somehow "(7, 8)" and "(5, 5)" are the same
Run Code Online (Sandbox Code Playgroud)

Mar*_*oun 5

您的代码相当于:

if(a.equals(b)) { }
{
   System.out.printf("Somehow \"%s\" and \"%s\" are the same" ,a,b);
}
Run Code Online (Sandbox Code Playgroud)

因此,块中的print语句将始终执行,而不管条件如何,因为该主体if不包含该语句.

JLS - 14.6.空声明:

空语句什么都不做.

EmptyStatement:

; 
Run Code Online (Sandbox Code Playgroud)

执行空语句总是正常完成

  • 啊..语言规范. (2认同)