• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Regarding Assertion

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can any one tell me why the compiler error in this prg??

class Test {

String f(int i) {
switch(i) {
case 0 : return "A";
case 1 : return "B";
case 2 : return "C";
default : assert false;

}
}
public static void main(String arg[]) {

Test t=new Test();
for(int i=0;i<4;i++) {
System.out.println(t.f(i));
}
}
}
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please show that you have made an effort to solve the problem. What does the error message mean to you? What happens if assertions are disabled at runtime?
[ November 27, 2006: Message edited by: Barry Gaunt ]
 
Suguna Gollapally
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got Compiler error : missing return statement

After i changege the code below :

class Test {

String f(int i) {
switch(i) {
case 0 : return "A";
case 1 : return "B";
case 2 : return "C";
default : assert false;

}
return "s";
}
public static void main(String arg[]) {

Test t=new Test();
for(int i=0;i<4;i++) {
System.out.println(t.f(i));
}
}

compiled fine then enabled assertions : java -ea Test

got the output as
A
B
C
Exception in thread "main" java.lang.AssertionError
at Test.f(Test.java:4586)
at Test.main(Test.java:4595)

But Why Assertion error here ??
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When i is incremented to 3, the switch statement invokes the default case, which is "assert false". At that point, the assertion test fails, and the program throws a runtime AssertionError.
 
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
Think what happens if at compile time assertions are disabled (in Java 5.0 they are enabled by default). Is your program valid when assertions are disabled at compile time?
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suguna,
At compile time compiler checks if u are returning a value. Since the switch may or may not execute compiler forces you to put a rerurn statement. Hence you get an error saying return value required. The error caused is no way related to assertions.
 
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

Originally posted by Satish Kota:
Suguna,
At compile time compiler checks if u are returning a value. Since the switch may or may not execute compiler forces you to put a rerurn statement. Hence you get an error saying return value required. The error caused is no way related to assertions.



I was hoping that Suguna was going to work that out.
 
You’ll find me in my office. I’ll probably be drinking. And reading this tiny ad.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic