What is the best way to exit/terminate a while loop in Java?
For example, my code is currently as follows:
while(true){
if(obj == null){
You can use "break", already mentioned in the answers above. If you need to return some values. You can use "return" like the code below:
while(true){
if(some condition){
do something;
return;}
else{
do something;
return;}
}
in this case, this while is in under a method which is returning some kind of values.