Assertions:
    Topics:

    1. Introduction
    2. Types of assert statement
    3. Various Run Time Flags
    4. Proper and Improper Use of assertions

    Introduction


    Assertions has introduced in 1.4 version. The main objective of assertions is to perform debugging.The traditional way of debugging is to use System.out.println’s. But the main disadvantage of this approach is compulsory we should remove these S.O.P’s after fixing the problem other wise these will execute at run time. Which may effect performance of the system. It may reduce readability of the code and disturbs logging mechanism.
    To resolve all these problems sun people has introduces Assertions concept in 1.4 version.The main advantage of assertions is we can enable or disable assertions based on our requirement. But by default assertions are disabled.Assertions concept we have to use in Test environment or in Development environment but not in the production.

    Identifier Vs Keyword
    Assert keyword has introduced in 1.4 version hence from 1.4 version on words we are not allowed to use assert as identifier.

    Example:

    package com.java2learn.assertions;
    public class AssertionDemo {
    
    	public static void main(String[] args) {
    		
    		int assert = 10;
    		System.out.println("assert");
    	}
    }
    

    in 1.4 or 1.5 if we compile
    javac AssertionDemo.java then we will get the following C.E as of release 1.4 assert is a keyword and may not used as an identifier.

    javac –source 1.3 AssertionDemo.java
    java AssertionDemo

    Types of assert statement


    There are 2 types of assert statement
    1) simple assert.
    2) Augmented assert.

    Simple assert
    Syntax: assert ;
    assert(b);
    If b is true then normal continuation follows. Else the program will terminate abnormally can rise assertion error.
    Example:

    package com.java2learn.assertions;
    public class AssertionDemo {
    
    	public static void main(String[] args) {
    		
    		int age = 10;
    		assert(age>10);
    		System.out.println("assert");
    	}
    }
    

    1) javac AssertionDemo.java
    2) java AssertionDemo
    in this case assert statement won’t be executed because it is disable by default.
    3) java –ea AssertionDemo
    Exception in thread “main” java.lang.AssertionError
    at com.java2learn.assertions.AssertionDemo.main(AssertionDemo.java:9)

    Augmented Version
    Syntax: assert : ;
    assert e1:e2;
    ‘e1’–> should be boolean type.
    ‘e2’–> any thing is allowed including method calls also.
    Example:

    package com.java2learn.assertions;
    public class AssertionDemo {
    
    	public static void main(String[] args) {
    		
    		int age = 10;
    		assert(age>10):"here the value of age should be > 10 but it is "+age;
    		System.out.println("assert");
    	}
    }
    

    javac AssertionDemo.java
    java –ea AssertionDemo
    Output:
    Exception in thread “main” java.lang.AssertionError: here the value of age should be > 10 but it is 10
    at com.java2learn.assertions.AssertionDemo.main(AssertionDemo.java:9)

    Note: assert e1:e2
    Here ‘e2’ will be executed iff ‘e1’ is false.

    package com.java2learn.assertions;
    public class AssertionDemo {
    
    	public static void main(String[] args) {
    		
    		int age = 10;
    		assert(age>10):++age;
    		System.out.println(age);
    	}
    }
    

    javac AssertionDemo.java
    java –ea AssertionDemo
    Output:
    Exception in thread “main” java.lang.AssertionError: 11
    at com.java2learn.assertions.AssertionDemo.main(AssertionDemo.java:9)

    case:1
    assert(e1):e2;
    for e2 any thing is allowed including method calls also but void return type method calls are not allowed. Violation leads to compile time error.
    Example:

    package com.java2learn.assertions;
    public class AssertionDemo {
    
    	
    	public static void main(String[] args) {
    		
    		int age = 10;
    		assert(age>10):find();
    		System.out.println(age);
    	}
    	
    	public static void find(){
    		System.out.println("find() method");
    	}
    }
    

    Output:
    Compile time error:Expression must return a value

    Various Run Time Flags


    1) -ea –> To enable assertions in every non-system class(i.e user defined class)
    2) -enableassertions–>To enable assertions in every non-system class(Exactly similar to -ea)
    3) -da–>To disable assertions in every non-system class.
    4) -isableassertions–>To disable assertions in every non-system class.(Exactly similar to -da)
    5) -esa–>To enable assertions in every system class
    6) -enablesystemassertion–>similar to -esa
    7) –dsa–>To disable assertions in every system class.
    8) -disablesystemassertions–>similar to ‘-dsa’

    diagram
    ?
    Example:
    java –ea –esa –da –dsa –ea AssertionDemo
    All the flags will execute from left to right and there is no priority difference b/w enabling and disabling

    Note: we can enable assertions either class wise or package wise also.

    Proper and Improper Use of assertions


    1) It is improper to use assert statement for validating the arguments of a public method.
    public void withdraw(double amount){
    assert(amount >= 100);
    }
    No Compile time error, Runtime error, it is improper use.
    2) It is improper to use assertions for validating command line argument also, because these are arguments to public main method.
    3) It is proper to use assertions for validating private method argument.
    4) It is improper to mix programming language with assert statement.
    5) In our code if there is any place where the control is not allowed to reach. It is the best place to use the assert statement.

    Example:

    switch (day){
    case 1:
    ....
    ....
    case 2:
    ....
    ....
    :
    :
    :
    case 7:
    ....
    ....
    default:
    assert(false);
    }
    
  1. indAssertionError