File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes About Switch Statements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "About Switch Statements" Watch "About Switch Statements" New topic
Author

About Switch Statements

Augusteen Chelliah
Greenhorn

Joined: Aug 30, 2006
Posts: 3
String f(int k)
{
switch(k)
{
case 0: return "A";
case 1: return "B";
}
}

gives comile error there is no return statement in the Function f()
Can any one explain
Saheed Adepoju
Ranch Hand

Joined: Jun 23, 2004
Posts: 267
Originally posted by Augusteen Chelliah:
String f(int k)
{
switch(k)
{
case 0: return "A";
case 1: return "B";
}
}

gives comile error there is no return statement in the Function f()
Can any one explain

Hello
You have a return type of a string and there is no place in your code that has a return, hence it will have a compile time error due to no return type. If you dont want to return any thing then you should just have a "return null" at the end of the method. I hope this helps.


Saheed Adepoju<br />SCJP 1.4<br />SCJD (B&S..In progress)
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,

... and welcome, Augusteen, to the Java Ranch!!!


The compiler knows, that at runtime this method may return nothing. Would be the case if you call e.g.
f(123);
And if it isn't sure, that a String will returned in all circumstances, the code does not compile.

Better than Saheed's idea of returning a null at the very end of the method would be a default case, perhaps returning a blank or empty String (depends on your purposes).


Yours,
Bu.


all events occur in real time
Karthigeyan Kannabiran
Greenhorn

Joined: Sep 08, 2006
Posts: 5
Hi,
You can also avoid this compilation error just by putting the default case for the switch.

String f(int k)
{
switch(k)
{
case 0: return "A";
case 1: return "B";
default: return "No value";
}
}


Thanks & Regards<br />Karthik
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Indeed, you have to guarantee that the method should return the appropriate value. Imagine k being 2, then nothing will be returned and therefore the compiler will complain of course.
Aniket Patil
Ranch Hand

Joined: May 02, 2006
Posts: 218
If a method containing a switch statement has a non-void return type and has no return statement, then each case of the switch statement must either have an appropriate return or a throw.


SCJP 5.0 | SCWCD 1.4 <br /> <br />If you don't know where you are going, any road will take you there!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About Switch Statements
 
Similar Threads
Switch and Assertion
Which is faster?
Difference between return, return ""
Can someone tell me why?
JWS - A Good Way To Demo Apps