• 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

doubt on assertion mechanism

 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please refer kb6 book page no. 387. the book says

for some reason you're using a Java 1.4 compiler, and if you're using assert
as a keyword (in other words, you're actually trying to assert something in
your code), then you must explicitly enable assertion-awareness at compile
time, as follows:
javac -source 1.4 com/geeksanonymous/TestClass.java



my question is that even if we dont use the -source 1.4 flag , still it would have compiled because we are using 1.4 compiler, then why the book says to explicitly enable assertion-awareness at compile time ? secondly if the book is right then is the above paragraph specifically for 1.4 compiler ?
 
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

gurpeet singh wrote:please refer kb6 book page no. 387. the book says

for some reason you're using a Java 1.4 compiler, and if you're using assert
as a keyword (in other words, you're actually trying to assert something in
your code), then you must explicitly enable assertion-awareness at compile
time, as follows:
javac -source 1.4 com/geeksanonymous/TestClass.java



my question is that even if we dont use the -source 1.4 flag , still it would have compiled because we are using 1.4 compiler, then why the book says to explicitly enable assertion-awareness at compile time ?



misprinting.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saloni jhanwar wrote:

gurpeet singh wrote:please refer kb6 book page no. 387. the book says

for some reason you're using a Java 1.4 compiler, and if you're using assert
as a keyword (in other words, you're actually trying to assert something in
your code), then you must explicitly enable assertion-awareness at compile
time, as follows:
javac -source 1.4 com/geeksanonymous/TestClass.java



my question is that even if we dont use the -source 1.4 flag , still it would have compiled because we are using 1.4 compiler, then why the book says to explicitly enable assertion-awareness at compile time ?



misprinting.




i'm afraid it is misprinting Saloni. please refer the link http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html?page=2. specifically it says

By default (that is, in the absence of the -source 1.4 switch), a J2SE 1.4 compiler does not allow assert statements. However, the compiler complains if the source code uses the assert keyword as an identifier or label. That means a J2SE 1.4 compiler rejects prior Java source files that use assert in this manner, even though the source compiled successfully under J2SE 1.3 or an earlier compiler. Note that this does not affect previously compiled class files



lets wait for other ranchers to post.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

gurpeet singh wrote:
i'm afraid it is misprinting Saloni. please refer the link http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html?page=2. specifically it says

By default (that is, in the absence of the -source 1.4 switch), a J2SE 1.4 compiler does not allow assert statements. However, the compiler complains if the source code uses the assert keyword as an identifier or label. That means a J2SE 1.4 compiler rejects prior Java source files that use assert in this manner, even though the source compiled successfully under J2SE 1.3 or an earlier compiler. Note that this does not affect previously compiled class files



lets wait for other ranchers to post.



The article that you linked to (even though it is over 10 years old) does a good job at explaining why (Now of course, since there are very few Java 1.4 JVMs still around, this is kind of a moot discussion). Regardless, what is it that you are confused about?

Henry
 
gurpeet singh
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

gurpeet singh wrote:
i'm afraid it is misprinting Saloni. please refer the link http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html?page=2. specifically it says

By default (that is, in the absence of the -source 1.4 switch), a J2SE 1.4 compiler does not allow assert statements. However, the compiler complains if the source code uses the assert keyword as an identifier or label. That means a J2SE 1.4 compiler rejects prior Java source files that use assert in this manner, even though the source compiled successfully under J2SE 1.3 or an earlier compiler. Note that this does not affect previously compiled class files



lets wait for other ranchers to post.



The article that you linked to (even though it is over 10 years old) does a good job at explaining why (Now of course, since there are very few Java 1.4 JVMs still around, this is kind of a moot discussion). Regardless, what is it that you are confused about?

Henry




I am confused because if we compile a program containing assert used as keyword using java 5 or java 6 compiler then we need not specify the -source flag. on the same lines i thought that since assertions were introduced in java 1.4 then the java 1.4 compiler will also behave the same and would compile the program without using the -source switch. but the book said that in case of java 1.4 compiler we have to explicitly give teh source 1.4 switch. so i was confused there.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

By default (that is, in the absence of the -source 1.4 switch), a J2SE 1.4 compiler does not allow assert statements. However, the compiler complains if the source code uses the assert keyword as an identifier or label. That means a J2SE 1.4 compiler rejects prior Java source files that use assert in this manner, even though the source compiled successfully under J2SE 1.3 or an earlier compiler. Note that this does not affect previously compiled class files



Download 1.4 and give a try.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saloni jhanwar wrote:

am confused because if we compile a program containing assert used as keyword using java 5 or java 6 compiler then we need not specify the -source flag. on the same lines i thought that since assertions were introduced in java 1.4 then the java 1.4 compiler will also behave the same and would compile the program without using the -source switch. but the book said that in case of java 1.4 compiler we have to explicitly give teh source 1.4 switch. so i was confused there.



