• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Return keyword

 
Juan Manuel Alberto de los Santos
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


What is the use of return when is used like that ?
 
Henry Wong
author
Marshal
Pie
Posts: 21003
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the way it is written, it probably has no value. But if the condition did more than "if true". And there were code after the conditional... then this would be a way of conditionally leaving the method.

Henry
 
Juan Manuel Alberto de los Santos
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Conditionally leaving the method ?

returns to main ? it leaves main ?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34198
340
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's consider a slightly more realistic example:

In this example, the return statement gets you out of the main method without executing any code after the if statement. This is still a bit contrived. A better example would call System.exit() so it could pass a return code back to the caller. "return" tends to be used more within methods other than the main method.
 
Juan Manuel Alberto de los Santos
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
understood !

thank you
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's how it works; whether it is good programming is a different matter. There is a brief discussion in JavaRanch here.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic