• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Assert in java

July 12, 2012 By j2eereference

Assertion is nothing but a condition that should be true while executing the program.

Assertion keyword is mostly used in the development and testing phases. It is used as an alternative debugging mechanism. Prior to the introduction of assert we had to use System.out.println to check whether the code compiles fine without any bug. After fixing the bugs in the program we used to comment the System.out.println lines . Assert help us to reduce this complexity.

Why Assertion ?

Assertion is really helpful for error checking during the development phase,Suppose you want to check for the negative number without using assert we will have to do it in the following way

1
2
3
4
5
if(number < 0)
{
System.out.println("Number is negative");
// need to return some value to indicate the error or throw an exception
}

But if we are using assert

assert number > 0 : “Number is negative”;

Here we can notice that , we need only one line of code and also we don’t have to remove this code before the release as the released code will be running without enabling assertion.

Enabling and disabling assertion for a class

We can enable assertion by prefixing -ea to the class name while executing .

-ea AssertDemo

the same way we can disable assertion as

-da  AssertDemo

Enabling and disabling assertion for a package

enabling assert :

-ea : myPackage

Disabling assert :

-da:myPackage

Enabling and disabling assertion for sub packages of a package

-ea:myPackage…

-da:myPackage…

In the above example assertion will be enabled for all the subpackages of myPackage.

How to use Assert ?

There are two ways for creating assert

  • assert (condition);

condition should be Boolean type.
If condition  is true, there is no bug in the program and executes normally further.
If condition is false, there is a bug and hence the program abnormally terminates with assertion error.

1
2
3
4
5
6
7
8
9
public class test {
public static void main(String[] args) {
  boolean   x=false;
  assert(x);
   {
     System.out.println("assert is true");
    }
   }
}

javac test.java
java test
Displays output as ‘assert is true’ because assertions are disabled by default
java  –ea  test
Run time Exception – Assertion error

Here condition is nothing but an expression that should evaluate a Boolean expression. If the result of this condition  is true there wont be any action . In case of false result a default AssertionError is thrown.

  • assert condition : expr;

In this form we are passing an argument to the AssertionError constructor. If there is any AssertionError happens the expression we are passing will be displaying as part of  the AssertionError .

condition should be Boolean type
expr can be any type of description or method calls(except void type)and it is evaluated iff x is false

1
2
3
4
5
6
7
8
9
public class test {
public static void main(String[] args) {
   boolean   x=false;
   assert(x): “assert is false”;
    {
      System.out.println("assert is true");
    }
   }
}

javac test.java
java test
Displays output as ‘assert is true’ because assertions are disabled by default
java –ea test
Run time Exception – Assertion error:assert is false

Related Posts

  • UncaughtExceptionHandler in java
  • How to generate and resolve OutOfMemoryError?
  • Difference between Spring’s Singleton scope & Singleton Design pattern
  • How to stop a thread?
  • Interrupting a thread in java
  • What is ThreadLocal in Java ?
  • ArrayList custom Implementation
  • Difference between volatile and synchronized keyword?
  • How to write thread safe code?
  • How to avoid deadlock in java ?

Filed Under: Core Java

Reader Interactions

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.