Witscale Test Center

5.6 Assertions > 5.6.3 Compiling code that contains assertions


5.6.3 Compiling code that contains assertions

If you try to compile the assertion examples we have studied so far with pre 1.4 versions of JDK, you will probably get a compiler error. Assertions were introduced in version 1.4 of the JDK. The previous versions of Java treat the assert as an identifier. They do not understand the assert statement.

If you want use assertions, you need to compile and run your code with Java version 1.4 or greater. In version 1.4, you can use the assert as a keyword as well as an identifier. By default, assert is used as an identifier. It will be used as a keyword only when you say so. You get to choose how to use it while compiling the code with version 1.4 compiler.

 

In version 1.4, the use of assert as an identifier is allowed to maintain the backward compatibility with the older versions of Java. The important thing to remember is that you can use it either as a keyword or as an identifier one at a time. You can not use it as a keyword and identifier at the same time. When you make your code assertion-aware (we will see how to do that), the assert will be treated as a keyword wherever it appears in the Java code. As an identifier, you can use the word “assert” to name a class, method or variable.

 

Compiling with –source 1.4 option

When you are compiling with version 1.4 of the Java compiler with option –source 1.4, you are telling the compiler that the code is assertions-aware and it needs to treat the assert as a keyword instead of an identifier. For instance, consider the following VotingTerminal.java class:

 

public class VotingTerminal {

 private void eligibleToVote(int age) {

  assert (age >= 18); // as a keyword

 }

}

 

This can be compiled as:

 

$ javac –source 1.4 VotingTerminal.java

 

With the –source 1.4 option, Java compiler always treats the assert as a keyword. Any assertions-unaware code that uses it as a identifier, say for instance, use it as a variable name, will fail to compile. The following code is assertions-unaware as it uses assert as an identifier.

 

public class AssertTest {

 public static void main(String[] args) {

  int assert = 6; // assert is used as an identifier

 }

}

 

When you compile it with –source 1.4 option, it will flag following error:

 

$javac -source 1.4 AssertTest.java

AssertTest.java:4: as of release 1.4, assert is a keyword, and may not be used as an identifier

          int assert = 6;

              ^

1 error

 

Compiling with –source 1.3 option

When you are compiling with Java compiler of version 1.4 with option –source 1.3, you are telling the compiler that it needs to treat the assert as a keyword instead of an identifier. The compiler’s default behavior is the same as using the –source 1.3 option. Therefore, the use of this option is only for the sake of clarity. For instance, the previous AssertTest.java can be compiled successfully either without any option or with the –source 1.3 option, as:

 

$ javac –source 1.3 AssertTest.java

 

or

 

$ javac AssertTest.java

 

The code will compile without any problems. However, since the option –source 1.3 behaves the same as the default behavior of javac, its use is redundant.

Now consider the VotingTerminal.java, that uses assert as a keyword. Any attempt to compile it with  –source 1.3 option, will result in a compiler error.