This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding Assertion

 
Suguna Gollapally
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • 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));
}
}
}
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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 ??
 
Rick Taylor
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Satish Kota
Ranch Hand
Posts: 88
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic