JAVA / CHAPTER 01

Intro to Java

Beginner

Trace a small program from source code to printed output.

By the end of this chapter

  • Identify the main entry point
  • Trace statement order
  • Distinguish compilation from execution

The idea

Java source describes instructions that a compiler checks before execution. In these examples, save the program as Main.java, compile it with javac Main.java, and run it with java Main. The JVM begins at the conventional main method. Braces group statements, and a semicolon ends a simple statement. A compile error prevents that version from running; correct syntax alone does not guarantee the intended result.

Read the example from top to bottom. print writes text without ending the line, whereas println finishes the line. Text between double quotes is a string literal. Keep predicted output separate from source: punctuation in the program may organize instructions without appearing on screen.

Java example
public class Main {
  public static void main(String[] args) {
    System.out.print("Code");
    System.out.println("Quest");
    System.out.println(2 + 5);
  }
}
Result
CodeQuest
7

The first two calls share one output line. The last call evaluates addition before printing its numeric result.

Try it yourself

Print a two-line ticket containing a project name on the first line and the sum of two task counts on the second.

Use your own editor for this exercise. Runnable challenges on CodeQuest are available for JavaScript, Python, and SQL.

CHECK YOUR UNDERSTANDING

Chapter checkpoint

What is printed by these statements inside main?
System.out.print("A");
System.out.println("B");
System.out.print("C");