Nice, but you know that it is not correct so you can simply ignore it.




i think we are posting answers in a way that will be confusing to other ranchers. we are using so many adjectives rather than explicitly stating. i didn't got you Saloni as what is not correct. i think the original question i posted was regarding whether source flag in jvm 1.4 compiler is explicitly required or not. the book says it is required. i was confused on this since assertions were introduced in java 1.4 so it should compile assertion code in bytecode. the link that i posted(http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html?page=4) confirms that it is required. so that means the book is correct. going by the link let me ASSERT that

FOR JAVA 1.4 COMPILER , THE -source 1.4 FLAG IS EXPLICITLY REQUIRED.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're just considering only that note about 1.4 then you should also consider K&B ,TABLE 5-3, page->389 that is for 1.6 compiler not for old 1.4. That's why i said previously you can download and can check what happened with old 1.4 if you want to know really but here we are preparing for 1.6(and i meant was you can compile 1.4 old source code using 1.6 compiler without explicit flag declaration which was mandatory for old 1.4 compiler as you tested but not with 1.6 ).It may depend on versions how they are programmed at that time that's it .

Use Version 6 of java and javac
As far as the exam is concerned, you'll ALWAYS be using version 6 of the Java compiler (javac), and version 6 of the Java application launcher (java). You might see questions about older versions of source code, but those questions will always be in the context of compiling and launching old code with the current versions of javac and java.



 
gurpeet singh
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saloni jhanwar wrote:If you're just considering only that note about 1.4 then you should also consider K&B ,TABLE 5-3, page->389 that is for 1.6 compiler not for old 1.4. That's why i said previously you can download and can check what happened with old 1.4 if you want to know really but here we are preparing for 1.6(and i meant was you can compile 1.4 old source code using 1.6 compiler without explicit flag declaration which was mandatory for old 1.4 compiler as you tested but not with 1.6 ).It may depend on versions how they are programmed at that time that's it .

Use Version 6 of java and javac
As far as the exam is concerned, you'll ALWAYS be using version 6 of the Java compiler (javac), and version 6 of the Java application launcher (java). You might see questions about older versions of source code, but those questions will always be in the context of compiling and launching old code with the current versions of javac and java.






i agree with you regarding the part that we are preparing for the exam. even though the book says that we will use current versions of javac and java , but that does not mean we should not explore or experiment. also that note could have confused the reader of the book since it worked differently for java 1.4 and 1.5 or 1.6.

And i just ran the code on java 1.4 compiler , and the book is correct, there is no misprinting as you have said. in a more clearer way for JAVA 1.4 COMPILER YOU HAVE TO EXPLICITLY WRITE -source 1.4 SWITCH if you want your assertion code to compile. else the compiler will throw error.

And i'm not JUST preparing for the exam. i'm trying to learn java that is what matters to me more than exam .
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

gurpeet singh wrote:

saloni jhanwar wrote:If you're just considering only that note about 1.4 then you should also consider K&B ,TABLE 5-3, page->389 that is for 1.6 compiler not for old 1.4. That's why i said previously you can download and can check what happened with old 1.4 if you want to know really but here we are preparing for 1.6(and i meant was you can compile 1.4 old source code using 1.6 compiler without explicit flag declaration which was mandatory for old 1.4 compiler as you tested but not with 1.6 ).It may depend on versions how they are programmed at that time that's it .

Use Version 6 of java and javac
As far as the exam is concerned, you'll ALWAYS be using version 6 of the Java compiler (javac), and version 6 of the Java application launcher (java). You might see questions about older versions of source code, but those questions will always be in the context of compiling and launching old code with the current versions of javac and java.






i agree with you regarding the part that we are preparing for the exam. even though the book says that we will use current versions of javac and java , but that does not mean we should not explore or experiment. also that note could have confused the reader of the book since it worked differently for java 1.4 and 1.5 or 1.6.

And i just ran the code on java 1.4 compiler , and the book is correct, there is no misprinting as you have said. in a more clearer way for JAVA 1.4 COMPILER YOU HAVE TO EXPLICITLY WRITE -source 1.4 SWITCH if you want your assertion code to compile. else the compiler will throw error.

And i'm not JUST preparing for the exam. i'm trying to learn java that is what matters to me more than exam .



Yes, everyone is learning here, if anyone is preparing for exam it doesn't mean he/she is not learning and wasting time, so please don't say like that for ranchers ,and there is nothing logical to discus on this doubt,it's just a command not logic, just give a try and know what works and what doesn't as i previously said.
reply
    Bookmark Topic Watch Topic
  • New Topic