Java tutorial for beginners: your first program, explained.
Start with a complete Java program, then change it to store values and calculate a result. This lesson explains the basic structure before asking you to use it, so you can understand each line instead of memorising a template.
Start with a program that prints one line
Java source code describes instructions using a defined syntax. The example below uses a class named Main and a method named main. A method is a named block of instructions. In this program, main is where execution begins.
Save this complete example as Main.java in a Java development environment, or use a Java editor that provides a Run control. Keep the capital M in the filename and class name. Read the expected output before running it.
public class Main {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}| Output |
|---|
| Hello, Java! |
Why this works
System.out.println(...) writes a value to the standard output and moves to the next line. The double quotation marks identify text. They are part of the code, so they do not appear around the printed message. The semicolon ends the print statement.
Understand the main method and braces
The outer pair of braces encloses the class. The inner pair encloses the main method. The print statement belongs inside that method. Indentation makes these relationships easier to see; braces define the blocks.
In this familiar form of a Java entry point, public makes the method accessible, static lets it run without first creating a Main object, and void means the method does not return a value. String[] args is a parameter that can receive command-line arguments. This example does not use those arguments.
You do not need to write your own objects or handle arguments to change the message. Keep the surrounding structure while you practise the statements inside main. As you learn methods and classes, you can return to the role of each part.
Add variables and calculate a total
A variable is a name for a value used by the program. Declare its type and name before using it. String is used here for text; int is used for whole numbers. The equals sign gives each variable its initial value.
public class Main {
public static void main(String[] args) {
String student = "Mira";
int minutesPerDay = 15;
int days = 4;
int totalMinutes = minutesPerDay * days;
System.out.println("Student: " + student);
System.out.println("Practice minutes: " + totalMinutes);
}
}| Output |
|---|
| Student: Mira |
| Practice minutes: 60 |
Why this works
Execution reaches the declarations in order. Multiplication uses the star symbol, so 15 * 4 produces 60 before that value is assigned to totalMinutes. In the print statements, the plus sign joins a text label with a value. The spaces after the colons are inside the quoted labels.
Use a name without quotes when you want its value. Printing "totalMinutes" would display those letters, while printing totalMinutes displays 60. Meaningful names make it easier to explain the calculation later.
Predict a change before you run it
Keep the same program and change minutesPerDay to 12 and days to 5. Work out the expected total yourself. Then run the program and compare the actual output with your prediction.
Your task
The second line should still display Practice minutes: 60, because 12 multiplied by 5 is 60. Now set days to 0. The total should become 0. You have checked a different combination and a boundary value without changing the calculation itself.
Why this works
If your output stays unchanged after editing, check that you ran the updated program. If you typed 60 directly into the print statement, the output may look correct for one example but will not respond to new values. A useful solution expresses the calculation rather than one remembered answer.
Find common mistakes one at a time
Java is case-sensitive: String and string are different names, as are minutesPerDay and MinutesPerDay. Check spelling and capitals before changing the algorithm. Use double quotes for the text in these examples, and keep each statement’s semicolon.
When a compiler reports an error, start with the first message and inspect that line and the line above it. A missing closing quote, brace or semicolon can make later lines appear incorrect too. Correct one cause, run again and inspect the next result.
After these examples, practise conditions, loops and methods. In InterviewSkills coding practice, choose Java and follow the selected problem’s input and output requirements. The labels used in this tutorial explain the output; a coding problem may require only a number, so match its instructions exactly.
Common questions
Why is the file named Main.java?
The example declares a public class named Main. When compiling this class from a source file, use the matching filename Main.java. An online coding editor may provide the filename and class template for you.
Does declaring a variable print its value?
No. A declaration such as int days = 4 creates a variable with an initial value. A print statement is needed to display that value in this program.
Can I practise Java coding on InterviewSkills?
Yes. The coding workspace supports Java alongside Python and JavaScript. Choose a problem, read its examples and constraints, select Java and test your solution.
Explore Java coding practice
Create an account to continue with guided learning and practice.
Already have an account? Sign in