Assertions Part Two Tutorial

After watching my Assertions Part 1 Tutorial you should have a general idea of what an assertion is used for. In part 1 I demonstrated one of two styles of the assert statement.
assert (expression) : argument to pass into the AssertionError constructor ;
The second style is even more simple:
assert (expression) ;
The second style will simply throw an AssertionError if the expression evaluates to false. You will receive the same sort of program stopping crash that you experience with the first style, only it will simply contain the method and line number of the assert statement. There is not much of a difference between the two versions, just a little more debugging info in the first style.

Some basic Assertion rules:

  • Use them for debug informational purposes only.
  • Do not use assertions to validate public, default, or protected method parameters - this includes the main method. Instead use exception handling classes such as IllegalArgumentException to handle invalid method parameters.
  • Do not try to catch the AssertionError, let it stop program execution if assertions are enabled.
  • Do not call methods that modify state in the expression portion or the portion following the colon.
Because assertions are optional, they will only run when they are enabled. A program should never execute a different result simply because assertions are enabled or disabled. Basically all the rules amount to a simple concept - assertions should not modify state or change the outcome of normal program execution in any way, shape, or form.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md AssertionsTwo
C:\Java>cd AssertionsTwo
C:\Java\AssertionsTwo>Notepad AssertionsTwo.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


import java.io.*;
class AssertionsTwo {
    static String name;
    public static void main(String args[]) {
        assert(args.length>0) : stupidMethod(); // don't validate public method parameters - use execption handling instead
        //if(args.length==0) {
        //    throw new IllegalArgumentException("Invalid number of arguments!");
        //} 
	
        System.out.println("Program complete.");
    }

    static String stupidMethod() {
        name = "Billy"; // don't modify variable values - makes no sense anyway
        return "Invalid number of arguments!";
    }
}

/*
        if(args.length==0) {
            throw new IllegalArgumentException("Invalid number of arguments!");
        } else {
            try {
                assert(args[0].equals("YES")) ;
            } catch (AssertionError e) {
                System.out.println(e.getMessage());
            }
            if (!args[0].equals("YES")) {
                System.out.println("Expecting YES as the first argument");
            } 
        }
*/
/*
        System.out.print("Enter a number from 0-4: ");
        try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
            try {
                int i = Integer.parseInt(br.readLine());
                if(i>=5) {
                    System.out.println("Really??? Nice try - not!");
                    return;
                } 
                switch(i) {
                    case 0:
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                        System.out.println("Good job, you entered: "+ i);
                        break;
                    default:
                        assert(false) : "i="+i;
                        break;
                }
    
            } catch ( NumberFormatException e) {
                System.out.println("Really??? Nice try - not!");
                return;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
*/

Now switch back to the command prompt (CMD) and type in javac AssertionsTwo.java and press Enter.
Now type in java AssertionsTwo and press Enter.


C:\Java\AssertionsTwo>javac AssertionsTwo.java
C:\Java\AssertionsTwo>java AssertionsTwo
results vary - see video


Final thoughts


Tutorials