my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Program with assert does not compile Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Program with assert does not compile" Watch "Program with assert does not compile" New topic
Author

Program with assert does not compile

Yan Lee
Ranch Hand

Joined: Sep 15, 2003
Posts: 94
Hi:
I have jdk 1.4.1 on my machine. I am trying this basic trial program to play with assert.
package assertions;
public class AssertTest
{
public static void main(String []args)
{
boolean x=true;
assert (x);
}
}
I get the following compilation error:
AssertTest.java:11: warning: as of release 1.4, assert is a keyword, and may no
be used as an identifier
assert (x);
^
AssertTest.java:11: cannot resolve symbol
symbol : method assert (boolean)
location: class assertions.AssertTest
assert (x);
^
1 error
1 warning
------------
Can you pls tell me what I need to do to make this program compile?
Thanks in advance for the help.
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
I guess u r using the early version of JDK 1.4 or sthing... If so, u have to use "-source 1.4" argument to compile and use "-enableassertions" or "-ea" to run the program...
For example,
javac -source 1.4 Program.java // Compiling the program
java -enableassertions Program // Running the program
Hope this helps....


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
You have to use the keyword �true� or �false� with the �assert�. U cannot use the variable with the syntax �assert booleanExpr�.
But if u want to use it with a variable, u have to use the syntax �assert booleanExpr: message�.
If booleanExpr is true, execution continues.
If booleanExpr is false, the message expression is evaluated and used as a parameter in creating an AssertionError, which is then thrown, and by default causes execution to terminate.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
You have to use the keyword ?true? or ?false? with the ?assert?. U cannot use the variable with the syntax ?assert booleanExpr?.

But that works.
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
Originally posted by Anupam Sinha:

But that works.

Then p intelli's problem would be in the compiler... Then he should follow the guideline in my first post in this thread...
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi
I think the problem is in the compilation and the use of brackets(). As previously said you would need to compile and execute the program with the -source 1.4 switch in case of compilation and -ea or its long form at the time of execution.
Secondly you cannot use assert like assert (x). Remove the brackets and it should work fine.
Lakshmi Saradha
Ranch Hand

Joined: Oct 21, 2003
Posts: 170
Hi,
I dont think using brackets would be a problem.
I used the following code.
class a
{
public static void main(String args[])
{
int x = 10;
assert (x==10);

}

}
When I tried to compile wusing the -source 1.4 option, I did not get any compiler error.
When I tried to compile without the otpions (javac a.java), I got both the above mentioned errors.
So the problem is only with the compiler options.


Thanks,<br />Lakshmi.
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
Hi Lakshmi Saradha,
So did u solve ur problem by the instruction that I provided up there in my post? It's glad to know that u got the solution....
Yan Lee
Ranch Hand

Joined: Sep 15, 2003
Posts: 94
Hi Ko Ko and others:
Thanks for the responses. The program works fine if I compile/run it using the switches suggested by Ko Ko Naing.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Program with assert does not compile
 
Similar Threads
Simple question on assertion
Error in code when using assertion
assertions compilation error
Assertions
Assertion Doubt.