This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes assertion doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "assertion doubt" Watch "assertion doubt" New topic
Author

assertion doubt

sentil kumar
Ranch Hand

Joined: Oct 23, 2006
Posts: 74
class Assert1{
public static void main(String[] a){
int assert i =12;
}
}
Assume that this code is developed in jdk1.3

but i am getting compile time error in jdk1.4. how to resolve this.
bharathi raja
Greenhorn

Joined: Jul 03, 2006
Posts: 5
How did you compile your program.?
sentil kumar
Ranch Hand

Joined: Oct 23, 2006
Posts: 74
javac -source 1.4 Assert1.java
javac Assert1.java
Manju Devarla
Ranch Hand

Joined: Dec 14, 2006
Posts: 85
Beginning with java 1.4 assert is a keyword..
so when you are compiling with 1.4 gives complier error because its a identifier in your program..
Priya Viswam
Ranch Hand

Joined: Dec 28, 2006
Posts: 81
If you want to compile that code in 1.4 use this command
javac -source 1.3 Assert1.java
or
javac Assert1.java

Also pls remove the space between assert and i
from the line int assert i =12;


SCJP 1.5<br />SCWCD 1.4
sentil kumar
Ranch Hand

Joined: Oct 23, 2006
Posts: 74
int asserti = 12; // i know this will compile
but my doubt is, if the code is developed in jdk1.3 which uses assert as a identifier then whether this code can be compiled or not in jdk1.4 .
please clarify this.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Priya Viswam:
...Also pls remove the space between assert and i
from the line int assert i =12;

I think you mean between i and the equals sign? Even so, this is a matter of style (which is always good for arguments of personal preference). For what it's worth, Sun seems to advocate using spaces.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by sentil kumar:
...but my doubt is, if the code is developed in jdk1.3 which uses assert as a identifier then whether this code can be compiled or not in jdk1.4...

If you are compiling in 1.4 using the -source 1.4 flag, then "assert" will be treated as a key word, and you will get an error using it as an identifier.

If you compile without the -source 1.4 flag, then you should be okay (until you move to 1.5), provided you're not trying to use the assertion mechanism in the same compilation unit.


(Barry: undid my wrongly corrected "typo" in last paragraph: 1.4 -> 1.3 -> 1.4)

[ January 25, 2007: Message edited by: Barry Gaunt ]
[ January 25, 2007: Message edited by: Barry Gaunt ]
Priya Viswam
Ranch Hand

Joined: Dec 28, 2006
Posts: 81
Try with this

javac -target 1.3 -source 1.3 Assert1.java
sentil kumar
Ranch Hand

Joined: Oct 23, 2006
Posts: 74
Thanks a lot Priya, it is working fine.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
When you use the Java 1.4 compiler for compiling 1.3 Java code it is helpful to read the documentation. Look at what it tells you about the -source option and -target option.

Assertions are well described in this document.
[ January 25, 2007: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
sentil kumar
Ranch Hand

Joined: Oct 23, 2006
Posts: 74
javac -target 1.3 Assert1.java // this is enough to compile in jdk1.4

what is the difference between -target option and -source option.

is both are meant to set the compiler version?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Well, what did the documentation tell you about the -target option?
sentil kumar
Ranch Hand

Joined: Oct 23, 2006
Posts: 74
-source release
Enables support for compiling source code containing assertions.
When release is set to 1.4, the compiler accepts code containing assertions. Assertions were introduced in J2SE 1.4.

When release is set to 1.3, the compiler does not support assertions. The compiler defaults to the 1.3 behavior if the -source flag is not used.

i am not clear whether the release is used to set compiler version or source code version(which uses newly added classes in java1.4)

scenario-1
C:\j2sdk1.4.2\bin>javac -target 1.3 -source 1.4 Assert1.java
javac: source release 1.4 requires target release 1.4
if both source,target is same , then how both will defer.

scenario-2
C:\j2sdk1.4.2\bin>javac -target 1.3 Assert1.java
Assert1.java:3: warning: as of release 1.4, assert is a keyword, and may not be
used as an identifier
int assert = 12;
^
1 warning

doubt is how compiler 1.3 gives warning message which is not aware of asser keyword.
sentil kumar
Ranch Hand

Joined: Oct 23, 2006
Posts: 74
Continuous to my previous post :
if compiler is 1.3 then how it is generating error message. because 1.3 will not know any thing about assert keyword.
why they have default to 1.3 in the 1.4 version
---
but there is no confusion in the java 5.0,
The compiler defaults to the version 5 behavior if the -source flag is not used.
5 Synonym for 1.5
--------------------------------------------------------------------------------
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
There is no Java 1.3 compiler being used.
If you are using the Java 1.4 jdk you are using the Java 1.4 compiler (javac) in all cases. -source tells the Java 1.4 compiler what version of the Java language is being used for writing the source code. If -source 1.3 (or nothing) is present the Java 1.4 compiler expects Java 1.3 style source code. The Java 1.4 compiler knows about assertions so it will warn you if "assert" is used as an identifier in the Java 1.3 style code. If -source 1.4 is used then the Java 1.4 compiler will not accept "assert" as an identifier and will issue an error if "assert" is used in this way. If "assert" is used as a keyword in the syntactically correct way the Java 1.4 compiler will place assertions into the compiled code.

The -target option is to specify what version of the JVM the compiled code is meant for. -target 1.3 means that the code is meant for a JVM version 1.3 or higher. If you compile code with -source 1.4 and use -target 1.3 then the code must not use assertions because a Java 1.3 JVM cannot process it.
[ January 25, 2007: Message edited by: Barry Gaunt ]
sentil kumar
Ranch Hand

Joined: Oct 23, 2006
Posts: 74
If "assert" is used as a keyword in the syntactically correct way the Java 1.4 compiler will place assertions into the compiled code.

class Assert1{
public static void main(String[] a){
assert 1 > 2;
System.out.println("asdasd");
}
}
this above code uses the assert keyword syntactically correct way only then why it gives compile time error.
C:\j2sdk1.4.2\bin>javac Assert1.java
Assert1.java:3: warning: as of release 1.4, assert is a keyword, and may not be
used as an identifier
assert 1 > 2;
^
Assert1.java:3: not a statement
assert 1 > 2;
^
Assert1.java:3: ';' expected
assert 1 > 2;
^
2 errors
1 warning

but if the above code is compiled with -source 1.4 only it compiles fine.

is java 1.4 compiler uses the java 1.3 language specfication to validate against the above code.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
sentil:: "is java 1.4 compiler uses the java 1.3 language specfication to validate against the above code."

I wrote:
"If -source 1.3 (or nothing) is present the Java 1.4 compiler expects Java 1.3 style source code"

Yes, because you used "javac Assert1.java".
sentil kumar
Ranch Hand

Joined: Oct 23, 2006
Posts: 74
now i am clear about assertions.

sorry i do not know that Java 1.3 style source code means JLS 1.3.
is java 1.4 compiler uses JLS1.3 to handle assert keyword.
but in java 5.0 compiler is default to JLS 5.0 only.

Thanks a lot Barry.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
There is no Java Language Specification JLS 1.3.
There is JLS First Edition, JLS 2nd edition and JLS 3rd edition. Between the last two editions there is this clarifications document.

Assertions are first mentioned (indirectly) in the clarifications document.

The document "Programming With Assertions" (link in a post above) is the one that SCJP 1.4 bases its assertion objectives on.
 
Consider Paul's rocket mass heater.
 
subject: assertion doubt
 
Similar Threads
Cant use object from another class
WA #1.....word association
Posting to bump up my count...
Text box value is not being completely filled with request attribute
The x900 effect ?