• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Difference between return, return ""  RSS feed

 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, What is the difference between return statements at Line 9 and 11. what I understand is, At Line 11, we are returning empty String..What about Line 9.

Also, In general, what is the difference between return, return ""

 
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
return -- it end the execution of method. void return type method has return as a last statement implicitly.

return "" --- returns a appropriate type[here an String object] to a method .

hope this helps
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great !! Thanks
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Harikrishna Gorrepati wrote:Great !! Thanks


You are welcome
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Please advice why Line 11 is unreachable. Line 9 return statement is part of switch and Line 11 is part of String f() method return type know ?
 
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Harikrishna Gorrepati wrote:Hi,

Please advice why Line 11 is unreachable. Line 9 return statement is part of switch and Line 11 is part of String f() method return type know ?


No. 9th line finishes the method.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Harikrishna Gorrepati wrote:Hi,

Please advice why Line 11 is unreachable. Line 9 return statement is part of switch and Line 11 is part of String f() method return type know ?


A switch statement will fall through all cases until a break is encountered. In this case your switch has a default which has a return and none of your cases have a break. Therefore whether you match a case or not, your switch will always eventually fall through to the return in your default case.
 
Sheriff
Posts: 9643
42
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mark Kramer wrote:A switch statement will fall through all cases until a break is encountered. In this case your switch has a default which has a return and none of your cases have a break. Therefore whether you match a case or not, your switch will always eventually fall through to the return in your default case.


Actually that won't happen as all the case labels themselves have return statements in them. The thing is, whatever the value of i is passed to f method, your code will not be able to reach line no 11...
 
Mark Kramer
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ankit Garg wrote:Actually that won't happen as all the case labels themselves have return statements in them. The thing is, whatever the value of i is passed to f method, your code will not be able to reach line no 11...

You're right, I looked at it too quickly. The last return cannot be reached because, regardless of the value of i, a return will be executed somewhere in the switch statement.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!