HowToDoInJava

  • Java 8
  • Regex
  • Concurrency
  • Best Practices
  • Spring Boot
  • JUnit5
  • Interview Questions

Labeled Statements in Java

By Lokesh Gupta | Filed Under: Java Basics

Java labeled blocks are logically similar to to goto statements in C/C++.

A label is any valid java identifier followed by a colon. e.g. outer:, inner:, inner123:, inner_: etc.

1. Labeled statement in String class

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;
}

What is this “scan:”. This is the labeled block which we are going to learn about today. Well, they always told us not to use them and used it in perhaps most used class in JDK distribution. 🙂

2. Labeled statement with ‘break’ and ‘continue’ keywords

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.

2.1. break keyword with and without labeled statement

while (Some condition) 
{
  if ( a specific condition ) 
  		break;        //Default usage
  else
  		normal business goes here..
}

Another way is to use break with a labeled statement is.

hackit:
while (Some condition) 
{
  if ( a specific condition ) 
  		break hackit;       //Usage with label
  else
  		normal business goes here..
}

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.

2.2. More examples

Let’s look at more example usages:

outer: for (int i = 0; i &lt; 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 &lt; 0) {
      break block1;
    }
    if (b &lt; 0) {
      break block1;
    }
    System.out.println( a + b );
  }
}

3. Summary

  • 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 !!

About Lokesh Gupta

Founded HowToDoInJava.com in late 2012. I love computers, programming and solving problems everyday. A family guy with fun loving nature. You can find me on Facebook, Twitter and Google Plus.

Ask Questions & Share Feedback Cancel reply

Your email address will not be published. Required fields are marked *

*Want to Post Code Snippets or XML content? Please use [java] ... [/java] tags otherwise code may not appear partially or even fully. e.g.
[java] 
public static void main (String[] args) {
...
}
[/java]

Search Tutorials

  • Email
  • Facebook
  • RSS
  • Twitter

Java Tutorial

  • Java – Introduction
  • Java – JDK, JRE and JVM
  • Java – Naming Conventions
  • Java – ClassPath
  • Java – Variables
  • Java – Operators
  • Java – keywords
  • Java – Data Types
  • Java – Primitive Types
  • Java – Wrapper Classes
  • Java – Types of Statements
  • Java – Control Statements
  • Java – String
  • Java – Create Class
  • Java – main() Method
  • Java – Comments
  • Java – hashCode() and equals()
  • Java – System Properties
  • Java – Pass-by-Value
  • Java – 32-bit vs. 64-bit
  • Java – java.exe vs javaw.exe
  • Java – Generate Bytecode
  • Java – Little-Endian vs Big-Endian
  • Java – Command Line Args
  • Java – Compare floats
  • Java – Static Import
  • Java – Recursion
  • Wrapper Class Internal Caching

Popular Tutorials

  • Java 8 Tutorial
  • Core Java Tutorial
  • Java Collections
  • Java Concurrency
  • Spring Boot Tutorial
  • Spring AOP Tutorial
  • Spring MVC Tutorial
  • Spring Security Tutorial
  • Hibernate Tutorial
  • Jersey Tutorial
  • Maven Tutorial
  • Log4j Tutorial
  • Regex Tutorial

Meta Links

  • Advertise
  • Contact Us
  • Privacy policy
  • About Me

Copyright © 2016 · HowToDoInjava.com · All Rights Reserved. | Sitemap