public class welcome{public static void Main (string[] args) { assert false; System.out.println ("Welcome World");} }
CMD under input command
Javac Welcome.java
Java Welcome
Output to
Welcome World
Java does not open assertions by default, in which case the Assert statement will be skipped and not executed.
In order to execute the Assert statement, you need to re-enter the command after adding the-ea parameter, cmd
Javac Welcome.java
Java-ea Welcome
At this point the output is
Exception in thread "main" Java.lang.AssertionError at welcome.main (welcome.java:3)
The Java assertion mechanism requires that the Boolean expression following the Assert must return a true value before it continues to execute, and if not true, throws a exception, which is an unchecked exception.
The assert can also return a hint message, as follows
public class welcome{
public static void Main (string[] args) {
Assert false: "assert is false"; separated by a colon
System.out.println ("Welcome World");
}
}
CMD under input command
Javac Welcome.java
Java Welcome
Output to
Exception in thread ' main ' java.lang.AssertionError: assert is false //assert hint
At Welcome.main (Welcome.java:3)
Assertions are a relatively simple mechanism for debugging programs.
Java Assertion assert