• 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 error

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Test16 {
public static void main(String[] args){
test();
}
static void test(){
while(true){
assert false;
System.out.println("B");
}
System.out.println("A");
}
}

compiler will complain that statement
System.out.println("A"); is unreachable
with assertion enabled compilation
But,
class Test16 {
public static void main(String[] args){
test();
}
static void test(){
assert false;
System.out.println("B");

System.out.println("A");
}
}
will not give any such error with assertion enabled compilation

will someone tell me why ?
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shyam,


It is nothing like, ea enabled compilation or disabled compilation. enabling/disabling assertion is the matter of run time.

What compilation error you are getting (statement not reachable) is due to
while(true){...}, because compiler is aware of the fact that the statement
that follows the infinite loop is not reachable.

assert false;
...
In this case compiler doesn't bother because assertion may not be enabled
while running the code. So no problem for it.

"By default assertion is disabled"



Thanks,
[ May 10, 2007: Message edited by: Chandra Bhatt ]
 
shyam kumarK
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chandra,
compilation can also be assertion enabled(assertion aware) or disabled as in:
javac -source 1.4 A.java or simply
javac A.java

let me know if I am wrong
Example:

class A {
public static void main(String[] args){
int assert=8;
System.out.println(assert);
}
}
if you compile this code as javac A.java it compiles with warnings but runs fine and prints 8
whereas if you compile the above code as javac -source 1.4 A.java it will produce 2 errors at compile time saying assert cannot be used as keyword.
Thats why I said assertion enabled/disabled compilation
[ May 10, 2007: Message edited by: shyam kumarK ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shyam,

I think compiling a .java source file containing assert as keyword or
identifier using -source option is not considered as enabling or disabling
the assertion. It is simply the way to tell the compiler how do you want
your assert should be treated like. Whether you want it to be treated like
identifier, or keyword and of course NOT both together.

When you compile your source code where "assert" has been used as identifier
(name of variable, method name, class name or so), you must tell compiler
that I want my source code to be taken compatible to Java 1.3; that allows
you to compile your code with warning though.

When you have used assert as keyword, you tell the compiler to treat your
assert as keyword, and of course the switch -source 1.4 and -source 1.5
demand your code must not use assert as identifier.

If you see the cases, you can't use assert as identifier and keyword in a source file together.


Enabling/Disabling Simply means whether you want your assert
(keyword) to be workable at run time or not. By default its OFF.
Enabling or Disabling never mean to be issue of compiling source file.

You do not enable assertion using -source switch while compiling, it is only
the way to tell the compiler, according to what version your code should be
treated like.



Thanks,
reply
    Bookmark Topic Watch Topic
  • New Topic