• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Return Statement

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm new to java , learning it through thinkjava. I don't understand the concept behind return statement given in the book. Could someone care to explain me with some good examples?.


Example mentioned in the book.

 
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Sheriff
Posts: 17357
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanraj Danz wrote: I don't understand the concept behind return statement given in the book. Could someone care to explain me with some good examples?


It would help if you formatted the code properly.
 
Junilu Lacar
Sheriff
Posts: 17357
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you have on lines 2 to 5 is called a Sentinel or Guard Clause. Under the conditions specified by the guard clause, attempts to enter the method will be rejected or turned away so that the rest of the method does not get executed, kind of like how a sentry prevents unauthorized access to a secure area.

This construct allows you to keep logic in the method simpler and more straightforward by reducing the number of levels of nesting/indentation in the code. It's a common strategy used to avoid or fix the Arrow Code Antipattern.
 
Sheriff
Posts: 8546
615
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanraj Danz wrote:I don't understand the concept behind return statement given in the book.

Welcome to Ranch.

In simple, "return" means return to the caller immediately (to the place, where the method has been called from), without executing the code below.
Examples below are just for presentation purpose, not showing a good practices.

1st Example - it exits the method and goes back to the caller once "if" statement's condition is satisfied.2nd example - returns only when all "if" statements are checked.
 
I love a woman who dresses in stainless steel ... and carries tiny ads:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic