Learn Java ProgrammingNew
• Updated 3/12/2026 • javaprogrammingjava tutorialobject oriented programmingcoding fundamentals
Learn Java Programming
Home
Welcome to learning Java.
Java is a programming language. A programming language is a way for people to give instructions to a computer. If you have ever followed a recipe, built something from steps, or played a game using rules, then you already understand the basic idea behind programming. In programming, we write instructions in the correct order so the computer knows exactly what to do.
Java is one of the most popular programming languages in the world. It is used to build many kinds of software, such as:
- Desktop applications
- Android apps
- Web backends
- Business software
- School projects
- Games and tools
Java is a great language for beginners because it teaches strong programming habits. It helps you learn how to write code clearly, organize your ideas, and solve problems step by step.
This tutorial is written for complete beginners. You do not need to know programming before starting. The goal is to explain Java in a simple, friendly way, like a teacher guiding you through your first real coding lessons.
As you go through this tutorial, remember these three ideas:
- Programming is a skill that improves with practice
- Making mistakes is normal
- Every expert programmer started as a beginner
Overview
Java was created to be reliable, secure, and easy to move between different computers. One of Java’s most famous ideas is:
Write Once, Run Anywhere
This means you can write a Java program one time, and it can run on many operating systems, such as Windows, macOS, and Linux.
Why does that work? Because Java programs do not run directly on the computer in the usual way. Instead, Java code is first turned into something called bytecode. Then a special program called the Java Virtual Machine, or JVM, reads that bytecode and runs it.
Here is the simple flow:
- You write Java code
- The code is compiled into bytecode
- The JVM runs the bytecode
This design gives Java some important benefits:
- Platform independent — your program can run on different systems
- Object-oriented — code can be organized using classes and objects
- Strongly typed — Java checks the type of data you use
- Secure — Java was designed with safety in mind
- Large standard library — many useful tools are built in
- Automatic memory management — Java helps clean up unused objects
In simple words, Java is like building with labeled blocks. You write clear instructions, Java checks a lot of things for you, and the JVM helps the program run properly.
Basics
Let us begin with the structure of a Java program.
A Java program is usually made of classes. A class is like a blueprint. A blueprint is a design or plan that explains how something should be built.
Inside classes, we write methods. A method is a block of code that performs a task.
Every Java program starts from a special method called main. This is the entry point of the program. When the program begins, Java looks for the main method first.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Welcome to Java");
}
}
Let us break this apart carefully:
- public class HelloWorld creates a class named HelloWorld
- public static void main(String[] args) is the special method where the program starts
- System.out.println() prints text on the screen
- "Welcome to Java" is a string, which means text
If you run this program, the output will be:
Welcome to Java
At first, Java code may look long. That is okay. As a beginner, focus on understanding what each part does instead of trying to memorize everything at once.
Environment
Before you can write and run Java code, you need a Java development setup. This setup is called your environment.
The main thing you need is the JDK, which stands for Java Development Kit. The JDK contains the tools required to:
- Write Java programs
- Compile Java programs
- Run Java programs
There are two very important commands beginners often hear about:
- javac — the Java compiler
- java — the command that runs the program
A common beginner workflow looks like this:
- Open a code editor
- Create a file named HelloWorld.java
- Write your Java code
- Compile it using javac
- Run it using java
javac HelloWorld.java
java HelloWorld
Important beginner note:
- The file name should match the class name if the class is public
- If your class is named HelloWorld, your file should be HelloWorld.java
Think of compiling like checking and translating your code into a form Java can run. If there is a mistake, Java will usually show an error message. Error messages can feel scary at first, but they are actually useful clues.
Basic Syntax
Syntax means the rules for writing code correctly. Just like human languages have grammar rules, programming languages have syntax rules.
In Java, even small mistakes can stop a program from working. That is why it is important to notice punctuation, spelling, and capitalization.
Main syntax rules:
- Java is case-sensitive
- Statements usually end with a semicolon
- Blocks of code use curly braces
- Text values use double quotes
- Single characters use single quotes
Case-sensitive means this matters:
int age = 13;
int Age = 20;
These are two different variable names because age and Age are not the same in Java.
Semicolons mark the end of a statement:
int number = 10;
System.out.println(number);
Curly braces group code together:
if (true) {
System.out.println("This code is inside a block");
}
Good formatting also matters. Java can run messy code if the syntax is correct, but neat code is easier to read, fix, and learn from.
Data Types
A data type tells Java what kind of value is being stored.
Think of data types like labels on containers. One container holds whole numbers, another holds decimal numbers, another holds text, and another holds true or false.
Java has two main groups of data types:
- Primitive types — simple built-in types
- Reference types — more complex types such as strings, arrays, and objects
Common primitive data types:
- int — whole numbers like 1, 25, 100
- double — decimal numbers like 3.14 or 9.99
- char — one character like 'A'
- boolean — either true or false
int age = 13;
double price = 49.99;
char grade = 'B';
boolean isJavaFun = true;
Reference types include things like:
- String — text
- Arrays — lists of values
- Objects — custom data made from classes
String name = "Mia";
If you choose the wrong data type, Java may not let your code work. For example, this would not make sense:
int score = "high";
That fails because int expects a whole number, but "high" is text.
Variables
A variable stores data that your program can use later.
You can imagine a variable as a named box. The name tells you what is inside, and the value is the actual content of the box.
int score = 90;
String playerName = "Alex";
In the first example:
- int is the data type
- score is the variable name
- 90 is the value
You can also change a variable after creating it:
int coins = 5;
coins = 10;
System.out.println(coins);
That will print:
10
Rules for naming variables:
- The name cannot start with a number
- The name cannot contain spaces
- The name should not be a Java keyword
- The name should describe the value clearly
Good variable names:
- userAge
- studentName
- totalScore
- isLoggedIn
Bad variable names:
- x1x1x1
- thing
- data12345abc
As a beginner, always pick names that make the code easier to understand.
Keywords
Keywords are special words that Java already uses for its own rules and structure.
Because these words already have a special meaning, you cannot use them as variable names, class names in the wrong places, or method names if Java expects them to mean something else.
Some common Java keywords are:
- class
- public
- private
- static
- void
- return
- if
- else
- for
- while
- new
- boolean
- int
- double
For example, this is not allowed:
int class = 5;
That fails because class is a keyword.
Here is how some important keywords are used:
- class creates a class
- public controls access
- static means something belongs to the class itself
- void means a method does not return a value
- return sends a value back from a method
You do not need to memorize every keyword today. Just learn them as you meet them.
Operators
Operators are symbols that perform actions on values.
Think of operators like tools. Different tools do different jobs.
Arithmetic operators are used for math:
- + add
- - subtract
- * multiply
- / divide
- % remainder
int a = 10;
int b = 3;
System.out.println(a + b);
System.out.println(a - b);
System.out.println(a * b);
System.out.println(a / b);
System.out.println(a % b);
Comparison operators compare two values:
- == equal to
- != not equal to
- > greater than
- < less than
- >= greater than or equal to
- <= less than or equal to
int age = 13;
System.out.println(age >= 13);
Logical operators combine conditions:
- && and
- || or
- ! not
int age = 14;
boolean hasTicket = true;
System.out.println(age >= 13 && hasTicket);
Assignment operators store values in variables:
int score = 10;
score = score + 5;
score += 5;
Both ways increase the value, but the second version is shorter.
Decisions
Programs often need to choose between different actions. This is called decision making.
For example:
- If the player has enough coins, buy the item
- If the password is correct, log in
- If the number is bigger than 10, print a message
The most common decision tool in Java is the if statement.
int number = 10;
if (number > 5) {
System.out.println("Number is greater than 5");
} else {
System.out.println("Number is 5 or less");
}
How it works:
- Java checks the condition inside the parentheses
- If the condition is true, the first block runs
- If the condition is false, the else block runs
You can also check more than one condition using else if:
int score = 85;
if (score >= 90) {
System.out.println("Excellent");
} else if (score >= 75) {
System.out.println("Good job");
} else {
System.out.println("Keep practicing");
}
This is useful when there are several possible outcomes.
Loops
A loop repeats code.
Loops are useful when you need to do something again and again, such as:
- Printing numbers
- Checking every item in a list
- Repeating a task until something changes
The for loop is great when you know how many times to repeat.
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
This loop:
- Starts with i = 0
- Runs while i < 5
- Adds 1 to i each time
So it prints:
0
1
2
3
4
The while loop is useful when you want to keep looping while a condition is true.
int count = 0;
while (count < 3) {
System.out.println(count);
count++;
}
The do-while loop is similar, but it runs the code at least once before checking the condition.
int count = 0;
do {
System.out.println(count);
count++;
} while (count < 3);
Important beginner warning: if the loop condition never becomes false, your program can get stuck in an infinite loop.
Numbers
Numbers are very important in programming. Java supports different numeric types because different situations need different sizes and levels of precision.
Common number types:
- byte — very small whole numbers
- short — small whole numbers
- int — regular whole numbers
- long — very large whole numbers
- float — decimal numbers
- double — more precise decimal numbers
Most beginners mainly use:
- int for whole numbers
- double for decimals
int apples = 12;
double price = 19.95;
You can do math with number variables:
int x = 20;
int y = 10;
int sum = x + y;
int difference = x - y;
int product = x * y;
int quotient = x / y;
System.out.println(sum);
System.out.println(difference);
System.out.println(product);
System.out.println(quotient);
Be careful with division. If both values are integers, the result stays an integer.
System.out.println(5 / 2);
This prints:
2
It does not print 2.5 because both values are integers. To get a decimal answer, use a decimal type:
System.out.println(5.0 / 2);
Characters
The char type stores one single character.
A character can be:
- A letter
- A number symbol
- A punctuation mark
- A special symbol
Characters use single quotes:
char letter = 'J';
char digit = '7';
char symbol = '!';
Printing a character:
char firstLetter = 'A';
System.out.println(firstLetter);
A char stores exactly one character. If you want more than one character, you should use a String.
Java uses Unicode for characters, which means it can support characters from many languages and writing systems.
Arrays
An array stores multiple values of the same type in one place.
Imagine you want to store five scores. Instead of writing five separate variables, you can use one array.
int[] scores = {90, 85, 88, 92, 79};
This creates an array of integers.
You can access each item by its index. An index is the position of an item in the array. In Java, array indexes start at 0.
That means:
- scores[0] is 90
- scores[1] is 85
- scores[2] is 88
int[] numbers = {1, 2, 3, 4, 5};
System.out.println(numbers[0]);
System.out.println(numbers[3]);
You can also change a value in an array:
int[] numbers = {1, 2, 3};
numbers[1] = 99;
System.out.println(numbers[1]);
Looping through arrays is very common:
int[] numbers = {1, 2, 3, 4, 5};
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
numbers.length tells you how many items are in the array.
Strings
A String stores text.
If char is one letter, then String is a whole word, sentence, or piece of text.
String name = "Java";
String message = "Hello, world!";
Strings use double quotes.
You can print a string like this:
String message = "Hello Java";
System.out.println(message);
Strings come with many useful methods.
String text = "Java Programming";
System.out.println(text.length());
System.out.println(text.toUpperCase());
System.out.println(text.toLowerCase());
Useful string ideas for beginners:
- length() gives the number of characters
- toUpperCase() changes text to uppercase
- toLowerCase() changes text to lowercase
You can also combine strings. This is called concatenation.
String firstName = "Lia";
String lastName = "Santos";
String fullName = firstName + " " + lastName;
System.out.println(fullName);
Functions
In Java, functions are called methods.
A method is a reusable block of code. Reusable means you can write it once and use it many times. This makes programs cleaner and easier to manage.
Here is a simple method:
public static int add(int a, int b) {
return a + b;
}
Let us understand it:
- public means it can be used from other places
- static means it belongs to the class
- int means it returns a whole number
- add is the method name
- int a, int b are inputs called parameters
- return sends the result back
Calling the method:
int result = add(3, 5);
System.out.println(result);
Methods can also return nothing. In that case, they use void.
public static void sayHello() {
System.out.println("Hello!");
}
Calling it:
sayHello();
Methods help break big problems into smaller pieces.
File I/O
File I/O means working with files. The letters I/O stand for input and output.
Input means reading data into a program. Output means writing data from a program.
In beginner Java programs, you will not always need files, but it is useful to understand the basic idea.
Here is a simple example that creates a file object and prints its name:
import java.io.File;
public class Example {
public static void main(String[] args) {
File file = new File("example.txt");
System.out.println(file.getName());
}
}
This example does not read the file contents. It simply points to a file named example.txt and prints the file name.
Main beginner idea:
- A File object represents a file path
- It does not automatically open or read the file
- It helps your program work with file information
Files are useful when programs need to save or load data, but for beginner learning, it is enough to first understand what a file object is and why programs sometimes use files.
OOP Concepts
Java is an object-oriented programming language. This is often shortened to OOP.
Object-oriented programming is a way to organize code using classes and objects.
Think about a video game. It may have players, enemies, items, and levels. Each of those can be thought of as objects. Each object has data and behavior.
The four main OOP ideas are:
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
Encapsulation means keeping data and the code that works with that data together. It also means protecting data when needed.
Inheritance means one class can build on another. This helps reuse code.
Polymorphism means one action can work in different ways depending on the object.
Abstraction means focusing on the important parts while hiding unnecessary details.
At first, these words can sound difficult. That is normal. For beginners, the main thing to remember is this:
OOP helps programmers organize code in a way that matches real-world things and makes big programs easier to manage.
Class and Object
This is one of the most important beginner topics in Java.
A class is a blueprint. An object is something made from that blueprint.
For example:
- A class is the design for a car
- An object is an actual car built from that design
Here is a simple class:
class Car {
String brand;
int year;
}
This class says that a Car object has:
- A brand
- A year
Now let us create an object:
public class Main {
public static void main(String[] args) {
Car car1 = new Car();
car1.brand = "Toyota";
car1.year = 2020;
System.out.println(car1.brand);
System.out.println(car1.year);
}
}
What is happening here:
- Car car1 creates a variable that can hold a Car object
- new Car() makes a new object from the Car class
- car1.brand and car1.year set values inside the object
You can create more than one object from the same class:
Car car1 = new Car();
car1.brand = "Toyota";
Car car2 = new Car();
car2.brand = "Honda";
Both objects come from the same blueprint, but they can store different data.
Dynamic Memory Management
When a program runs, it uses memory. Memory is where the computer stores the data your program needs while it is working.
In some programming languages, programmers must manually clean up memory. In Java, much of that work is handled automatically.
This is called automatic memory management.
Java uses something called garbage collection. Garbage collection finds objects that are no longer being used and removes them from memory so that space can be used again.
Think of it like this:
- Your program creates objects
- Some objects are still being used
- Some objects are no longer needed
- Java cleans up the unused ones
This helps beginners because you can focus more on logic and less on manual memory cleanup.
Important idea:
Even though Java helps manage memory, it is still smart to write clean code and avoid creating unnecessary objects.
Algorithms and Their Complexity
An algorithm is a step-by-step method for solving a problem.
Examples of simple algorithms:
- Finding the biggest number in a list
- Adding all numbers in an array
- Checking whether a value exists
- Sorting values into order
Here is a basic algorithm that prints numbers from 0 up to one less than n:
for (int i = 0; i < n; i++) {
System.out.println(i);
}
When programmers talk about complexity, they are usually talking about how much time or memory an algorithm uses.
You may hear something called Big O notation. It describes how an algorithm grows as the input grows.
Common complexity examples:
- O(1) — constant time
- O(log n) — logarithmic time
- O(n) — linear time
- O(n²) — quadratic time
Simple beginner meaning:
- O(1) stays fast because it does the same amount of work
- O(n) grows as the number of items grows
- O(n²) can get slow much faster on big data
Example of a linear task:
int[] numbers = {2, 4, 6, 8, 10};
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
This loop checks each item once, so it is a simple example of O(n).
As a beginner, you do not need to master complexity yet. Just understand that some solutions are more efficient than others, and that matters more when programs get bigger.
Summary
You have now gone through the core beginner ideas of Java programming.
Here is what you learned:
- Java is a programming language used to build many kinds of software
- Java programs run through the JVM
- Every program has structure, syntax, and rules
- Variables store data
- Data types describe what kind of data is stored
- Operators perform actions
- Decision statements let programs choose what to do
- Loops repeat code
- Arrays store groups of values
- Strings store text
- Methods help you reuse code
- Classes and objects are the heart of object-oriented programming
- Java helps manage memory automatically
- Algorithms are step-by-step solutions to problems
The most important thing now is practice.
Do not try to remember every word all at once. Instead, read a section, try the example, change the code, and see what happens. Programming becomes easier when you experiment.
A very good beginner practice plan is:
- Write a Hello World program
- Create variables and print them
- Try math with operators
- Use if statements to make decisions
- Use loops to repeat tasks
- Create arrays and print their values
- Write simple methods
- Create your own class and object
That is how real learning happens: small steps, repeated often.
You do not need to be perfect. You only need to keep going.

