Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

return in Switch case

 
pradeepta chopra
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


When i write a return statement in the Switch case,where does the control returns to??
the line 1 is not printed
 
Steven Landers
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "return" keyword always has to do with exiting the current method (with a few peculiarities regarding the finally{} block). "break" will exit a switch statement.

In this case, "return" returns the flow to the "caller of main" which in this case, would be the JVM, I imagine.

You can test this similarly by creating another function, with a return type of "void" - with this same switch statement. If you call this new function from main(), you'll note that the return statement "returns" the flow to main - and your //line 1 is never printed.
 
pradeepta chopra
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Steven Landers,ur suggestion works
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi pradeepta did you run the code?

i am getting java.lang.NoClassDefFoundError at runtime....
 
pradeepta chopra
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ofcourse i have run the code.
but i dont get any Exception,check for the directory and its path where you are storing the file
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI pradeepta.........

class k{
public static int main(String[] args)
{
int x=1;
switch(x+2){
case 4: System.out.println("4");
break;
default: System.out.println("default");
break;
case 3: System.out.println("3");
return ;
//break;
}
System.out.println("returned"); //line 1
}
}

i have compiled the same code which is posted by you with 1.5compiler.i am getting compilation error as:

missing return value
return ;
^
 
pradeepta chopra
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi GaneshKumar,
you probably have done a typing mistake.
make sure that it is return; //no space between return and;
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
return;

still it is giving the same compilation error

missing return value;
return;
^


 
pradeepta chopra
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thats because you have declared
public static int main()

and that requires you to return a value
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually i tried your code by returning some int value so that i put int as a return type....

ok i have changed it to void and compiled

but now it is compiled and throwing runtime exception as

NoClassDefFoundError



[ November 05, 2008: Message edited by: Ganeshkumar cheekati ]
 
pradeepta chopra
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have compiled your post's code with return; and void modifications ,it compiles fine and no Exception is thrown.
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
which compiler you are using?

what is the output?
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ganesh just do what pradeepta is saying. Change the return type of main to void...
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah i have done that.
i am getting NoClassDefFoundError.

but she got output for the same code.

i dont know why i am getting this exception?
[ November 05, 2008: Message edited by: Ganeshkumar cheekati ]
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ganeshkumar cheekati:
yeah i have done that.
i am getting NoClassDefFoundError.

but she got output for the same code.

i dont know why i am getting this exception?

Firstly NoClassDefFoundError is not an exception.

Where is the compiled .class file stored on your disk?
are you executing java k.class from that same location?
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah i got it now.
output is 3.
thanks to every one who has spended their time for me...


[ November 06, 2008: Message edited by: Ganeshkumar cheekati ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic