Input/Output using System.out.

From CompSciWiki
Jump to: navigation, search

COMP 1010 Home > Java Fundamentals


Introduction

One method of interacting with a user is through the command line interface (The black screen with the white righting, looks like DOS). Java has built in functionality to do this using the System package.

   

{{{Body}}}

Giving the User Information

Sending information to the user through the command line interface is a lot like printing to paper. You tell the computer what to print (with the right method) and then it prints what you tell it too on the screen for the user to read. Printing can be done string by string (strings can be any character(s)) or line by line.

Code

System.out.print("Hello ");
System.out.println("World!");

What it Does

The code shows the two methods for printing to the screen. First of all, the System.out portion says you are using the out section of the System package. A package is a chunk of code supplied to you, usually by the creators of Java, that contains sections of methods to do various tasks. In this case the task is printing on the screen.

The first line uses the print method. It takes 1 argument, a string, and prints it on the screen. It prints exactly the string as shown.

The second line uses the println method. It is referred to as the printline method. It is exactly the same as the print method however it appends a line return at the end, so any printing done after it will appear on the next line.

To contrast the two methods, think about a word processing program like Word. The print method will print a word or words and leave the cursor right next to the last thing you typed. The println method will print a word or words and press the enter button so the cursor is on the next line.

Printing More Than Just Text in Quotes

Now that you know how the two methods work, here are some examples demonstrating how to print more than one kind of argument. Remember, ultimately, the type of the argument in the print methods must always be a string.


int num = 5;
System.out.println("The number is: " + num);


The result of this code is The number is: 5.

There are a couple key things going on here. First of all, the argument contains a string in quotes, but also an int variable. Secondly there is a plus sign between the two. These two things work together to make things happen.

Firstly, the plus sign is telling the compiler to join the two items, the string in quotes and the int variable, and together they makeup a single argument for the println method. Secondly, the println method exclusively takes a string argument. What happens is, by using the plus sign to tell the compiler to join the two items, you are also telling it to implicitly cast the int variable to its string value. For all int's this is just its value (i.e. 5 is "5" and -987654321 is "-987654321" and so on). The int variable is first cast to a string so it is allowed to be joined to another string, and together they form a single string argument for the print method.

You can join as many items together as you wish, however it quickly becomes unreadable.

The next likely question is what if we want to print a number, but no words? How do we convert the int to a string? The answer involves a little trick as shown below:


int num = 99;
System.out.println("" + num);


This code works exactly the same way, except this time we don't have anything between our quotes. Why do we need this? Why can't we just have the num part? Well if we just gave the println method an int argument, it would give us an error because it only takes strings. By including the empty string "" we are telling the compiler we want to cast the int variable to a string again, but this time we want to join it with nothing, we want the number by itself.

Quotes are not always needed to give a string variable to the print methods either.


string text = "Comp 1010 rocks!";
System.out.println(text);
System.out.println("Comp 1010 rocks!");


The two above examples of code would produce identical results. There are several ways to give the print methods a string argument. We can define it in a string variable, like the first example, and give it the variable, or we can define it right in the argument itself as in the second example.


More information on creating Strings expressions can be found in the string section.