Java 中有 true 和 false 关键字吗?

java 8object oriented programmingprogramming

关键字 − Java 中的关键字向编译器传达了特殊含义,因此不能用作标识符。Java 提供了一组 50 个关键字。

abstractcontinuefornewswitch
assertdefaultgotopackagesynchronized
booleandoifprivatethis
breakdoubleimplementsprotectedthrow
byteelseimportpublicthrows
caseenuminstanceofreturntransient
catchextendsintshorttry
charfinalinterfacestaticvoid
classfinallylongstrictfpvolatile
constfloatnativesuperwhile

保留字 − 在上述关键字列表中,关键字 goto 和 const 目前未使用。它们是保留字(供将来使用)。

true false 和 null − True、false 和 null 表示 Java 中的某些值,它们用作文字。它们不被视为关键字。

但是,您不能在 Java 中将它们用作标识符,如果您尝试这样做,将产生编译时错误。

示例

public class Example {
   public static void main(String args[]) {
      int true = 20;
      int false = 30;
      float null = 23.6f;
   }
}

输出

Example.java:3: error: not a statement
   int true = 20;
   ^
Example.java:3: error: ';' expected
   int true = 20;
       ^
Example.java:4: error: not a statement
   int false = 30;
   ^
Example.java:4: error: ';' expected
   int false = 30;
       ^
Example.java:5: error: not a statement
   float null = 23.6f;
   ^
Example.java:5: error: ';' expected
   float null = 23.6f;
         ^
6 errors

相关文章