Print statements |
| BASIC |
There are three kinds of print statements:
System.out.print(argument) just prints out its argument,
andSystem.out.println(argument) prints out its argument
and ends the line. A third kind, System.out.printf(format, arguments),
gives more control over how things are printed.
Example Result System.out.print("one"); System.out.print("two"); System.out.println("three"); onetwothreeSystem.out.println("one"); System.out.println("two"); System.out.println("three"); one two threeSystem.out.println();[new line]
A print "statement" is actually a call to the print or
println method of the System.out
object. The print method takes exactly
one argument; the println method takes one argument or no arguments.
However, the one argument may be a String which you create using
the string concatenation operator +. If you ask to print an
object, the print and println methods call that object's toString() method
to get a printable string.
Example Result int x = 3; int y = 5; System.out.println(x + " and " + y + " is " + (x + y)); 3 and 5 is 8 int x = 3; int y = 5; System.out.println(x + " and " + y + " is " + x + y); 3 and 5 is 35
Anything concatenated ("added") to a string is first converted to a string
itself. In the second example above, the x is concatenated with the string,
then the y is concatenated.