"assert" Statements

This section describes 'assert' statement, which allows you to make an assertion in the execution flow. 'assert' statements are executed only when -ea' JVM option is specified.

What Is "assert" Statement? - An "assert" statement allows you to make an assertion in the execution flow. "assert" statements are executed only when "-ea" JVM option is specified. When executed an "assert" statement will throw an exception if the given assertion condition fails.

Here is the syntax for an "assert" statement. "exception_message" is optional.

assert assertion_condition
assert assertion_condition : exception_message

Here is a sample program that shows you how to use "assert" statements:

/* AssertStatementTest.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
class AssertStatementTest {
   public static void main(String[] arg) {
      java.io.PrintStream out = System.out;

      int hour = Integer.parseInt​(arg[0]);
      out.println("Hour specified: "+hour);

   private static void greeting(int hour) {

      // Assertion on hour must >= 0
      assert hour >= 0 : "Hour = "+hour+". It be must be >= 0.";

      // Assertion on hour must < 24
      assert hour < 24 : "Hour = "+hour+". It be must be < 24.";

      // Assuming hour value is between 0 and 23
      if (hour < 6)
         System.out.println("Why are you not sleeping?");
      else if (hour < 12)
         System.out.println("Good morning!");
      else if (hour < 18)
         System.out.println("Good afternoon!");
      else if (hour < 22)
         System.out.println("Good evening!");
         System.out.println("Good night!");

If you compile and run the above program with different hour values, you will see:

herong> java AssertStatementTest.java 8
Hour specified: 8
Good morning!

herong> java AssertStatementTest.java 18
Hour specified: 18
Good evening!

herong> java AssertStatementTest.java 28
Hour specified: 28
Good night!

herong> java AssertStatementTest.java -8
Hour specified: -8
Why are you not sleeping?

As you can see, "assert" statements were not executed by default. the greeting() method prints unwanted greeting messages, when invoked as greeting(28) and greeting(-8).

See the next tutorial to turn on the JVM assert option detect the issue.

Table of Contents

 About This Book

 JDK - Java Development Kit

 Execution Process, Entry Point, Input and Output

 Primitive Data Types and Literals

 Control Flow Statements

 Bits, Bytes, Bitwise and Shift Operations

 Managing Bit Strings in Byte Arrays

 Reference Data Types and Variables

 Enum Types and Enum Constants

 StringBuffer - The String Buffer Class

 System Properties and Runtime Object Methods

 Generic Classes and Parameterized Types

 Generic Methods and Type Inference

 Lambda Expressions and Method References

 Java Modules - Java Package Aggregation

 Execution Threads and Multi-Threading Java Programs

 ThreadGroup Class and "system" ThreadGroup Tree

 Synchronization Technique and Synchronized Code Blocks

 Deadlock Condition Example Programs

 Garbage Collection and the gc() Method

Assert Statements and -ea" Option

"assert" Statements

 JVM "-ea" and "-da" Options

 Annotation Statements and Declarations

 Java Related Terminologies

 Archived Tutorials


 Full Version in PDF/EPUB