Can a for loop be written in Java to create an infinite loop or is it only while loops that cause that problem?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Is there a limit to how many levels you can nest i
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
Just for fun (and this too long for a comment): a lot of people will be very surprised to learn that for a lot of very practical purposes the following is nearly an infinite loop:
If the thread executing this loops can do 4 billions cycles per second and can do the increment and the check in one cycle (quite beefy for a single thread) and if my maths ain't totally off, I think the above code needs about 150 years to execute : )
There is also this one, to complete the topic:
The way I made my server to run as long until I shut it down is
for(int i = 0;i<-1;i++){//code here}
You can also do such with for loops. E.g. the following is identical to
while(true)
:I'll just add this since nobody did this version:
you can declare a subpart of the code to be another part in a for loop, example -
}
it is almost in infinite loop; if you change int to long, and add more variables, you can practically make it last 25 x 10^12 minutes long