• Post Reply Bookmark Topic Watch Topic
  • New Topic

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: 1441
43
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: 58831
179
  • 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!