GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes return statement doubt 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 "return statement doubt" Watch "return statement doubt" New topic
Author

return statement doubt

Cristi Tudose
Ranch Hand

Joined: Dec 25, 2000
Posts: 53
this is the code:
import java.io.*;
class Stdin{
private static BufferedReader reader=new BufferedReader(
new InputStreamReader(System.in));
public static String readLine(){
while(true) // (1)
try{ //(2)
return reader.readLine(); //(3)
}catch(IOException ioe){} //(4)
}
public static void main(String[] args){
readLine();
}
}
Could someone explain why if we comment (1)
we get a compile error(missing return statement) but
if we comment (2)-(4) we won't get a compile error
I also know that it is possible for a method to have
a declared return type and yet contain no return statements.
thanks in advance
rgds,
cristi
Vladimir Kositsky
Ranch Hand

Joined: Nov 03, 2000
Posts: 116
Nice example.
Compiler smart, eh?
Compiler checking return value in all options.
If method does not return String it complains. So let's giv'em the String!!!

IMHO it looks like compiler does not much care about return value from enternity loop. There is nothing in code that makes it suppose that this loop will be ever finished, so it does not complain about return value.
Following code compiles and runs nicely, and never ending( ooh, again restart prompt!)

Hopefully it is useful.
Cheers

[This message has been edited by Vladimir Kositsky (edited January 12, 2001).]
Cristi Tudose
Ranch Hand

Joined: Dec 25, 2000
Posts: 53
Hi Vladimir,
Thanks for your hints.
As we are talking about return statement and compile problem
could u tell me please how you understand the concept of
unreachable statement.How it works?I'm a little confused;
JSL states:

{
...
try{
return reader.readLine();//(1)
}catch(IOException ioe){return "hi"}//(2)
//return "d"; //(3)
}
From the code above,if (3) is uncomment we get a compile error
(unreachable stm.).I suppose bcoz in any options, we have a return stm,and compiler observe this.So,there is no execution path to (3)
If we remove return from catch and (3) is uncomment -ok.
but now,how the compiler could configure a path to (3)???
The same applies to return in finally clause-if there is a
stm. after try clause u get copile error(... maybe bcoz finally will always executed ...???)If u have return in catch -ok.
hope give me another hints and by the way what means IMHO ?
rgds,
cristi

[This message has been edited by Cristi Tudose (edited January 13, 2001).]
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Cristi,
When you declare a method with a return type you are making a contract with anyone using the method: you are guaranteeing the method will always return a result.
The compiler checks to make sure your code honours the contract.
It does this by checking the execution paths the code can take.
In your first example

The only place a <code>return</code> statement occurs is in the <code>try</code> block. <code>while(true)</code> guarantees the statement will be reached; the method will continue to loop until the <code>return</code> is executed.
When you comment out the <code>while(true)</code>; the <code>return</code> statement may not be reached if an exception occurs. There is no other way for a return to be made therefore you get a compile error.
If there was a return statement added to the <code>catch</code> block or contained in a <code>finally</code> block (which is always reachable) then the code would compile.
Hope this helps.
PS IMHO means "in my humble opinion"
Jane

Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Vladimir Kositsky
Ranch Hand

Joined: Nov 03, 2000
Posts: 116
Cristi, Jane:
again IMHO
try- catch block is like transformed if-else block.
there are two ways out, first if exception never occured, second if "do" occured. Both of them should return value type String in this case.
This is example of another unreachable statement, compiler can't find way to underlayed expression. This sneaky code reflect compiler's way of looking for dark corners ( kidding ).

Exception ALWAYS thrown, no way to reach printing statement!
Cheers!!!
V
[This message has been edited by Vladimir Kositsky (edited January 13, 2001).]
[This message has been edited by Vladimir Kositsky (edited January 14, 2001).]
Cristi Tudose
Ranch Hand

Joined: Dec 25, 2000
Posts: 53
Jane thanks for your promtness.
also,Vladimir
things come clear now
best rgds,
cristi
Vladimir Kositsky
Ranch Hand

Joined: Nov 03, 2000
Posts: 116
{
...
try{
return reader.readLine();//(1)
}catch(IOException ioe){return "hi"}//(2)
//return "d"; //(3)
}
Cristi,
parser returns after "return" of catch or "return" of try. It just no way - no third way in this statement, so (3) is unreachable.
V
 
GeeCON Prague 2014
 
subject: return statement doubt