Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

is assert java keyword?

 
mohana konakanchi
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is assert java keyword.
or Can i use it for variable names?
like
<code> int assert=0; </code>
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic