Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

return versus system.out.println  RSS feed

 
Jody Mathias
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the difference between return versus system.out.println? Return gets the info back to the caller, but does it ever print it out without a specific call to system.out.print?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jody,

Welcome to JavaRanch!

As you noted, return "gets the info back to the caller" and it also exits the method. But it has nothing to do with System.out.
[ December 13, 2005: Message edited by: marc weber ]
 
Scott Selikoff
author
Bartender
Posts: 4093
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's way too much to explain here, so I'll just point out a common example. In small java applications (simple command line tools) there *may* be many similarities between what you output using System.out.println() and what you return to the user.

For example, if you have a method add(int,int) you might return a single number to the user or output in one the screen.

Most applications though have varying degrees of complexity and System.out.println() is not enough to transmit all of the data cleanly. In other cases such as Swing Applications, Websites, etc, System.out.println() gets sent to the server's log file, but not to the user. So you would often have thousands of messages sent to a log file, none of which are ever seen by the user. The more complex the java application, the more obvious the differences between these two become.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
return never prints anything to the screen. You have to have an explicit System.out.println() call to do that.

Layne
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!