• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Program with assert does not compile

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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....
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic