Assert in Java

Here we have discussed assertion in Java in detail with its types, benefits, syntax and an example that will help beginners to understand its concept and use.

Assert in Java

In this section we will discuss about what is assertion, how to use assert keyword and benefits of assertion in Java. We have also provided an example to help beginners understand the concept of assertion in Java. Assertion are assumption or predictions made at the beginning of the program so that to ensure the program is true, otherwise, it will not reach to that point and the program will be terminated and throw an AssertionException error.

Assertion is used to test assumptions in a program and make it valid by catching exceptions and logical errors. It makes make program more readable and user friendly. It validates data at run-time. Assertion contain a Boolean expression which is set to be true in the beginning of the program.

Assertion are of two types:

preconditions- invoked when a method is invoked
postconditions- invoked when a method is finished

Assertion can also be used in control flow, as invariants of a class.

Implementing Assertion in Java

Assertion is carried out in Java by using assert keyword. It can be expressed in two ways:

assert expression:

This statement specifies that if assertion expression becomes false, an AssertionError is thrown and program is terminated

assert expression1 : expression2 :

This statement means that the expression1 is evaluated first and if it is false then an AssertionError error with an additional message from expression2 is thrown.

Benefits of Assertion In Java:

  • Assertion in Java is used to review the code.
  • It makes debugging easier.
  • It helps to integrate unit testing as Junit testing.
  • It improves the quality of code.

Syntax:

assert statement1;

"statement1" is a Boolean expression, which is checked when program is executed. If it returns true, program runs without interruption. If statement1 returns false,, the program throw AssertionError and program gets terminated.

assert statement1:statement2;

"statement1" is an error message and statement2 is a Boolean expression. statement1 gives an error message if the statement 2 return false, then it is passed to the Assertion Error constructor of the respective error class. The constructor changes the value in statement1 into string format.

Example of Assertion in Java:

Here we have defined a public class 'Mark Assert Demo'. Inside it we have defined the static variable i.e. maximum marks to be 100, changes is another static variable, The main static ( ) method has assumption of maximum marks i.e. 40, if the marks come below to 40, the code will show you java.lang.AssertionError and display the Marks is below than 40 on the command prompt.

public class MarkAssertDemo
{
static float maximummarks=100;
static float changes(float mark)
{
maximummarks=maximummarks-mark;
System.out.println("The maximummark is:" + maximummarks);
return maximummarks;
}
public static void main(String args[])
{
float g;
for(int i=0;i<5;i++)
{
g=changes(15);
assert maximummarks>=40.00:"marks is below 40.";
}
}
}

Output:

C:\saurabh>javac -source 1.4 MarkAssertDemo.java

C:\saurabh>java -ea MarkAssertDemo

The maximummark is:85.0

The maximummark is:70.0

The maximummark is:55.0

The maximummark is:40.0

The maximummark is:25.0

Exception in thread "main" java.lang.AssertionError: marks is below 40.
at MarkAssertDemo.main(MarkAssertDemo.java:16)

Resource:

Tutorials

  1. Assertion in java
  2. Anonymous Inner Classes - Anonymous Inner Classes tutorial
  3. Appending Strings - Java Tutorials
  4. Assertion in Java
  5. Autoboxing unboxing in Java - Java Tutorials
  6. Thread Deadlocks - Java Tutorials
  7. BASIC Java - Java Tutorials
  8. Interthread Communication in Java
  9. boolean comparisons - tutorial
  10. Catching Exceptions in GUI Code - Java Tutorials
  11. Exception in Java - Java Tutorials
  12. Causing Deadlocks in Swing Code
  13. Class names don't identify a class - Java Tutorials
  14. Commenting out your code - Java Tutorials
  15. Java Deadlocks - Java Deadlocks Tutorials, Deadlocks in Java
  16. Disassembling Java Classes - Java Tutorials
  17. Double-checked locking,java tutorials,java tutorial
  18. Exceptional Constructors - Java Tutorials
  19. Final Methods - Java Tutorials
  20. garbage collection in java
  21. Java - JDK Tutorials
  22. J2EE Singleton Pattern - Design Pattern Tutorials
  23. Java Comments - Java Tutorials
  24. Java Field Initialisation - Java Tutorials
  25. Java HashSet  - Java Tutorials
  26. Java Multi Dimensions Array - Java Tutorials
  27. java awt package tutorial
  28. Java GC
  29. Java HashMap - Java Tutorials
  30. JDK 1.4 the NullPointerException - Java Tutorials
  31. HashMap and HashCode
  32. LinkedHashMap - Java Tutorials
  33. Which is Faster - LinkedList or ArrayList?
  34. Making Enumerations Iterable - JDK 5 Example
  35. Making Exceptions Unchecked - java tutorial,java tutorials
  36. Creation Time Comparison of Multi Dimensional Array- Java Tutorials
  37. Multicasting in Java - java tutorials,tutorial
  38. Non-virtual Methods in Java - java tutorials
  39. Orientating Components Right to Left,java newsletter,java,tutorial
  40. The link to the outer class,java tutorial,java tutorials