9.5: Displaying Strings
- Page ID
- 15211
You can put as many statements as you like in main
. For example, to display more than one line of output:
public class Hello { public static void main(String[] args) { // generate some simple output System.out.println("Hello, World!"); // first line System.out.println("How are you?"); // another line } }
Phrases that appear in quotation marks are called strings, because they contain a sequence of “characters” strung together. Characters can be letters, numbers, punctuation marks, symbols, spaces, tabs, etc.
System.out.println
appends a special character, called a newline, that moves to the beginning of the next line. If you don’t want a newline at the end, you can use print
instead of println
:
public class Goodbye { public static void main(String[] args) { System.out.print("Goodbye, "); System.out.println("cruel world"); } }
In this example, the first statement does not add a newline, so the output appears on a single line as Goodbye, cruel world
. Notice that there is a space at the end of the first string, which appears in the output.