• 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

assertion doubt

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How did you compile your program.?
 
sentil kumar
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
javac -source 1.4 Assert1.java
javac Assert1.java
 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
 
sentil kumar
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

javac -target 1.3 -source 1.3 Assert1.java
 
sentil kumar
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Priya, it is working fine.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
sentil kumar
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, what did the documentation tell you about the -target option?
 
sentil kumar
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
-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
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic