• 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
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

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 ?
 
Bartender
Posts: 1856
81
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: 61702
193
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!