Results 1 to 7 of 7
- 06-19-2011, 06:13 PM #1
Senior Member
- Join Date
- Nov 2010
- Posts
- 164
- Rep Power
- 3
System.out.println versus return: Perspective?
Hello all,
this is not code specific per se, but I am going through the Java tutorials on oracle and I was wondering if those of you who are more advanced would be willing to offer me some perspective please?
In some cases, methods are declared as such:
I was wondering if it would actually display anything and why does it work to print something without a return. The method is void so it does not return anything, but the System.out.println still "returns" something by printing an output.Java Code:void printClassName(Object obj) { System.out.println("The object's class is " obj.getClass().getSimpleName()); }
Would you explain this to me? How do you view this concept? I'm looking for clarification as well as perspective.
Thank you!
- 06-19-2011, 06:40 PM #2
The print statement writes text to a console. That is not the same as a method returning a value to the caller of the method. There is no relationship between a method that returns a value and a method that writes a value to the console.why does it work to print something without a return
- 06-19-2011, 06:45 PM #3
Senior Member
- Join Date
- Nov 2010
- Posts
- 164
- Rep Power
- 3
Thank you.
So from a user's point of view, well from a coder's point of view,(please keep in mind that I'm a noob), which is more practical? If I want to call on a method to do something with the ultimate goal being to print it to the screen, should I place my system.out.println directly in the class where the method is declared or should I return a string to the class with the main method and then print it?
- 06-19-2011, 06:52 PM #4
The print statement is used to write to the console so that the user of the program can see it.
Where you put the print statements is up to you.
A method does something useful for the program and can return a value to the caller. For example if you want the square root of a number you call a method, give it the number and it will do some computations and return the computed value to you. At that point you could print it or you could use it in further computations.
How you write a method depends on the definition of what the method is supposed to do. The example you posted purpose was to print something. So it prints it.
- 06-19-2011, 06:55 PM #5
Senior Member
- Join Date
- Nov 2010
- Posts
- 164
- Rep Power
- 3
Got it. Thank you!
- 06-19-2011, 08:40 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,602
- Blog Entries
- 7
- Rep Power
- 17
It depends to whom or what something is returned; in a program context 'returning something' means returning a value to whatever called the method; the 'whatever' has to be program code. To a user a program 'returns' something if the program shows something on a screen in whatever form or makes a noise through a speaker etc. Stick to the first form: a method returns something to its caller, if you want to be generally understood. All the other phenomena are called 'side effects' and if a method is free of side effects it can be called over and over again (with the same parameter values if applicable) while it returns the same value over and over again and no speaker makes noise and nothing new is seen on the screen. Mathematical inclined people call those methods 'referential transparent', but those folks are weird ;-)
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-19-2011, 08:42 PM #7
Senior Member
- Join Date
- Nov 2010
- Posts
- 164
- Rep Power
- 3
Similar Threads
-
is System.out.print same as System.out.println ?
By usuf in forum New To JavaReplies: 8Last Post: 06-21-2011, 02:21 PM -
return list - rather than println()
By gcclinux in forum New To JavaReplies: 9Last Post: 04-28-2011, 03:41 PM -
System.out.println
By JohnDoe in forum New To JavaReplies: 1Last Post: 09-05-2010, 10:14 AM -
Println VS system.out.println
By ccie007 in forum New To JavaReplies: 2Last Post: 05-20-2010, 08:52 AM -
difference between system.out.println() & out.println()
By wickedrahul9 in forum Advanced JavaReplies: 5Last Post: 10-18-2008, 11:06 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks