filmov
tv
Breaking a code block in Java

Показать описание
A obscure part of the Java syntax allows you to break out of a block of code. What does this look like and why might it have been used in the past.
[Transcript]
Hello. My name is Peter Lawrey.
Today I'm looking at an obscure part of the Java syntax which is labels on blocks of code.
As these are obscure most developers avoid writing code like this but you may come across it in the wild.
Let's take this line by line.
The code initializes an array of int values to process.
This line has a label on it which labels the block of code which you will note is not a loop.
This is a pattern I've sometimes used in the past that require special handling when a condition is not met.
In this example, I loop over the array and check for some condition. If any element is okay I break out of the block as no special handling is required.
If the loop finishes normally I need the special handling otherwise I continue to execute.
There are other ways of writing this and until streams came along it was still one of the cleanest ways of implementing special handling however now it's more likely that a Stream is a more appropriate.
[Transcript]
Hello. My name is Peter Lawrey.
Today I'm looking at an obscure part of the Java syntax which is labels on blocks of code.
As these are obscure most developers avoid writing code like this but you may come across it in the wild.
Let's take this line by line.
The code initializes an array of int values to process.
This line has a label on it which labels the block of code which you will note is not a loop.
This is a pattern I've sometimes used in the past that require special handling when a condition is not met.
In this example, I loop over the array and check for some condition. If any element is okay I break out of the block as no special handling is required.
If the loop finishes normally I need the special handling otherwise I continue to execute.
There are other ways of writing this and until streams came along it was still one of the cleanest ways of implementing special handling however now it's more likely that a Stream is a more appropriate.