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){ // I need to exit here } }
Use break:
break
while (true) { .... if (obj == null) { break; } .... }
However, if your code looks exactly like you have specified you can use a normal while loop and change the condition to obj != null:
while
obj != null
while (obj != null) { .... }
1.4m articles
1.4m replys
5 comments
57.0k users