Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes is assert java keyword? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "is assert java keyword?" Watch "is assert java keyword?" New topic
Author

is assert java keyword?

mohana konakanchi
Ranch Hand

Joined: May 16, 2001
Posts: 67
Is assert java keyword.
or Can i use it for variable names?
like
<code> int assert=0; </code>
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
That depends on how you decide to compile your code. If you wish to compile to enable assertions (-1.4) then, yes it's a keyword and you can't use it. If you wish to compile without assertions, then it's not a keyword and you can use it.
But, since it's a potential keyword, why ask for trouble? Why not just use another name?


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
The default compilation option is "assertions disabled" or -source 1.3 flag to enforce it explicitly. With JDK 1.4.2, if assert is used as an identifier rather than a keyword, a compiler gives a warning. assert in the code can be used as either identifier or a keyword, but never both.
mohana konakanchi
Ranch Hand

Joined: May 16, 2001
Posts: 67
If I get a question on certification exam 1.4
Should answere assert as keyword or not a keyword.?
I could not find assert keyword in language spec.
Which is correct ans for 1.4 exam.
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
Hi, Mohana -
"assert" was not in my exam as a keyword, and it's not listed in the keywords table in The Java Tutorial from Sun. According to The Java Language Specification at 3.9, "assert" is not a keyword:

The following character sequences, formed from ASCII letters, are reserved for use as keywords and cannot be used as identifiers (�3.8):
abstract default if private this
boolean do implements protected throw
break double import public throws
byte else instanceof return transient
case extends int short try
catch final interface static void
char finally long strictfp volatile
class float native super while
const for new switch
continue goto package synchronized
The keywords const and goto are reserved, even though they are not cur-rently used.

Hope that helps.
[ October 30, 2003: Message edited by: Jeff Bosch ]
Sonja Schmid
Greenhorn

Joined: Nov 02, 2003
Posts: 1
Originally posted by mohana konakanchi:
If I get a question on certification exam 1.4
Should answere assert as keyword or not a keyword.?
I could not find assert keyword in language spec.
Which is correct ans for 1.4 exam.

Hi mohana
http://www.jchq.net has a mock exam with a question on this (Question Bank Id No 149). It says:
The Correct Answer is
3) Starting with jdk 1.4 assert is a keyword
If you attempt to use the word assert as an identifier and compile it with JDK1.4 from Sun the following message is produced.
warning: as of release 1.4, assert is a keyword, and may not be used as an identifier
 
Don't get me started about those stupid light bulbs.
 
subject: is assert java keyword?