Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Oct 2011
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    What System.out.println() print out when it has no data?

    Hi, so this seems like a very dumbr question...

    What does System.out.println(); print out when no data is passed to it? Is it just regular whitespace? It's not a linebreak or space character?

    I ask because I've got an exam in a few days, and I'm just going over a past paper. Some of the questions say, "If you think nothing is printed out, then write no output.", however, System.out.println(); is placed in such a way that even if, say an loop through an array didn't print anything [meaningful] out, there will always be a System.out.println(); outside of the array to print, but no data is passed to it - kinda to catch people out?

    I tested this in Eclipse and within the command line area, there is whitespace printed out. Do you believe this would be considered something printed out?
    Last edited by bingowinner; 05-19-2012 at 02:12 PM.

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,724
    Thanks
    41
    Thanked 191 Times in 190 Posts
    Code:
    System.out.println("begin...");
    for(int i =0; i <=2; i++)
    {
      System.out.println("cat");
      System.out.println("nip");
      System.out.println();
    }
    System.out.println("end...");
    output

    Code:
    begin...
    cat
    nip
    
    cat
    nip
    
    cat
    nip
    
    end...
    if you are in a class, and having an exam, then that means you should have been trying code, practicing, etc- all that aside- it means you should have an IDE (eclipse, netbeans, etc)

    in short- you could have (and still can for extra practice/combinations) coded this real quick and seen the answer

    you need to expose yourself to as many code situations as possible.

    edit:
    the code block for output puts in some extra line breaks after "end..." it would not do extras, just one

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #3
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,724
    Thanks
    41
    Thanked 191 Times in 190 Posts
    Quote Originally Posted by bingowinner View Post
    I tested this in Eclipse and within the command line area, there is whitespace printed out. Do you believe this would be considered something printed out?
    sorry, just saw that line oops-
    I would write that it writes a line break to the console (or a new line feed)... or to be more technical saw there is a line feed or you could even be slightly 'cocky' and say that it prints the escape \n (note not "\n" bc that would be System.out.println("\\n")

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    println is equivalent to using System.out.print(whateverdata + '\n') or System.out.print(whateverdata + "\n"). Java considers \n as a valid linefeed either inline with double quotes similar to C printf, or with single quotes denoting it as a char and not a string. When provided with no data, or data that is empty, it will print only a linefeed.
    So:
    Code:
    System.out.println();
    System.out.print('\n');
    System.out.print("\n");
    Would print three empty lines.


  •  

    Posting Permissions

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