Java Tutorial
Java Tutorial for beginners to learn Java online for free.
Java is one of the most popular and secure programming language first released by Sun Microsystems in 1995. From laptops to datacenters, game consoles to scientific supercomputers, cell phones to the Internet, Java is everywhere!
In this online Java training course, we cover basics, functions, loops, array, objects etc with examples. You will also find useful exercise, codes, Interview Questions to enhance your knowledge at the end of the most chapters.
Java Introduction
Java is a free, general purpose programming language. It was created by James Gosling at Sun Microsystems and was first released in 1995. Today Oracle looks after it, and a big community…
What is Java? JDK, JRE and JVM
Java is a programming language and also a platform. When people say “Java” they can mean the language (the code you write), or the whole platform (the tools and the runtime which…
Java Install and Setup
To write Java programs you must install a JDK (Java Development Kit). There are many free builds of the same OpenJDK, from Eclipse Temurin, Oracle, Amazon Corretto, Microsoft and others. They all…
Your First Java Program: Hello World
Let us write the Hello World program, which prints a message on the screen. We will do it two times: first the classic way, which every Java book and every older project…
Java Syntax
Syntax means the rules of writing code. Java has strict rules, and the compiler tells you when you break one. Here are the most important ones. A Java program The rules All…
Java Comments
Comments are notes inside the code. The compiler ignores them. They help other people (and you after six months) to understand the code. Single line comment Starts with // and goes to…
Java Variables
A variable is a named box in the memory where you keep a value. In Java every variable has a type, which says what kind of value it can keep. Declare and…
Java Data Types
Java has two groups of data types: primitive types (simple values) and reference types (objects like String, arrays and classes). The 8 primitive types Type Size What it keeps Example byte 8…
Java Type Casting
Type casting means changing a value from one data type to another. Java has two kinds: widening (automatic) and narrowing (manual). Widening casting (automatic) A smaller type goes into a bigger type…
Java Operators
Operators do work on values and variables. Java has these groups. Arithmetic operators Operator Name Example Result + Addition 10 + 3 13 – Subtraction 10 – 3 7 * Multiplication 10…
Java Strings
A String is a sequence of characters, like a name or a sentence. It is an object (not a primitive) and it is used in almost every program. Create a string Join…
Java String Methods
The String class has many methods. These are the ones you will use most. All the examples use this text. Basic methods Find and cut The positions start at 0. For substring(start,…
Java StringBuilder and Text Blocks
A normal String can not be changed, so every join creates a new object. When you build a long text in a loop, this is slow. StringBuilder is a changeable text, made…
Java User Input with Scanner
The Scanner class reads input from the keyboard (or from a file or a text). You import it from java.util and give it System.in. Read text and numbers In the examples below…
Java Math Class
The Math class (in java.lang, no import needed) has static methods for common maths. You call them like Math.sqrt(16). Common methods Rounding Method What it does round(x) Nearest whole number (as long…
Java if, else and else if
Programs must take decisions. The if statement runs a block of code only when a condition is true. if if … else else if Use else if to check many conditions. Java…
Java switch Statement and Expression
switch chooses one path from many, based on the value of a variable. It is cleaner than many else if. Classic switch with break Without break Java goes on and runs the…
Java for Loop
A loop repeats a block of code. Use the for loop when you know how many times to repeat. Syntax Initialization runs once at the start. The condition is checked before every…
Java while and do while Loop
Use a while loop when you do not know how many times you need to repeat, and you only know the condition. while loop The condition is checked before each round. If…
Java break and continue
break and continue change the normal flow of a loop. break break stops the loop at once. continue continue skips the rest of the current round and goes to the next round….
Java Arrays
An array keeps many values of the same type in one variable. The size is fixed when you create it, and the items are numbered from 0. Create an array Default values…
Java Multidimensional Arrays
A multidimensional array is an array of arrays. The most common is the two dimensional (2D) array, like a table with rows and columns. Create a 2D array Loop with nested loops…
Java Methods
A method is a named block of code which you can run many times. Methods make programs short, clear and easy to test. In other languages they are called functions. Declare and…
Java Method Overloading and Varargs
Overloading means many methods with the same name but different parameters (different number or different types). Java chooses the right one from the arguments you give. Example Rules The parameter list must…
Java Recursion
Recursion means a method calls itself. It is a good way to solve a big problem by solving smaller copies of the same problem. Two parts of every recursion Base case: the…
Java Classes and Objects
Java is an object oriented language. A class is a blueprint, and an object is a real thing made from that blueprint. A class Car is the idea of a car, and…
Java Constructors
A constructor is a special method which runs when you create an object with new. It sets the starting values of the object. It has the same name as the class and…
Java static and this Keywords
static and this are two keywords which every Java developer must understand. static fields: one copy for the class A normal (instance) field has a copy in every object. A static field…
Java Encapsulation and Access Modifiers
Encapsulation means keeping the data of a class private and giving controlled access through methods. So the outside code can not put the object in a wrong state. The problem without it…
Java Inheritance
Inheritance lets a class (the subclass or child) take the fields and methods of another class (the superclass or parent). It gives you code reuse and an “is a” relation: a Dog…
Java Polymorphism
Polymorphism means “many forms”. One method call can behave in different ways depending on the real object. It is the most powerful idea of object oriented programming. Parent reference, child object The…
Java Abstract Classes
An abstract class is a class which you can not create objects of. It is made to be a base for other classes. It can have abstract methods (no body, only the…
Java Interfaces
An interface is a contract: it lists the methods that a class promises to have. Classes implement the interface. A class can implement many interfaces, so it solves the “only one parent”…
Java Enums
An enum is a special class for a fixed set of constants, like days of the week, order status or sizes. It is safer than plain numbers or strings, because the compiler…
Java Records
A record (Java 16+) is a short way to write a class which only carries data. With one line you get the fields, the constructor, the getters, equals(), hashCode() and toString(). A…
Java Packages and Imports
A package is a folder of related classes. Packages keep big projects organized and avoid name clashes: two different classes can both be named Date if they are in different packages. Built…
Java Exceptions: try, catch, finally and throw
An exception is an error which happens while the program is running, like dividing by zero or opening a file which does not exist. If you do not handle it, the program…
Java ArrayList
An ArrayList is a list which can grow and shrink, unlike an array which has a fixed size. It is part of the Collections framework in java.util. Create and use Loop through…
Java HashMap
A HashMap keeps data as key and value pairs, like a dictionary. You find a value very fast by its key. Keys are unique. Basic use Loop over a map Count the…
Java HashSet and LinkedList
The Collections framework has many types. We already saw ArrayList and HashMap. Here are three more which you will use often. HashSet: unique values A Set does not allow duplicates. HashSet is…
Java Generics
Generics let you write classes and methods which work with many types, and still keep the type safe. You already use them in List<String> and Map<String, Integer>. The type in the angle…
Java Lambda Expressions
A lambda expression (Java 8) is a short way to write an anonymous function. It is mostly used to pass a small piece of behavior to a method, for example how to…
Java Streams API
A stream (Java 8) is a pipeline to process a collection of data in a clear way: take data, filter it, change it and collect the result. It does not change the…
Java Optional
Optional (Java 8) is a box which may or may not have a value. A method returns an Optional to say clearly: “the result can be missing”. It helps you to avoid…
Java Date and Time (java.time)
The modern date and time API is in the package java.time (Java 8+). Its objects are immutable and easy to use. Do not use the old java.util.Date and Calendar in new code….
Java File Handling
Java can create, read, write and delete files. The modern way is the java.nio.file package (Files and Path), which is simple and short. Write and read a text file Append and write…
Java Threads and Concurrency
A thread is a path of execution. A Java program starts with one thread (main). With more threads, a program can do many jobs at the same time, like downloading a file…
Java Pattern Matching, Sealed Classes and Records
Modern Java (17, 21 and 25) has features which make code shorter and safer. The three most important work together: sealed types, records and pattern matching. Sealed classes and interfaces A sealed…
Java Cheat Sheet
A quick reference of the most used Java syntax. Use it to revise before an exam or an interview. Every example is tested on Java 25. Program Variables and types Type Example…