No announcement
 

Assertions

An assertion statement allows testing the validity of an assumption that have been made in the program. An assertion is believed to be true when executes, if it fails, then an AssertionError is thrown.

Syntax:

assert expression;

Or

assert expresstion1 : expression 2;

Example:

AssertionDemo.java

package com.codingnous.tutorial;

public class AssertionDemo {

	public static void main(String[] args) {
		int percentage = 15;
		assert percentage >= 40 : " Fail";
		System.out.println("Percentage: " + percentage);
	}

}

Output
Exception in thread "main" java.lang.AssertionError: Fail at com.codingnous.tutorial.AssertionDemo.main(AssertionDemo.java:7)

Note: assertion must be enabled to run assertion. enable JVM parameter '-ea' in Run configuration.