Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Assertion in Java

Assertion in Java


Java final method Assert in Java Final method in java Autoboxing in Java assert In Java JDBC batch insert using Java bean class Java Singleton Pattern Java Collection API - Java Tutorials Stack Overflow - Java Tutorials End of Year Puzzle The link to the outer class,java tutorial,java tutorials Orientating Components Right to Left,java newsletter,java,tutorial Non-virtual Methods in Java - java tutorials Multicasting in Java - java tutorials,tutorial Creation Time Comparison of Multi Dimensional Array- Java Tutorials Making Exceptions Unchecked - java tutorial,java tutorials Making Enumerations Iterable - JDK 5 Example Which is Faster - LinkedList or ArrayList? LinkedHashMap - Java Tutorials HashMap and HashCode JDK 1.4 the NullPointerException - Java Tutorials Java HashMap - Java Tutorials Java GC java awt package tutorial Java Multi Dimensions Array - Java Tutorials Java HashSet  - Java Tutorials Java Field Initialisation - Java Tutorials Java Comments - Java Tutorials J2EE Singleton Pattern - Design Pattern Tutorials Java - JDK Tutorials garbage collection in java Final Methods - Java Tutorials Exceptional Constructors - Java Tutorials Double-checked locking,java tutorials,java tutorial Disassembling Java Classes - Java Tutorials Java Deadlocks - Java Deadlocks Tutorials, Deadlocks in Java Commenting out your code - Java Tutorials Class names don't identify a class - Java Tutorials Causing Deadlocks in Swing Code Exception in Java - Java Tutorials Catching Exceptions in GUI Code - Java Tutorials boolean comparisons - tutorial Interthread Communication in Java BASIC Java - Java Tutorials Thread Deadlocks - Java Tutorials Autoboxing unboxing in Java - Java Tutorials Assertion in Java Appending Strings - Java Tutorials Anonymous Inner Classes - Anonymous Inner Classes tutorial Assertion in java

Assertion in java

To check or test your assumption in your code or program, assertion is used . For example , if you write a method for calculating the speed of a car , you can assert that calculated speed must be less than 100km/hr.

Given below the two forms of the assertion. The simple first form is :

assert BooleanExpression ;

In the above statement, system executes assertion statement and evaluate boolean expression and if it false, it throws exception AssertionError.

The second form of the assertion is :

assert BooleanExpression : Expression2 ;

Here Expression2 is an expression who has value or returns value, it can't be a void method.

Compiling Code containing assertions

Compiler accept your code having assertions, if you compile it as follows :

  javac ?source 1.4     

The javac compiler accept your program, if you compile your .java file with -source 1.4 command-line option.

Enable/disable assertion at runtime

By default assertions are disabled at runtime. You can enable assertion at runtime as follows :

   java -ea DemoAssert             


   java ?enableassertion DemoAssert     


You can disable assertion at runtime as follows :

   java -da DemoAssert             


   java -disableassertions DemoAssert     


Given below a sample example to check the value must lie in between 0 & 20 :

import java.util.*;
import java.util.Scanner;

public class AssertionExample
public static void main( String args[] )
Scanner scanner = new Scanner( );

System.out.print( "Enter a number between 0 and 20: " );
int value = scanner.nextInt();
assert( value >= 0 && value <= 20 ) :
"Invalid number: " + value;
System.out.printf( "You have entered %d\n", value );


If you enter value in between 0 & 20, output should be :

C:\>java -ea AssertionExample
Enter a number between 0 and 20 : 4
You have entered 4

If you enter value greater than 20, For example 21, the following message will be displayed :

C:\>java -ea AssertionExample
Enter a number between 0 and 20 : 21
Exception in thread "main" java.lang.AssertionError: Invalid number: 21
at AssertionExample.main(


Share on Google+Share on Google+

Assertion in Java

Posted on: April 18, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Assertion in Java  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image