A Guide To Assertions In Java

June 20, 2023
A Guide to Assertions in Java

There is no better way to detect bugs and test code in Java than using Java assert statements. They let us test our code at runtime and pull the brakes at first sight of an error. Much like Java exceptions, they check the correctness of the code to ensure no unchecked assumption breaks havoc at the development stage.

new java job roles

The Syntax

Java assert statements are usually used with a Boolean expression and written in one of two ways.

assert expression;


assert expression1 : expression2;

In the above, the first expression is a Boolean expression, while the second entails a value comparable to expression 1.

Both syntax works and can test the correctness of your code at runtime. However, it is better to use the second as it also helps diagnose and document the errors.

How to Enable Java Assert Statements?

Java assertions come disabled by default to ensure backward compatibility. The versions before Java 1.4 enabled developers to use now keyword ‘assert’ to name variables which gives rise to a naming clash if a developer uses an older code with versions after Java assert release.

Luckily, you can easily enable Java assert statements with a single-line command, as given below:

java -ea: arguments


java -enableassertions: arguments

In addition, the following commands are used to enable Java asserts for a specific class.

java -ea TestProgram


java -enableassertions: TestProgram

In the syntaxes above, TestProgram is used in place of the specific class name where assertions are required to be enabled.

How to Disable Java Assert Statements?

Similarly, Java asserts can also be disabled via the below commands.

java -da arguments


java -disbaleassertions arguments

While the below code is used to disable assert statements within a System class.

java - dsa: arguments


java -disablesystemassertions:arguments

When to Use Java Assert Statements?

Many developers think Java assert statements are futile. It is true they don’t change the game when developing small programs though the minute big ones enter the system and complexities arise, asserts could save the day.

Primarily, these statements help debug and test the code and stop an erred code from compilation. They stop processing at first false, as by default, all asserts are true or positive.

Seeing an AssertionError is nothing less than a blessing in disguise, as it helps identifies where a fix is required and saves loads of time spent in identifying and diagnosing errors. Not to forget, these statements serve multiple purposes, including increasing readability and simplifying code documentation.

Let’s explore a short example of Java asserts in action and how they help minimize needless hassle and speed up development.

Without Java Assert

Connection conn = getConnection();

if(conn == null) {

throw new RuntimeException("Connection is null");


With Java Assert

Connection conn = getConnection();

assert conn != null;

That’s how exceptional Java assert statements are. A single statement can help do away with the ever-so-complex if and throw statements.

Java Assert Statements Practical Example

Let’s begin with the first syntax with only the Boolean expression. The example below illustrates how you can use Java assert to control input from exceeding the specified value.

import java.util.Scanner;

public class Example1 {

  public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    System.out.println("Enter the marks out of 100");

    int marks = sc.nextInt();


    assert(marks < 100);

    System.out.println("marks : "+marks);



With Java assert enabled, the above program will output marks, if less than 100, while throw an exception if they exceed 100.

Enter the marks out of 100


marks: 50
Enter the marks out of 100


Exception in thread "main" java.lang.AssertionError

Now, let’s run the second syntax with a Boolean expression and a statement. As explained before, this is a more practical way forward as it helps document and fix the highlighted issues or errors.

In the example below, we will run a program to show how you can use asserts to halt the program at first false and show the reason for the stop.

import java.util.Scanner;

public class Example2 {

public static void main(String[] args) {

  Scanner sc = new Scanner(System.in);

  System.out.println("Enter your age ");

  int age = sc.nextInt();


  assert age<130 : "Age is greater than 130";

  System.out.println(" Age : "+age);



The Wrap Up

Java asserts check code for logically impossible statements & define parameters for the code. They also help simplify arguments or condition statements within the code. Thus, it is a must-know feature for Java developer to write error-free and clean codes.

Also Read: A Guide To Iterator In Java

new Java jobs



Full Stack Java Developer | Writer | Recruiter, bridging the gap between exceptional talent and opportunities, for some of the biggest Fortune 500 companies.

Candidate signup

Create a free profile and find your next great opportunity.


Employer signup

Sign up and find a perfect match for your team.


How it works

Xperti vets skilled professionals with its unique talent-matching process.


Join our community

Connect and engage with technology enthusiasts.