• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

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

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
WeiJie Lim
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 .

 
lowercase baba
Posts: 13074
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'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.
 
Marshal
Posts: 76462
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
The City calls upon her steadfast protectors. Now for a tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic