溫馨提示×

java如何結束當前方法

小億
111
2024-09-03 08:35:21
欄目: 編程語言

在Java中,要結束當前方法,可以使用return關鍵字。return關鍵字可以用于立即結束當前方法的執行,并返回到調用該方法的地方。以下是一個簡單的示例:

public class Test {
    public static void main(String[] args) {
        System.out.println("Before calling the method");
        endMethodExample();
        System.out.println("After calling the method");
    }

    public static void endMethodExample() {
        System.out.println("Inside the method");
        return; // 結束當前方法
        System.out.println("This line will not be executed");
    }
}

在這個示例中,endMethodExample()方法中的return語句會導致方法提前結束,因此"This line will not be executed"這行代碼不會被執行。當你運行這個程序時,輸出將是:

Before calling the method
Inside the method
After calling the method

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女