Difference between revisions of "Output using System.out."

From CompSciWiki
Jump to: navigation, search
(Updated java doc links)
m (C:\> should be on its own line)
Line 36: Line 36:
 
C:\>javac PrintDemo.java
 
C:\>javac PrintDemo.java
 
C:\>java PrintDemo
 
C:\>java PrintDemo
Hello WorldHello WorldHello WorldC:\>
+
Hello WorldHello WorldHello World
 +
C:\>
 
</pre>
 
</pre>
  

Revision as of 11:44, 1 December 2011

COMP 1010 Home > Java Fundamentals


Introduction

Programs would not be very interesting if they could not interact with the user. This section covers how to use the Java System.out function to display a message to the user within a console window. We will start off by display general text, then move on to combining text and numbers to create complex output.

Giving the User Information

Console.png
One method of interacting with a user is through the console, which is command line interface. Java can write to the console using methods within the System package. Sending information to the user through the command line interface is a lot like printing to paper. You tell the computer what to print and then it prints your message to the console for the user to read.

The System package contains two methods that allows you to write a String to the console window: System.out.print and System.out.println.

System.out.print

The System.out.print[[1]] method call is use to write a String to the console window. As you can see in the code block below, to write to the console all you need to do is put the String inside the brackets. When we look at the output however, we see that the command line prompt is right next to the text we are trying to show the user - this makes it hard to read. Although there are situations where this is beneficial, it would be better to use System.out.println.

Code

public class PrintDemo
{
    public static void main(String args[]) 
    {
        System.out.print("Hello World");
        System.out.print("Hello World");
        System.out.print("Hello World");
    }
}

Output

C:\>javac PrintDemo.java
C:\>java PrintDemo
Hello WorldHello WorldHello World
C:\>

System.out.println

The System.out.println[[2]]</code> method call is almost identical to the System.out.print method, except it add a line terminator, new line character, to the end of the String. As you can see in the code below it is used exactly the same way. In the output, each String appears on its own line.

Code

public class PrintDemo
{
    public static void main(String args[]) 
    {
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
    }
}

Output

C:\>javac PrintDemo.java
C:\>java PrintDemo
Hello World
Hello World
Hello World
C:\>

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. Even though you probably haven't worked much with variables you can still get the general idea here.

public class PrintNum
{
    public static void main(String args[]) 
    {
        int num = 5;
        System.out.println("The number is " + num);
    }
}
C:\>javac PrintNum.java
C:\>java PrintNum
The number is 5
C:\>

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:

public class PrintNum
{
    public static void main(String args[]) 
    {
        int num = 99;
        System.out.println("" + num);
    }
}
C:\>javac PrintNum.java
C:\>java PrintNum
99
C:\>

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 int part? Well if we just gave the println method an int argument, it would give us an error because it only takes String. 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.

public class PrintRocks
{
    public static void main(String args[]) 
    {
        String text = "Comp 1010 Rocks!";
        System.out.println(text);
    }
}
C:\>javac PrintRocks.java
C:\>java PrintRocks
Comp 1010 Rocks!
C:\>

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.

Previous Page: Comments Next Page: Input/Output using JOptionPane