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

From CompSciWiki
Jump to: navigation, search
Line 1: Line 1:
{{1010Topic|Introduction=Java uses '''System.out.''' for program output. This section will show you how to use System.out.println() and System.out.print() to print to the screen.|Overview=Programs would not be very interesting if they could not produce any output. Here you will learn how to output messages and data to the screen.|Chapter_TOC=[[Java Fundamentals]]}}
+
==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.
  
=Printing Text to the Screen=
+
<br>
To print text to the screen you can use:
+
 
+
'''System.out.println("text");'''
+
 
+
or
+
 
+
'''System.out.print("text");'''
+
 
+
Whatever you put inside of the quotes in place of ''text'' will be printed to the screen. The difference between the two is System.out.println() will print a newline after the text unlike System.out.print() which just prints the text as-is. The examples that follow demonstrate the usage of these two output methods.
+
 
+
==Example 1==
+
This program prints the message "Go Bisons!" to the screen.
+
 
<pre>
 
<pre>
public class Output1
+
int num = 5;
{
+
System.out.println("The number is: " + num);
  public static void main(String args[])
+
  {
+
    System.out.println("Go Bisons!");
+
  }
+
}
+
 
</pre>
 
</pre>
 +
<br>
  
==Example 2==
+
The result of this code is <b>The number is: 5</b>.
This program prints the message "Go Bisons!" to the screen three times, on separate lines.
+
<pre>
+
public class Output2
+
{
+
  public static void main(String args[])
+
  {
+
    System.out.println("Go Bisons!");
+
    System.out.println("Go Bisons!");
+
    System.out.println("Go Bisons!");
+
  }
+
}
+
</pre>
+
'''Output'''
+
<pre>
+
Go Bisons!
+
Go Bisons!
+
Go Bisons!
+
</pre>
+
  
==Example 3==
+
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.
Now, lets compare this program to the one in example 2. This program also prints the message "Go Bisons!" to the screen three times. However, it does not print a new line after each message.
+
<pre>
+
public class Output3
+
{
+
  public static void main(String args[])
+
  {
+
    System.out.print("Go Bisons!");
+
    System.out.print("Go Bisons!");
+
    System.out.print("Go Bisons!");
+
  }
+
}
+
</pre>
+
  
'''Output'''
+
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.
<pre>
+
Go Bisons!Go Bisons!Go Bisons!
+
</pre>
+
  
=Printing Variables with System.out.println()=
+
You can join as many items together as you wish, however it quickly becomes unreadable.
You can also print the contents of a variable by using System.out.println(). The following examples demonstrate how to do this.
+
  
==Example 1==
+
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:
<pre>
+
public class PrintVariable
+
{
+
  public static void main(String args[])
+
  {
+
    int num;
+
 
+
    num = 5;
+
 
+
    System.out.println(num);
+
  }
+
}
+
</pre>
+
  
'''Output'''
+
<br>
 
<pre>
 
<pre>
5
+
int num = 99;
 +
System.out.println("" + num);
 
</pre>
 
</pre>
 +
<br>
 +
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.
  
In this example we create an integer called '''num''' and use it to store the number '''5'''. We then print the contents of '''num''' by using System.out.println(). Notice that we did not put quotes around '''num'''. Variables do not need to be put in quotes. Only a [[Strings|string]] of text needs to be put inside of quotes.
+
Quotes are not always needed to give a string variable to the print methods either.
 
+
==Example 2==
+
In this example we will show you how to print a string of text followed by a variable.
+
  
 +
<br>
 
<pre>
 
<pre>
public class PrintTemp
+
string text = "Comp 1010 rocks!";
{
+
System.out.println(text);
  public static void main(String args[])
+
  {
+
    int temp;
+
 
+
    temp = 25;
+
 
+
    System.out.println("The temperature is currently " + temp);
+
  }
+
}
+
 
</pre>
 
</pre>
 
'''Output'''
 
 
<pre>
 
<pre>
The temperature is currently 25
+
System.out.println("Comp 1010 rocks!");
 
</pre>
 
</pre>
 +
<br>
 +
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.
  
Note that since only text in the quotes is printed, we need to include a space after '''currently''' to produce one in the output.
 
 
==Example 3==
 
We can also put the variable in the middle of a sentence.
 
<pre>
 
public class PrintTemp
 
{
 
  public static void main(String args[])
 
  {
 
    int temp;
 
 
    temp = 25;
 
 
    System.out.println("The temperature is currently " + temp + " degrees Celsius.");
 
  }
 
}
 
</pre>
 
 
'''Output'''
 
<pre>
 
The temperature is currently 25 degrees Celsius.
 
</pre>
 
  
We use a <code>+</code> sign to join a string of text and a variable. More information on creating Strings expressions can be found in the [[Strings#String_Expressions|string]] section.
+
More information on creating Strings expressions can be found in the [[Strings#String_Expressions|string]] section.

Revision as of 02:07, 5 December 2007

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.