Remember: The basic output statement in
Java is
System.out.println ( );
System.out.println(" text " );
will print what is between the double quotes “ “ and move the printing
cursor to the next line.
System.out.print ( “ text ”);
will print what is between the double quotes and leave the printing
cursor on the same line.
|
 |
When dealing with variables:
System.out.println( “ The total pay is “ + totalPay);
what is surrounded by " " is referred to as a "literal print" and gets
printed exactly. The "+" sign is the concatenator
operator and concatenates the string with the value that is
stored in the variable totalPay.
totalPay is declared as a
double, but is automatically converted to a String for
the purpose of printing out.
double
totalPay = 1006.5;
System.out.println("The total pay is " + totalPay); |
On the screen:
The total pay is 1006.5 |
You can print an arithmetic expression within a
System.out.print statement.
Use parentheses around the arithmetic expression to avoid
unexpected problems.
System.out.println("Adding ten to the
total: " + (totalPay + 10));
System.out.println("answer " + 3 + 4);
// becomes answer 34
System.out.println("answer " + (3 + 4));
// becomes answer 7
|