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 瀏覽量

開啟您的 職業生涯

透過完成課程獲得認證

開始
廣告