Step 1 of 5 · Reading · ~3 min
Learn
Getting Started
Hello, World
Every Java program is a class with one special method the JVM starts from. By the end of this lesson you can print a line, print part of a line, and build an output line out of fixed text plus a value held in a variable.
The smallest program
public class Main {
public static void main(String[] args) {
System.out.println("Starting up");
}
}
Read it from the outside in:
public class Main-- all Java code lives inside classes; there are no free-floating functions as in Python or JavaScript. This platform compiles your file as classMain, so keep that name.public static void main(String[] args)-- the entry point, and the JVM looks for exactly this signature.publicso the JVM can reach it,staticso it can run before any object exists,voidbecause it returns nothing,String[] argsfor command-line arguments (unused here, but required).System.out.println(...)-- write the argument to standard output, then end the line.Systemis injava.lang, which every file imports automatically.
The ceremony is deliberate: from line one, every program has an unambiguous starting point and every statement has a home. Small scripts pay a tax; large codebases collect the refund.
print versus println
println writes the text and a line break. print writes only the text, so the next output continues on the same line:
System.out.print("3 ");
System.out.print("2 ");
System.out.println("1");
System.out.println("liftoff");
3 2 1
liftoff
The first three calls build one line, and only the println on "1" ends it. Change that call to print and the output becomes 3 2 1liftoff -- two lines silently fused into one, which an exact-match grader will reject. Every line you intend to be a line must end with println (or a %n, which a later lesson covers).
Joining text and values with +
+ between a string and anything else builds a longer string. This is how a fixed message and a value from the program share one line:
int points = 42;
System.out.println("Score: " + points + " points");
Score: 42 points
The quotes mark literal text; points without quotes is the variable. Put points inside the quotes and you print the word, not the number. A variable holding text works the same way: "Hi " + user joins the two.
Where input comes from
Most exercises here read standard input. The starter code already does it with a Scanner and gives you a variable to work with, so for now treat a line like String name = sc.nextLine(); as "the program received a line of text and stored it in name". Scanner gets its own treatment in the next lessons.
What the compiler says
system.out.println("hi"); // error: cannot find symbol -- Java is case-sensitive; it is System
System.out.println("hi") // error: ';' expected -- every statement ends with a semicolon
System.out.println('hi'); // error: unclosed character literal -- single quotes hold ONE char
One more sneaky one: public static void Main(...) with a capital M compiles, because it is a perfectly legal method, but the JVM then reports that no main method was found.
Common mistakes
- Using
printwhereprintlnwas needed. Two intended lines come out as one fused line. - Quoting a variable.
"name"prints the five letters n-a-m-e;nameprints what it holds. - Missing spaces around joined pieces.
"Hello," + nameprintsHello,Ada; the space has to be inside the quotes. - Forgetting the
;. The error message points at the line after the real problem.
Your exercise
Read one line, a name, and print a two-line greeting built from it. The starter already reads the name; the two output lines are yours. The mistake the tests catch is gluing the lines together or dropping a space or punctuation mark.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…