首页 > 解决方案 > 布尔变量

问题描述

我以为布尔的默认值是假的?为什么它会打印真实的陈述呢?我的输出是再见


public class Test {

public static void main (String [] args) {

if(false)
    System.out.print("hello");

else System.out.print("goodbye");
}
}

标签: javaif-statementboolean

解决方案


您的代码不使用布尔值的默认值。你总是 print System.out.print("goodbye");,因为这部分是真的。为此,请使用以下代码


public class Test {
    static boolean defaultValue;
public static void main(String[] args) {
System.out.println("Default value is "+defaultValue); if(defaultValue) System.out.println("hello"); else System.out.println("goodbye"); } }


推荐阅读