首页 > 文章列表 > 在Java中使用非最终变量的不可达语句

在Java中使用非最终变量的不可达语句

Java非最终变量 不可达语句 使用编程
290 2023-08-20

Following is an example, wherein we will see unreachable statement using the non-final variable −

Example

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'的函数 defined, and the two variables are checked for their equality. Relevant message is displayed on the 控制台。另一个名为'Demo'的函数包含主函数,在主函数中创建了一个实例 ‘Demo_example’类已创建。在该实例上调用‘display_msg’,并输出相关结果 在控制台上显示。