wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Statement in Try statement, method that has two return values ? 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 » Java » Beginning Java
Bookmark "Statement in Try statement, method that has two return values ?" Watch "Statement in Try statement, method that has two return values ?" New topic
Author

Statement in Try statement, method that has two return values ?

WeiJie Lim
Ranch Hand

Joined: Sep 05, 2012
Posts: 85


System.out.println("11"); is causing error of "unreachable statement" because a method can only have one statement that returns something ? I want to correct my understanding on this.

In this case, there are two statements that return something, return sc.nextInt(); and System.out.println("11"); .

Thanks in advance.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

WeiJie Lim wrote:System.out.println("11"); is causing error of "unreachable statement" because a method can only have one statement that returns something ?

No. The System.out.println statement is after the return statement, so that it will never be executed. The compiler detects this and gives you an error.

WeiJie Lim wrote:In this case, there are two statements that return something, return sc.nextInt(); and System.out.println("11"); .

No. The System.out.println statement prints something on the console, but it is not a statement that returns a value from the method. Only with the "return" keyword you can return a value from a method.

Printing something on the console and returning a value from a method are two totally different things. Why do you think that a System.out.println method returns anything from a method?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
WeiJie Lim
Ranch Hand

Joined: Sep 05, 2012
Posts: 85
Jesper de Jong wrote:
No. The System.out.println statement is after the return statement, so that it will never be executed. The compiler detects this and gives you an error.

No. The System.out.println statement prints something on the console, but it is not a statement that returns a value from the method. Only with the "return" keyword you can return a value from a method.

Printing something on the console and returning a value from a method are two totally different things. Why do you think that a System.out.println method returns anything from a method?


Oh noted, didn't know anything after the return statement is not executed =) . Thanks for this.

Oops, I guess my understanding of return statements is flawed. I assumed println prints out a value, so it is counted under returning a value .

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11152
    
  16

'return sc.nextInt();' means "leave the method RIGHT NOW, and send back the value we get from sc.nextInt()".

There are some subtleties with try/catch/finally statements, but within a given block, return immediately leaves that block.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37907
    
  22
As you will see here, a lot of people believe a return statement should be the last statement in the method and should appear nowhere else.

A lot of people disagree with that, but you can see how much confusion that multiple return has caused.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Statement in Try statement, method that has two return values ?
 
Similar Threads
What is wrong in this code ?
Unexpected Recursion
how do i input
Calculating the sum of numbers in a user defined range.
how does the hasNextInt() work in the following situation?