• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Difference between return, return ""

 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great !! Thanks
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic