Your first program
Problem
The computer does exactly what it says
The computer does not guess what you want. It runs commands, one after another, exactly as they are written. A sequence of such commands is a program.
Here is a whole program. It prints two lines on the screen:
public class Main { public static void main(String[] args) { System.out.println("Welcome!"); System.out.println("This is your first program."); }}Welcome!This is your first program.It looks like a lot of code for two lines of text. Most of it is a wrapper that you write in every program. Your commands are only the two lines in the middle.
The idea
What is in the program
public static void main(String[] args)is the place where the program starts. We call it main. The computer runs the lines in it from top to bottom.System.out.println(...)prints the text in the parentheses and moves to a new line.- The text is in double quotes:
"Welcome!". The quotes are not printed. - Every command ends with
;. - The curly braces
{and}group the lines that belong together.
public class Main is the wrapper of every program. You will learn what it means in the "Classes and objects" module. For now you copy it and write your code inside main. Later in the lessons we show only the lines from main.
More precisely, if you are curious
- Uppercase and lowercase letters matter.
system.out.printlnis not the same asSystem.out.println, and the program will not compile.
- The file is named after the class:
public class Mainis in a file calledMain.java. - From Java 25 on, a small program can also be written in a shorter way, without the class. Here we write the full form, because it works with every version and because that is how you will see it in other people's code.
The idea
Printing with and without a new line
Sometimes you want several pieces of text on one line. There is a second command for that: System.out.print. It prints the text and stays on the same line.
System.out.print("Order ");System.out.print("#1024");System.out.println(" is confirmed.");System.out.println("Delivery: tomorrow");Order #1024 is confirmed.Delivery: tomorrowThe first two lines of the code use print, so "Order", "#1024" and "is confirmed." come out together. After println the next text starts on a new line.
Pay attention to the spaces. They are inside the quotes, because the computer does not add a space by itself.
Follow the cursor: the text always comes out where it is.
System.out.print("Order ");System.out.print("#1024");System.out.println(" is confirmed.");System.out.println("Delivery: tomorrow");Step by step, in words
- print prints "Order " and the cursor stays on the same line.
- print prints "#1024" and the cursor stays on the same line.
- println prints " is confirmed." and the cursor moves to a new line.
- println prints "Delivery: tomorrow" and the cursor moves to a new line.
- The spaces are inside the quotes: the computer does not add a space by itself.
Try it
On how many lines?
What will the program print?
System.out.print("Train ");System.out.println("8:15");System.out.print("Platform ");System.out.print("3");System.out.println();After "8:15" there is println, so "Platform" is on a new line. The last line, System.out.println();, prints no text. It only moves to a new line.
The idea
Comments: notes for people
Sometimes you want to leave a note in the code: why it is like this, or what comes next. You write it as a comment. The computer skips comments.
//starts a comment that runs to the end of the line./*and*/surround a comment over several lines.
/* The coffee shop receipt. Prices are in euros. */System.out.println("Coffee €2.50");// System.out.println("Croissant €1.80");System.out.println("Total €2.50"); // out of croissantsCoffee €2.50Total €2.50The croissant line is code, but it is behind //, so it does not run. This is how you switch off a line for a while without deleting it.
A good comment says why, not what. The code itself shows what it does.
Try it
What will come out?
What will the program print?
System.out.println("9:00 Math");// System.out.println("10:00 Physics");System.out.println("11:00 History"); // lunch is nextThe whole second line is a comment, so "Physics" is not printed. On the third line the comment starts after `;`: the command runs, and the note after // is skipped.
Problem
The program does not start
You wrote the program, you run it, and it does not even begin. Instead of output you get a message:
public class Main { public static void main(String[] args) { System.out.println("Welcome to the course!") System.out.println("Here we go."); }}Main.java:3: error: ';' expected System.out.println("Welcome to the course!") ^1 errorBefore it runs, the program goes through the compiler. It translates the code for the computer and checks that it is written by the rules. If it is not, the compiler stops and does not run anything. This is called a compile error.
The idea
How to read the message
The message tells you three things: the file, the line and what is wrong.
Main.java:3 means the file Main.java, line 3. ';' expected means "I expected a ;". The ^ under the code points to the place where the compiler noticed the problem.
The most common errors at the start:
- a
;is missing at the end of a command; - a quote or a parenthesis is opened but not closed;
- a lowercase letter instead of an uppercase one:
system.out.println.
Fix the first error in the list first. One missing quote can confuse the compiler and show several more errors further down, which go away along with it.
More precisely, if you are curious
- The compiler tells you where it noticed the problem. Sometimes the error is a little higher up: if you forgot to close a
}, it finds out only at the end of the file. - There are also errors that nobody catches: the program starts, but it does something other than what you want. We will hunt those in almost every lesson.
Try it
Find the bug
The program should print the exam schedule. It does not compile. Which line is the bug on?
Pick the line with the bug.
); as part of the text too and cannot find the end of the command. The fix is System.out.println("Room: 204");.Exercise
Your first programs
Solve the tasks in your editor. Each one is a separate program. Compare your output with the sample, character by character.
1. Hello World. The traditional first program. Print:
Hello, World!2. Business card. Print a business card of three lines: name, job and email. You can use your own.
Maria Ivanovaweb developermaria@example.com3. Frame. Print today's cafeteria menu in a frame of asterisks. Watch the spaces, so the right side comes out straight.
**************** Today: soup ** €5.40 ****************More tasks
- One sentence in three parts. With three
printcommands and oneprintln, print on one line "The shop is open from 9:00 to 19:00." - A schedule with a comment. Print a schedule of four classes. Then switch off the third line with
//, without deleting it, and add a comment saying why.
- An error on purpose. Remove the
;from a line in task 1, then remove a quote as well. Read what the compiler says each time.
Test