Difference between revisions of "Output using System.out."
Line 1: | Line 1: | ||
− | + | ==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. | ||
− | + | <br> | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
<pre> | <pre> | ||
− | + | int num = 5; | |
− | + | System.out.println("The number is: " + num); | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
</pre> | </pre> | ||
+ | <br> | ||
− | + | The result of this code is <b>The number is: 5</b>. | |
− | + | ||
− | < | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | </ | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | 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. | |
− | You can | + | |
− | + | 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: | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | <br> | |
<pre> | <pre> | ||
− | + | 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. | ||
− | + | Quotes are not always needed to give a string variable to the print methods either. | |
− | + | ||
− | + | ||
− | + | ||
+ | <br> | ||
<pre> | <pre> | ||
− | + | string text = "Comp 1010 rocks!"; | |
− | + | System.out.println(text); | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
</pre> | </pre> | ||
− | |||
− | |||
<pre> | <pre> | ||
− | + | 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. | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | 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.