Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

return statement  RSS feed

 
Ranch Hand
Posts: 227
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between return and return 1?

I think return will return void value and

return 1 will return 1 . Anything else ?
 
Saloon Keeper
Posts: 1800
74
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a void method then you can use return; but you do not have to and many times people don't.

If you have a method which returns a value that you need to use return JavaObject; in that method.
The returned value can be an valid Java object including exceptions, primitives and user defined objects.
 
Marshal
Posts: 59742
188
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The return; statement on its own doesn't return void; it transfers program control back to whichever code called the current method. It should be obvious that it can only be used in a method with void instead of a return type.
The statement return 1; both transfers control back to the calling method and leaves the value 1 on the stack for the calling method to use; it can only be used in a method with a return type consistent with int.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!