Welcome back to my next post in series of less known java features. If you are new to this series, I would like to encourage you to read previously discussed features e.g. usage of sun.misc.Unsafe, SerialVersionUID and related facts, Instanceof operators don’t need explicit null checks and Checked exceptions thrown in initializer blocks can be declared by the constructors etc.
In this post, I will discuss another such feature in java which is labeled blocks. They are logically similar to to goto statements in C/C++.
How many times, we have been told that “goto” statements are evil. I myself have read about this so called evil through many respected authors of our time. But, if you look at the sourcecode of String.java, and read the sourcecode of public String toLowerCase(Locale locale) method, you will something like this:
scan : for (firstUpper = 0 ; firstUpper < count; ) { char c = value[offset+firstUpper]; if ((c >= Character.MIN_HIGH_SURROGATE) && (c <= Character.MAX_HIGH_SURROGATE)) { int supplChar = codePointAt(firstUpper); if (supplChar != Character.toLowerCase(supplChar)) { break scan; } firstUpper += Character.charCount(supplChar); } else { if (c != Character.toLowerCase(c)) { break scan; } firstUpper++; } } return this; }
In java, we all know for what purpose the keywords “break” and “continue” exist. Basically, statements break and continue alter the normal control flow of compound statements. They can be used in two ways:
Default usage: e.g.
while (Some condition) { if ( a specific condition ) break; //Default usage else normal business goes here.. }
Another way is to use with a label.
hackit: while (Some condition) { if ( a specific condition ) break hackit; //Usage with label else normal business goes here.. }
FYI, A label is any valid Java identifier followed by a colon. e.g. outer:, inner:, inner123:, inner_: etc.
Whenever during a program execution, a labeled break statement is encountered that control immediately goes out of enclosing labeled block. Similarly, labeled continue will bring control back to start. Just like in normal break and continue statements, with additional names given to blocks.
Let’s look at more example usages:
outer: for (int i = 0; i < 10; i++) { inner: for (int j = 10; j > 0; j--) { if (i != j) { System.out.println(i); break outer; }else{ System.out.println("-->>" + i); continue inner; } } } //OR int a = 10; int b = 12; block1: { if (a < 0) { break block1; } if (b < 0) { break block1; } System.out.println( a + b ); } }
Bullet points
- Java does not have a general goto statement.
- The statements break and continue in Java alter the normal control flow of compound statements. They can use labels which are valid java identifiers with a colon.
- Labeled blocks can only be used with break and continue statements.
- They must be called within its scope. You can not refer them scope of labeled block.
- The break statement immediately jumps to the end (and out) of the appropriate compound statement.
- The continue statement immediately jumps to the next iteration (if any) of the appropriate loop.
- A continue statement does not apply to a switch statement or a block statement, only to compound statements that loop: for, while, and do.
Happy Learning !!
Ask Questions & Share Feedback