Incremental Java
Printing to the Screen

Computation, Input, and Output

What's your impression about programming so far? Does it seem strange? We've been talking about expressions and boxes and how to read values from boxes and write to boxes.

Can this really be what programming is all about? Keeping track of many boxes?

That's certainly one way to view programming. Other clever computer people have come up with other ways to think about programming, but this kind of programming is rather common.

We seem to be missing one important piece of programming. The user!

Think about any program you've ever used. A spreadsheet, a browser, a game, instant messenger, a photo editing program, an MP3 player. All of these programs require you to interact with the program. You do something such as press a button, type a phrase, drag a scrollbar, paint a line, etc., and the program reacts.

This is called input. Input is information you provide to the program. The program reacts by changing what's on the screen. Maybe it prints a message, or turns the volume up when you drag the volume control, or lets you drag something. When the program provides information to you, that's output.

Most useful programs require input and provide output so it can interact with the user. A few programs have minimal interaction, but they are the exception rather than the rule.

It turns out that input is quite complex in Java, at least compared to output. In other languages such as C/C++, input is easier, at least, text input. Text input is when you type a response to the program. The program might ask for your age, and you might respond by typing it in.

Here's an example:

Enter in age: 23
Why, 23 is a mighty fine age!
The program's output is in black, and the user input is in red.

Until languages like Java came along, most programming used text input and output. Handling buttons and mice and scrollbars used to be complicated (it's still not that easy), and at one point, it was non-existent (think DOS). Text input and output used to be the only way you could interact with a program.

Java has made the task of making GUI (graphical user interfaces) easier, which allows users sophisticated ways to interact with the program. We'll see some of that.

However, while GUI programming requires a certain way of thinking, you can still understand how programming works without ever writing a program that has a button or a menu or a scrollbar, i.e., by just using plain text as input and output. We won't do that, but just keep in mind that programming is closer to manipulating boxes (in an organized way).

Which brings us to the simple goal of this lesson. You're going to learn how to print text output. Java makes this easy.

System.out.println

In Java, you can print a String to the screen.
    System.out.println( "Hello, World!" ) ;
Or an int.
    System.out.println( 42 ) ;
Or a double.
    System.out.println( 3.14159265358979 ) ;
In fact, Java allows you to print almost any type. Whether it does something sensible depends on the type. It usually handles all of the primitive types well, but has some problems with object types.

As you can tell, to print something you need:

    System.out.println( STUFF ) ;
That is, you need to type System.out.println, then an open parenthesis, then the stuff you want to print, then a close parenthesis, then a semicolon.

What is System.out.println? I'll explain it now, but I don't expect you to understand all of it. System is a class. out is a static instance variable of that class. println is a method of the type of out. We've used words like class, instance variable, and method. All of these should be new and wonderfully strange. We will explain these concepts soon enough.

For now, all you have to know is that if you want to print, you use System.out.println, followed by open parenthesis, the thing you want to print (it can be an expression), and close parenthesis.

The stuff between the parentheses is called the argument. This has nothing to do with fighting or arguing. It's just information you are giving to System.out.println so it knows what to print.

A Closer Look at println

println looks a little like print line. And so it is. This prints the argument, but then also prints a newline. OK, it doesn't really print a newline. A newline isn't a character (well, it is a character, but not a character that can be printed). Instead, it's a character that is a command. The command says to the screen, "Move the cursor to the beginning of the next line" (basically, equivalent to hitting return key).

System.out.print

System.out.println puts a newline character after the argument is printed to the screen. What if you didn't want to do that? Perhaps you wanted to print two pieces of information to the same line.

You should use System.out.print instead. This prints the argument, but doesn't print an additional newline.

Let's see an example:

  System.out.print( "Why, " ) ; // Using print with String argument
  System.out.print( age ) ;     // Using print with int argument
  // Using println
  System.out.println( " is a mighty fine age!" ) ; 
If age had the value 23, the output would be:
Why, 23 is a mighty fine age!

Concatenation and Mixed Types

What happens if you want to print something to the screen, but it has different types? In the previous example, we printed a String, an int, and another String.

Do we need three print statements (a print statement is either of the print or println variety)?

We don't. That's because string concatenation works with other types than strings. In particular, as long as one of the operands of + is a string, Java does string concatenation. This may require creating a string version of the non-string operand.

For example, we can write:

System.out.println( "Why, " + age + " is a mighty fine age!" ) ; 
Due to left associativity, Java evaluates "Why, " + age first. This creates a string version of the value of age. If age is 23, then it produces "23". The concatenation results in a new, temporary string "Why, 23".

"Why, 23" is then concatenated to " is a mighty fine age!" which evaluates to yet another new temporary string, "Why, 23 is a mighty fine age!". This is the final evaluated result, which is then given to System.out.println to print.

This is an interesting example for several reason. First, you can put an expression as the argument of System.out.println. Second, this expression is evaluated before System.out.println prints it out.

Third, and this is not at all obvious, Java can perform some shortcuts to printing. You may not realize it, but each time a concatenation occurs, a new string is created. This is considered slow. It would be better if Java would just create one big string once, made from all the three parts. It can do this. As a programmer, you don't have to care (at least not now). It doesn't hurt to think of Java doing the series of concatenations as we just described, even if, in reality, it may perform this concatenation is a more optimized way.

System.out.println prefers Strings

Even though System.out.println (and print) can handle any primitive type and String expression as argument, you will often want to take advantage of string concatenation.

This means that at least one subexpression has to be a String, preferably, the leftmost. One way to guarantee this is to use an empty string at the beginning of the concatenation. For example,

  System.out.println( "" + age + isTall ) ; 
where age is an int and isTall is boolean. By making "" the leftmost string being concatenated, we can force the + to stand for string concatenation.

Let's see why. "" + age does string concatenation since one of the operands is a string (namely ""). Evaluating this results in a new temporary string (say, "23"). This is then concatenated to isTall. Since the temporary string is a String, we again use string concatenation.

Recall that concatenation means "to put together".

Here's a rule of thumb. Use the empty string trick if the arguments you are using don't work. For the most part, you won't need to use this trick, because you'll print a string in the concatentation somewhere early.

This is the problem with +. It's overloaded. Its meaning depends on context. If we had some special operator such as @ that stood for string concatenation, we'd do even better off because then neither operand would have to be a String. It would figure this information from the operator.

Instead, because + is overloaded, Java has to figure out whether it's int addition or double addition or string concatenation.

Summary

System.out.println and System.out.print allows us to write programs that communicate with the outside world. These are output methods (we'll explain what a method is soon enough!). They print text messages to the screen.

String concatenation allows these methods to be flexible, where we can have different types printed, and string concatenation does the appropriate conversion to strings.

Use this simple method to print to the outside world!