Showing posts with label Keyword. Show all posts
Showing posts with label Keyword. Show all posts

Assert keyword in Java

Posted by Somesh Shinde On Monday, 1 August 2016 0 comments

ASSERT IN JAVA


The assert keyword is used in assert statement which is a feature of the Java programming language since Java 1.4. Assertion enables developers to test assumptions in their programs as a way to defect and fix bugs.

Syntax of assert statement

 Syntax of an assert statement is as follow (short version):
assert expression1;

or (full version):
assert expression1 : expression2;

Where:
  • expression1 must be a boolean expression.
  • expression2 must return a value (must not return void).
The assert statement is working as follows:
    • If assertion is enabled, then the assert statement will be evaluated. Otherwise, it does not get executed.
    • If expression1 is evaluated to false, an AssertionError error is thrown which causes the program stops immediately. And depending on existence of expression2:
      • If expression2 does not exist, then the AssertionError is thrown with no detail error message.
      • If expression2 does exist, then a String representation of expression2’s return value is used as detail error message.
    • If expression1 is evaluate to true, then the program continues normally.

Enable assertion


By default, assertion is disabled at runtime. To enable assertion, specify the switch –enableassertions or -ea at command line of java program. For example, to enable assertion for the program called CarManager:
java –enableassertions CarManager

or this for short:
java –ea CarManager

Assertion can be enabled or disable specifically for named classes or packages. For more information on how to enable and disable assertion, go to: http://docs.oracle.com/javase/1.4.2/docs/guide/lang/assert.html#enable-disable

Assertion examples

 The following simple program illustrates the short version of assert statement:
public class AssertionExample{
           public static void main(String args[]){
                              //get a number in the first argument
                             int number=Integer.perseInt(args[0]);
                             assert number<=10;
                             System.out.println("PASS");
           }
}

 When running the program above with this command:
java -ea AssertionExample 15

A java.lang.AssertionError error will be thrown:

Exception in thread "main" java.lang.AssertionError

       at AssertionExample.main(AssertionExample.java:6)

But the program will continue and print out “Pass” if we pass a number less than 10, in this command:

java -ea AssertionExample 8

And the following example is using the full version of assert statement:
public class AssertionExample2 {
    public static void main(String[] args) {
        int argCount = args.length;
        assert argCount == 5 : "The number of arguments must be 5";
        System.out.println("OK");

    }

}
 When running the program above with this command:

java -ea AssertionExample2 1 2 3 4

it will throw this error:

Exception in thread "main" java.lang.AssertionError: The number of arguments must be 5

       at AssertionExample2.main(AssertionExample2.java:6)

Generally, assertion is enabled during development time to defect and fix bugs, and is disabled at deployment or production to increase performance.
READ MORE

Assertion in Java

Posted by Somesh Shinde On Wednesday, 20 July 2016 0 comments

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.

Syntax

There are two ways to use assertion. First way is:
  1. assert expression;  
and second way is:
  1. assert expression1 : expression2;  


Example

  1. import java.util.Scanner;  
  2.     
  3. class AssertionExample{  
  4.  public static void main( String args[] ){  
  5.   
  6.   Scanner scanner = new Scanner( System.in );  
  7.   System.out.print("Enter ur age ");  
  8.     
  9.   int value = scanner.nextInt();  
  10.   assert value>=18:" Not valid";  
  11.   
  12.   System.out.println("value is "+value);  
  13.  }   
  14. } 


If you use assertion, It will not run simply because assertion is disabled by default. To enable the assertion, -ea or -enableassertions switch of java must be used.
Compile it by: javac AssertionExample.java
Run it by: java -ea AssertionExample
Output: Enter ur age 11
        Exception in thread "main" java.lang.AssertionError: Not valid


Where to not use an Assertion

There are some situations where assertion should be avoid to use. They are:
  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.
READ MORE