Basic Java Language: Difference between revisions
wikademia>Lurkingfate →Using the Displayer with Variables: Clean-up, changed the section heading. |
wikademia>Lurkingfate →System.out: Cleaned up code examples |
||
| Line 318: | Line 318: | ||
Using System.out.println() is extremely simple, though C/C++ programmers may find it cumbersome to type up compared to simply typing printf(). The method println() takes one parameter, the variable you wish to output, and writes it to the console. | Using System.out.println() is extremely simple, though C/C++ programmers may find it cumbersome to type up compared to simply typing printf(). The method println() takes one parameter, the variable you wish to output, and writes it to the console. | ||
< | <source lang="java">System.out.println( myInt );</source> | ||
You can also print out multiple variables on the same line: | You can also print out multiple variables on the same line: | ||
< | <source lang="java">System.out.println( false + " " + 54 );</source> | ||
Note that this won't work for two literal numeric values: | Note that this won't work for two literal numeric values: | ||
< | <source lang="java">System.out.println( 54 + 32 );</source>. | ||
The plus sign would be mistaken for the addition operator for adding numeric values. So to print out "5432", you would have to type: | The plus sign would be mistaken for the addition operator for adding numeric values. So to print out "5432", you would have to type: | ||
< | <source lang="java">System.out.println( 54 + "" + 32 );</source> | ||
Now you can do calculations and print the result! For example, say you want to print 6*6*6*6. Here is the code: | Now you can do calculations and print the result! For example, say you want to print 6*6*6*6. Here is the code: | ||
< | <source lang="java"> | ||
public class Multiplying | public class Multiplying | ||
{ | { | ||
| Line 343: | Line 343: | ||
} | } | ||
} | } | ||
</ | </source> | ||
== Compiling and running your program == | == Compiling and running your program == | ||