The keyword break
in Java can be used for breaking out of a loop or switch statement. Is there anything which can be used to break from a method?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
use
return
to exit from a method.Here's another example
If you are deeply in recursion inside recursive method, throwing and catching exception may be an option.
Unlike Return that returns only one level up, exception would break out of recursive method as well into the code that initially called it, where it can be catched.
How to break out in java??
Ans: Best way:
System.exit(0);
Java language provides three jump statemnts that allow you to interrupt the normal flow of program.
These include break , continue ,return ,labelled break statement for e.g
Output:
1
Now Note below Program:
output:
Similarly you can use continue statement just replace break with continue in above example.
Things to Remember :
A case label cannot contain a runtime expressions involving variable or method calls
To add to the other answers, you can also exit a method by throwing an exception manually:
Use the
return
keyword to exit from a method.From the Java Tutorial that I linked to above: