Output in Java Playground
The Java Playground is great for trying out segments of code, but if we write print
statements to show that the output doesn't move to the next line unless you wrap the statements into a block of code using {
at the start and }
at the end. Without the statements being included in a block of code, it will treat each print
statement distinctly and not behave as expected. Try the following in the Java Playground.
{
print("Let's play with Java! ");
print("Java is fun!");
}
Prepare Your IDE
Another option is to move over to an IDE. If you need help, please reference the Getting Set-up page.
In this section, we will be using preview features in JDK 24. To use features that are still in preview, you will need to enable preview features in your IDE.
- Create a
.java
file. The file name should start with an uppercase letter.
- Use the following
main
method template. Add your statements to the body of the main
method. A comment is in the body of the main
. A comment is ignored by the compiler. Comments are added to the program code to help programmers understand what the program code does. There are three types of comments:
- single-line comments start with
//
- multi-line comments start with
/*
and end with */
- document comments, which generate documentation, start with
/**
and end with */
void main() {
//add your statements here
}
Practice Set 1 - Predict the Output of Code
For each of the following code segments, first predict what will be generated as output and then test the program code in an IDE. (If you choose to use the Java Playground, remove void main()
as the Java Playground does not require you to use a main method.)
Using Escape Sequences
A. Given the following code, what is the output?
void main() {
print("She said, \"How are you?\"");
print("\nHe replied, \"I'm great!\"");
}
B. Given the following code, what is the output?
void main() {
int totalCustomers = 255;
int todaysCustomers = readln("Enter how mnay customers were served today");
totalCustomers = totalCustomers + todaysCustomers;
print("Today we served : \n\t" + todaysCustomers + " customers.\n");
println("The total number of customers served is " + totalCustomers);
}
C. Given the following code, what is the output?
void main() {
String message = """
Hello Karen,
Confirming your appointment for 6:00pm.
Sincerely,
Frank
""";
print(message);
}
D. Given the following code, what is the output?
void main(){
String message = """
Name Age
Brad 17
Jen 18
Susan 15
""";
print(message);
}
A - Displaying a Game Score
Player Score
1 150
2 100
3 95
B - File Path
C:\Name\JavaPrograms\OutputPractice.java