Java 中使用非 final 变量的无法执行的语句


下面是一个使用非 final 变量的无法执行的语句的示例 -

示例

class Demo_example {
   int a = 2, b = 3;
   void display_msg(){
      while (a < b){
         System.out.println("The first variable is greater than the second");
      }
      System.out.println("This is an unreachable statement");
   }
}
public class Demo{
   public static void main(String args[]){
      Demo_example my_instance = new Demo_example();
      my_instance.display_msg();
   }
}

输出

“The first variable is greater than the second” displayed infinitely

一个名为 Demo_example 的类,其中定义了两个变量。然后定义了一个名为“display_msg”的函数,并且检查这两个变量是否相等。在控制台上显示相关消息。另一个名为“Demo”的函数包含了 main 函数,其中创建了一个“Demo_example”类的实例。在这个实例上调用“display_msg”,并且在控制台上显示相关的输出。

更新于: 17-8-2020

84 浏览量

开启您的 职业生涯

通过完成课程获得认证

开始
广告
© . All rights reserved.