This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regarding Assertion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regarding Assertion" Watch "Regarding Assertion" New topic
Author

Regarding Assertion

Suguna Gollapally
Ranch Hand

Joined: Oct 31, 2006
Posts: 37
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

Joined: Aug 03, 2002
Posts: 7729
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 ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Suguna Gollapally
Ranch Hand

Joined: Oct 31, 2006
Posts: 37
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

Joined: Oct 10, 2006
Posts: 12
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.


Rick Taylor
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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

Joined: Feb 08, 2006
Posts: 88
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.


SCJP 5.0 77%
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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
 
subject: Regarding Assertion
 
Similar Threads
Switch and Assertion
toString() behaviour?
missing return statement
wrapper classes
toString() on Integer class