Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By Norm

Thread: System.out.println versus return: Perspective?

  1. #1
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    167
    Rep Power
    5

    Default 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:

    Java Code:
    void printClassName(Object obj) {
        System.out.println("The object's class is "
                              obj.getClass().getSimpleName());
    }
    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.

    Would you explain this to me? How do you view this concept? I'm looking for clarification as well as perspective.

    Thank you!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    why does it work to print something without a return
    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.

  3. #3
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    167
    Rep Power
    5

    Default

    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?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    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.
    bigsonny likes this.

  5. #5
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    167
    Rep Power
    5

    Default

    Got it. Thank you!

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by bigsonny View Post
    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?
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    167
    Rep Power
    5

    Default

    Quote Originally Posted by JosAH View Post
    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,

    Jos
    This is a very fresh perspective. Thank you.

Similar Threads

  1. Replies: 8
    Last Post: 06-21-2011, 03:21 PM
  2. return list - rather than println()
    By gcclinux in forum New To Java
    Replies: 9
    Last Post: 04-28-2011, 04:41 PM
  3. System.out.println
    By JohnDoe in forum New To Java
    Replies: 1
    Last Post: 09-05-2010, 11:14 AM
  4. Println VS system.out.println
    By ccie007 in forum New To Java
    Replies: 2
    Last Post: 05-20-2010, 09:52 AM
  5. difference between system.out.println() & out.println()
    By wickedrahul9 in forum Advanced Java
    Replies: 5
    Last Post: 10-19-2008, 12:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •