Java Tutorial for Beginners (2026): Complete Guide with Practical Examples
Java Tutorial
Java is one of the most popular and powerful programming languages in the world. It is widely used for developing enterprise applications, Android apps, web applications, desktop software, cloud services, and backend systems.
Java follows the principle of “Write Once, Run Anywhere (WORA)”, meaning Java programs can run on any platform that has the Java Virtual Machine (JVM).
If you’re starting your programming journey, Java is an excellent language because of its simplicity, security, portability, and huge community support.
What is Java?
Java is an object-oriented, class-based, high-level programming language developed by Sun Microsystems (now owned by Oracle).
Java source code is compiled into bytecode, which runs on the Java Virtual Machine (JVM).
Flow:
Java Source Code (.java)
↓
Java Compiler (javac)
↓
Bytecode (.class)
↓
Java Virtual Machine (JVM)
↓
Program Output
Why Learn Java?
Java is an excellent choice because it offers:
- Easy to learn
- Platform Independent
- Object-Oriented
- Highly Secure
- Fast Performance
- Huge Community Support
- Cross-Platform Compatibility
- Excellent Career Opportunities
- Used by Large Companies
- Rich API Library
Features of Java
- Object-Oriented Programming
- Platform Independent
- Strongly Typed
- Automatic Memory Management
- Multithreading
- Exception Handling
- High Security
- Rich Standard Library
- Distributed Computing Support
- Portable Code
Install Java
Step 1
Download and install JDK (Java Development Kit).
Step 2
Verify installation
java -version
Example Output
Java 24.x.x
Check compiler
javac -version
Your First Java Program
Create
HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}
Compile
javac HelloWorld.java
Run
java HelloWorld
Output
Hello, Java!
Variables
public class Main {
public static void main(String[] args){
String name="John";
int age=25;
double salary=45000.50;
boolean student=true;
System.out.println(name);
}
}
Data Types
Primitive Types
- int
- long
- short
- byte
- float
- double
- char
- boolean
Example
int number=100;
double price=999.99;
char grade='A';
boolean active=true;
Operators
int a=20;
int b=10;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/b);
If Else
int age=20;
if(age>=18){
System.out.println("Eligible");
}else{
System.out.println("Not Eligible");
}
Switch Statement
String day="Monday";
switch(day){
case "Monday":
System.out.println("Start Work");
break;
case "Sunday":
System.out.println("Holiday");
break;
default:
System.out.println("Normal Day");
}
Loops
For Loop
for(int i=1;i<=5;i++){
System.out.println(i);
}
While Loop
int i=1;
while(i<=5){
System.out.println(i);
i++;
}
Do While
int i=1;
do{
System.out.println(i);
i++;
}while(i<=5);
Arrays
int[] numbers={10,20,30,40};
System.out.println(numbers[0]);
Methods
public class Main{
static int add(int a,int b){
return a+b;
}
public static void main(String[] args){
System.out.println(add(10,20));
}
}
Output
30
Classes and Objects
class Student{
String name;
void display(){
System.out.println(name);
}
}
public class Main{
public static void main(String[] args){
Student s=new Student();
s.name="Rahul";
s.display();
}
}
Constructor
class Employee{
Employee(){
System.out.println("Constructor Called");
}
}
Inheritance
class Animal{
void sound(){
System.out.println("Animal Sound");
}
}
class Dog extends Animal{
void bark(){
System.out.println("Woof");
}
}
public class Main{
public static void main(String[] args){
Dog d=new Dog();
d.sound();
d.bark();
}
}
Polymorphism
class Animal{
void sound(){
System.out.println("Animal");
}
}
class Dog extends Animal{
@Override
void sound(){
System.out.println("Dog Bark");
}
}
Encapsulation
class Student{
private String name;
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
}
Abstraction
abstract class Vehicle{
abstract void start();
}
class Car extends Vehicle{
void start(){
System.out.println("Car Started");
}
}
Interface
interface Animal{
void sound();
}
class Dog implements Animal{
public void sound(){
System.out.println("Woof");
}
}
Exception Handling
try{
int result=10/0;
}catch(Exception e){
System.out.println(e.getMessage());
}
File Handling
import java.io.File;
File file=new File("demo.txt");
System.out.println(file.exists());
Collections
import java.util.ArrayList;
ArrayList<String> names=new ArrayList<>();
names.add("Rahul");
names.add("Amit");
System.out.println(names);
HashMap
import java.util.HashMap;
HashMap<Integer,String> map=new HashMap<>();
map.put(1,"John");
map.put(2,"David");
System.out.println(map);
String Methods
String text="Java";
System.out.println(text.length());
System.out.println(text.toUpperCase());
System.out.println(text.toLowerCase());
System.out.println(text.contains("Ja"));
Java Packages
import java.util.Scanner;
User Input
Scanner sc=new Scanner(System.in);
System.out.print("Enter Name: ");
String name=sc.nextLine();
Multithreading
class MyThread extends Thread{
public void run(){
System.out.println("Running Thread");
}
}
MyThread t=new MyThread();
t.start();
Java Collections Framework
- ArrayList
- LinkedList
- Vector
- Stack
- Queue
- HashMap
- HashSet
- TreeMap
- TreeSet
- PriorityQueue
Java vs Python
| Feature | Java | Python |
|---|---|---|
| Typing | Static | Dynamic |
| Speed | Faster | Slower |
| Syntax | More Verbose | Simpler |
| Performance | High | Moderate |
| Enterprise Usage | Excellent | Good |
Java Best Practices
- Use meaningful variable names.
- Follow Java naming conventions.
- Handle exceptions properly.
- Avoid duplicate code.
- Write reusable methods.
- Use interfaces where appropriate.
- Keep classes focused on a single responsibility.
- Use the latest LTS version of Java.
- Write unit tests for your code.
- Keep dependencies updated.
Popular Java Frameworks
- Spring Boot
- Spring MVC
- Hibernate
- Jakarta EE
- Apache Struts
- Micronaut
- Quarkus
Real-World Applications
Java is widely used for:
- Android Applications
- Banking Software
- Enterprise Applications
- E-commerce Platforms
- Cloud Services
- REST APIs
- Desktop Applications
- Big Data Systems
- Financial Software
- Government Applications
Common Interview Questions
What is JVM?
The Java Virtual Machine (JVM) executes Java bytecode and enables platform independence.
What is JDK?
The Java Development Kit (JDK) includes the compiler, JVM, and development tools required to build Java applications.
What is JRE?
The Java Runtime Environment (JRE) provides the libraries and JVM needed to run Java programs.
Is Java object-oriented?
Yes. Java is primarily an object-oriented programming language.
Can Java be used for web development?
Yes. Frameworks like Spring Boot make Java a popular choice for building web applications and REST APIs.
Conclusion
Java remains one of the most reliable and widely adopted programming languages. From Android apps and enterprise software to cloud-native services and financial systems, Java is used across industries. Learning Java gives you a strong foundation in object-oriented programming and opens doors to careers in backend development, Android development, and enterprise application engineering.
FAQ
Is Java easy to learn?
Yes. Java is beginner-friendly and has extensive documentation and community support.
Is Java free?
Yes. OpenJDK is free and open source.
Which Java version should I learn?
Start with the latest Long-Term Support (LTS) version, such as Java 21, and become familiar with newer features in current releases.
Can I build Android apps with Java?
Yes. Although Kotlin is now Google’s preferred language for Android, Java is still fully supported and widely used in existing Android applications.