Compile and Run code in assertion in java


Compile and run code using assertion, 
Note: The Java 6 compiler will use the assert keyword by default. Unless you tell it otherwise, the compiler will generate an error message if it finds the word assert used as an identifier. However, you can tell the compiler that you're giving it an old piece of code to compile, and that it should pretend to be an old compiler!

Here is a code:

-------------------------Assertiontest.java----------------------------------------------------------
class AssertionTest{


  public static void main(String [] arg){
     int num=Integer.parseInt(arg[0]);
     assert num>=0 : "The number you have provide by command line 
                argument is not a positive number:"+num;
     System.out.println("This number is positive:"+num);  // This will execute if number is positive


  }


}

------------------------------------------------------------------------------------------------------
Now compile :
 javac  -source 1.4  AssertionTest.java

Run :  
java  -ea  AssertionTest
where -ea means 'enableassertion' and you can this also.


For example see the output:

E:\SCJP 6 PPT\programs>java -ea AssertionTest 3
This number is positive:3

// fine no problem, code will run fine.


Now run with negative number like here..

E:\SCJP 6 PPT\programs>java -ea AssertionTest -3
Exception in thread "main" java.lang.AssertionError: The number you have provide by command line argument is not a positive number:-3 at AssertionTest.main(AssertionTest.java:7)
---------------------------------------------------------------------------------------------------


Let's say you've got to make a quick fix to an old piece of 1.3 code that uses assert as an identifier. At the command line you can type
javac -source 1.3 OldCode.java

The compiler will issue warnings when it discovers the word assert used as an identifier, but the code will compile and execute.
Suppose you tell the compiler that your code is version 1.4 or later, for instance:
javac -source 1.4 NotQuiteSoOldCode.java

In this case, the compiler will issue errors when it discovers the word assert used as an identifier.
------------------------------------------------------------------------------------------------------------------------------
Command Line                                  If assert Is an Identifier                    If assert Is a Keyword
------------------------------------------------------------------------------------------------------------------------------
javac -source 1.3 AssertTest.java      Code compiles with warnings.         Compilation fails.
javac -source 1.4 AssertTest.java      Compilation fails.                            Code compiles.
javac -source 1.5 AssertTest.java      Compilation fails.                            Code compiles.
javac -source 5 AssertTest.java         Compilation fails.                            Code compiles.
javac -source 1.6 AssertTest.java      Compilation fails.                            Code compiles.
javac -source 6 AssertTest.java        Compilation fails.                             Code compiles.
javac AssertTest.java                        Compilation fails.                             Code compiles.
----------------------------------------------------------------------------------------------------------



Disable assertion:

java -ea -da:com.example.Test
Here assertion is enabled generally but disabled for Test class inside package example.

java -ea -da:com.example...
Here assertion is enabled generally but disabled for all classes inside package 'example' under 'com' and it will be disable for subpackages of 'example' also if exists.

java -ea -dsa 
Enable assertions in general, but disable assertions in system classes.

java -ea
java -enableassertions
Enable assertions.

java -da
java -disableassertions
Disable assertions (the default behavior of Java 6).

Comments

Popular posts from this blog

Struts 2 : Warning :No configuration found for the specified action: 'Login.action' in namespace: '/'

Upload csv file at Google Cloud Storage using java

Read Images from a xlsx file using Apache POI