CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Java and JSP (http://www.codingforums.com/forumdisplay.php?f=54)
-   -   What System.out.println() print out when it has no data? (http://www.codingforums.com/showthread.php?t=261924)

bingowinner 05-19-2012 02:08 PM

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?

alykins 05-19-2012 02:53 PM

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

alykins 05-19-2012 02:57 PM

Quote:

Originally Posted by bingowinner (Post 1230689)
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");)

Fou-Lu 05-19-2012 03:46 PM

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.


All times are GMT +1. The time now is 10:10 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.