• Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference between system.out.print() and return?  RSS feed

 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here I have two methods:



How are they different? don't they both return the same thing? Why does the first have "void" in it as a return type when it actually outputs Hello? and why do return types have to be explicitly pointed out to java?

Thanks!
 
Steffe Wilson
Ranch Hand
Posts: 165
12
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This method prints "Hello!" to the console.
It does not return anything to the caller (hence void return type).



This method does not print anything to the console.
It returns the string "Hello!" to the caller (hence String return type).

Java has to be told what the return type is so that the java compiler can check that (a) the method itself returns the right type of value and (b) any methods that call this method expect the right type of return value. Strongly typed languages like java use this approach to help avoid run-time errors.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should also know that it is legal, and sometimes preferable, to use a return in a void return type method.

Notice that the return has nothing after it. This is necessary with a void return type.

You should also know that some people don't like this pattern, but I'll let them speak for themselves.
 
Junilu Lacar
Sheriff
Posts: 11160
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:You should also know that some people don't like this pattern, but I'll let them speak for themselves.

It's a perfectly valid pattern and it's called the Guard Clause or Sentinel. People object to it probably are sticklers for the rule of having "One exit point" at the end, insisting on following the letter rather than the spirit of the rule. One exit point rule helps you avoid bugs caused by the complexity created by multiple exit points. Multiple exit points make it harder to trace the flow of execution, especially when you have nested loops and branches involved. The key is to keep the sentinels relatively small and simple, and focused on ensuring that the proper conditions are present to make it worth allowing execution of the method to proceed. Otherwise, you just kick it back to the caller.

Guard clauses can also throw IllegalArgumentExceptions to make it clear that the method's contract for use has been violated.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your first example is like hiring someone to yell the word "hello" every time you poke them.

Your second example is like hiring someone to hand you a slip of paper with something written on it each time you poke them...and it just happens to always have the word "hello" on it. You can then do whatever you want with that slip of paper. you can yell what it says out loud, you can hand that to someone else, or you can simply throw it away.
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:. . .Guard Clause or Sentinel. People object to it probably are sticklers for the rule of having "One exit point" . . .
It is very easy to write that method to avoid multiple returns:-So you get the guard clause without multiple returns simply from a little tinkering with the condition.
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The point Junilu is making is that code like that can become very annoying to read when it's several scopes deep.

Check out "Arrow anti-pattern".
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!