CoSTARS Note #2:
Printing with print and println


1.  Using println to print a string

Here again is the HelloWorld program:

public class HelloWorld {
   public static void main(String[] args) {
       System.out.println("Hello World!");
   }
}
The program is highlighted in two colors:  gray and green.  We will discuss the gray part later.  For now, you just need to understand that you need the gray part in order for your program to run.  Here, we will focus on the green part, which is the part of your HelloWorld program inside the inner braces { ... }.

So the main part of our program is this one line:
      System.out.println("Hello World!");
What is going on here?  Simple.  System.out.println is a command that tells the computer to print out whatever is inside the parentheses.  That is:
     System.out.println("PRINT THIS OUT");
Note that you must type the command exactly like this -- with an upper-case "S" in System, with parentheses around the thing we print out, and with a semicolon at the end.  Also, if we are printing out a message, then we must place double-quotes (") around the message.

Some words to know:

Program:  A program is a set of instructions that direct the computer to perform a given task.

CodeCode is another word for a program or any part of a program.

Method:  We call System.out.println a methodA method is a command that the Java compiler understands.

Parameter:  We call "the thing that println should print" its parameterA parameter is a piece of information supplied to a method.  When you call a method, you place its parameters inside the parentheses after the method's name.

StringA string is a message inside double-quotes (").  For example:  "this is a string".  When you call the println method, you can supply a string as its parameter, and println will print out that string.

And now for some practice:

2.  Using println to print a number

We can also use the println method to print a number instead of a string.  Try running the following code, which prints the number 7:
      System.out.println(7);
Now, to run this code, you will have to replace the green code with this line of code, while keeping the gray code as-is.

Already, we have a powerful calculator at our disposal!  For example, we can print out the sum of two numbers:
      System.out.println(7+8);

Or the product of two numbers (that is, we can multiply two numbers):
      System.out.println(7*8);  // * is the multiplication operator

And these can be larger numbers, too:
      System.out.println(1234 * 5678);  // Here we multiply larger numbers

Or even larger numbers:
      System.out.println(1234567 * 7654321);  // Here we multiply even larger numbers

And now for some practice:

3.  What if we want to print a string and a number on the same line?

We have seen that we can use the println method to print out either a string like "Hello World" or a number like 7.  It turns out that we usually want to do both of these steps.  Why?  Because if we just print out a number by itself, its meaning is unclear.  For example, consider a program which prints out:
      168
What is this program doing?  Maybe it's telling us the distance in miles from Reno, Nevada, to Mammoth Mountain, California.  Or perhaps it's telling us the height (in feet above sea level) of the Palatine Hill over the ancient Forum in Rome?  Probably not, but how are we to know, since both answers are in fact 168.

Here is that same program, but with a bit more information being printed out:
      Hours in a week:  168
Ah, now it's clear!  By adding the text "hours in a week", the meaning of the number 168 becomes very clear.

So we see that we want to print out a string and a number one the same line.  Here is our first attempt:
      System.out.println("Hours in a week: ");
      System.out.println(24 * 7);

Unfortunately, this will not work.  It prints the right information out, but on separate lines.  This is because the println command not only prints out its parameter, but then it goes to the next line.

So:  we need a version of the println command that stays on the same line!

4. Using print instead of println to print a string and a number on the same line

Fortunately, we have one!  The print method is just like the println method, except that it stays on the same line.  So now we can write our hours-in-a-week program like this:
      System.out.print("Hours in a week: ");
      System.out.println(24 * 7);

Notice that the first line uses the print method and not the println method.  This prints out the string "Hours in a week: " but then stays on the same line, so that the 168 is printed out on the same line.  Just as we wanted!!!

And now for some practice:

We can also use the print command more than once on the same line.  You will need this for the following:

5.  Escape sequences:  or, how do we print double-quotes?

Here is a tricky question:  how do we print a double-quote (")?  We cannot put a double-quote inside a string, because the compiler would like the double-quote ends the string, and everything after the double-quote would be outside the string.  The compiler would be very confused!

So we need a way to tell the compiler to include the double-quote inside the string.  We do this by preceding the double-quote with a backslash (\).  The combination of a backslash and the double-quote (\") is called an escape sequence.  It tells the compiler to include the escaped double-quote in the string.  So, here is a line of code that prints out a double-quote:
      System.out.println("Here is a double-quote: \"");
Note that the escape sequence is highlighted in green.

Now it's your turn:

6.  Another Escape Sequence:  printing tabs

We can print a tab character with the escape sequence \t (backslash-t).  This prints some spaces so that the next character on the line will be at the next tab stop (usually at 8, 16, 24, 32, and so on).  Try this example which demonstrates using tabs -- see if you can guess what this will print out before you run it:
      System.out.println("These items are tab-delimited at 3-per-line:");
      System.out.println("abc\tde\tfgh");
      System.out.println("i\tj\tk");
      System.out.println("\"lm\"\t\tn");

Now it's your turn:

7.  Yet Another Escape Sequence:  printing backslashes

What if we want to print a backslash?  To see the problem, try this simple approach (which will not work)
      System.out.println("This fails to print a backslash:  \");  // WILL NOT WORK!

When you try to compile this, you get this error:  "Unclosed string literal".  What went wrong?   Look at the backslash in the string -- it is followed by a double-quote.  Recall that \" is the escape sequence to include a double-quote in your string.  So the backslash was not included in the string.  Instead, the backslash started an escape sequence.

So we need a way to include a backslash without starting an escape sequence.  To do this, we use another escape sequence!  We use backslash backslash (\\) to print a backslash, as such:
      System.out.println("This prints a backslash:  \\");  // Notice the escape sequence \\

Now it's your turn:

8.  More Practice

Do each of the following.  For each program, any numbers that you print out should include some clear explanatory text on the same line.  Also, any arithmetic should be done by Java, not by you.