Assert in Java

Tables

Assertion

Assertion is a statement in java. It can be used to test your assumptions about the program.

While executing assertion, it is believed to be true. If it fails, JVM will throw an error named AssertionError. It is mainly used for testing purpose.

Advantage of Assertion

It provides an effective way to detect and correct programming errors.

Syntax of using Assertion

There are two ways to use assertion. First way is:

assert booleanExpression;

and second way is:

assert booleanExpression : errorMessage;  

Note: booleanExpression, so that expression must return an boolean.

Where not to use Assertion

  1. According to Sun Specification, assertion should not be used to check arguments in the public methods because it should result in appropriate runtime exception e.g. IllegalArgumentException, NullPointerException etc.
  2. Do not use assertion, if you don’t want any error in any situation.

Note

  • Assert statements should not cause side effects (mean should not change value of variable in assert statement).

Static block

Unlike C++, Java supports a special block, called static block (also called static clause) which can be used for static initializations of a class.

This code inside static block is executed only once: the first time you make an object of that class or the first time you access a static member of that class (even if you never make an object of that class). For example, check output of following Java program.

// filename: Main.java
class Test {
    static int i;
    int j;
     
    // start of static block 
    static {
        i = 10;
        System.out.println("static block called ");
    }
    // end of static block 
}
 
class Main {
    public static void main(String args[]) {
 
        // Although we don't have an object of Test, static block is 
        // called because i is being accessed in following statement.
        System.out.println(Test.i); 
    }
}

Output:

static block called
10

Also, static blocks are executed before constructors. For example, check output of following Java program.

// filename: Main.java
class Test {
    static int i;
    int j;
    static {
        i = 10;
        System.out.println("static block called ");
    }
    Test(){
        System.out.println("Constructor called");
    }
}
 
class Main {
    public static void main(String args[]) {
 
       // Although we have two objects, static block is executed only once.
       Test t1 = new Test();
       Test t2 = new Test();
    }
}

Output:

static block called
Constructor called
Constructor called

What if we want to execute some code for every object?

Initializer block

Initializer block contains the code that is always executed whenever an instance is created.

It is used to declare/initialize the common part of various constructors of a class. For example,

import java.io.*;
public class GFG
{
    // Initializer block starts..
    {
        // This code is executed before every constructor.
        System.out.println("Common part of constructors invoked !!");
    }
    // Initializer block ends
 
    public GFG()
    {
        System.out.println("Default Constructor invoked");
    }
    public GFG(int x)
    {
        System.out.println("Parametrized constructor invoked");
    }
    public static void main(String arr[])
    {
        GFG obj1, obj2;
        obj1 = new GFG();
        obj2 = new GFG(0);
    }
}

Output:

Common part of constructors invoked!!
Default Constructor invoked
Common part of constructors invoked!!
Parametrized constructor invoked 

We can note that the contents of initializer block are executed whenever any constructor is invoked (before the constructor’s contents)

The order of initialization constructors and initializer block doesn’t matter, initializer block is always executed before constructor.

What if we want to execute some code once for all objects of a class?

 

Java Integer To String Examples

  • Convert using Integer.toString(int)
  • Convert using String.valueOf(int)
  • Convert using new Integer(int).toString()
  • Convert using String.format()
  • Convert using DecimalFormat
  • Convert using StringBuffer or StringBuilder
  • Quick Solution: + “”
  • Convert with special radix (not base 10 system)

Leave a Reply

Your email address will not be published. Required fields are marked *