Java is a powerful, object-oriented programming language used to build applications that run on many devices and operating systems. This blog explains Java from the basics to constructors, OOP, loops, memory, and runtime concepts in a simple way, with code, output, and diagrams.
1. What Is Java?
Java is a high-level, object-oriented, platform-independent programming language. It was created by James Gosling at Sun Microsystems and first released in 1996 as JDK 1.0.
Java is used to build:
Android apps
- Banking and finance systems
- Web backend applications
- Enterprise software
- Cloud applications
- Desktop applications
Java code is written in .java files, compiled into .class bytecode files, and executed by the Java Virtual Machine (JVM).
Human-readable code → Bytecode → JVM → Output
Main.java Main.class Hello World
2. Why Java?
Java is popular because it is:
- Platform-independent: The same Java program can run on Windows, Linux, and macOS.
- Object-oriented: Real-world things can be represented using classes and objects.
- Secure: Java has strong memory management and runtime checks.
- Stable: Large companies use Java for systems that must run for many years.
- Job-friendly: Java is still widely used in backend, Android, banking, insurance, and enterprise development.
- Community support: Java has huge documentation, libraries, frameworks, and learning resources.
3. Java History
Java was originally called Oak. Later, it was renamed Java and released publicly in 1996.
| Year | Java version | Important point |
|---|---|---|
| 1996 | JDK 1.0 | First public release oracle |
| 1997 | JDK 1.1 | Improved event handling |
| 2004 | Java 5 | Generics, enums, for-each loop |
| 2014 | Java 8 | Lambda expressions, Streams |
| 2018 | Java 11 | LTS version ops |
| 2021 | Java 17 | LTS version ops |
| 2023 | Java 21 | LTS version ops |
| 2025 | Java 25 | Latest LTS version ops+1 |
Java 8, 11, 17, 21, and 25 are LTS versions, meaning Long-Term Support versions. Companies prefer LTS versions because they receive support and updates for a longer time.
4. What Is a Fortune 500 Company?
A Fortune 500 company is a company listed in the Fortune magazine’s annual list of the 500 largest United States companies by total revenue.
Examples include companies such as:
- Amazon
- Walmart
- JPMorgan Chase
- Microsoft Many large companies use Java because their systems need stability, security, scalability, and long-term support. Java is especially common in banking, insurance, financial services, and government systems.
5. Do 90% of Companies Use Java?
No. The statement “90% of companies use Java” is not accurate.
Java is very widely used, but not by 90% of all companies. In the Stack Overflow Developer Survey 2025, about 29.4% of all respondents reported using Java. Java adoption is higher in large companies, banking, insurance, and government sectors.
So the correct way to write it in a blog is:
Java is used by millions of developers and many large enterprises, especially in banking, insurance, backend systems, and Android development. However, it is not correct to say that 90% of all companies use Java.
6. Is Java the Fastest Programming Language?
No. Java is not the fastest programming language in every situation.
Languages such as C, C++, Rust, and Zig are generally faster for raw, low-level performance because they compile more directly to machine code. However, Java is still very fast for real-world applications because the JVM uses JIT compilation to convert frequently used bytecode into optimized machine code while the program runs.
In one 2026 benchmark, Java completed the tested workloads close to C#, Go, and C-level performance, but it was not the overall fastest language.
Correct statement:
Java is not always the fastest language, but it is fast enough for most enterprise, backend, and Android applications.
7. Java Job Market
Java remains in demand, especially for:
- Backend developer roles
- Android developer roles
- Full-stack Java roles
- Software engineer roles
- Enterprise application developer roles In 2026, Java is still used heavily in enterprise backend systems and cloud services. Large companies continue to hire Java developers because many existing business systems are built using Java.
Java skills that improve job opportunities:
- Core Java
- OOP
- Collections
- JDBC
- Spring Boot
- REST APIs
- SQL and databases
- Git
- Maven or Gradle
8. Java Plays Well With Others
Java works well with other technologies:
- Databases: MySQL, PostgreSQL, Oracle, MongoDB
- Web: Spring Boot, REST APIs, JSON
- Cloud: AWS, Azure, Google Cloud
- Android: Android apps use Java or Kotlin
- Build tools: Maven, Gradle
- Version control: Git and GitHub Example: A Java backend application can receive a request from a website, read data from a MySQL database, process it, and return JSON data to the frontend.
9. String Types in Java
A String is a non-primitive data type used to store text.
String one = "hi";
System.out.println(one);
Output:
hi
String array
String[] playlists = new String[3];
playlists[0] = "songs1";
playlists[1] = "songs2";
playlists[2] = "songs3";
System.out.println(playlists[0]);
Output:
songs1
Here:
- String[] means an array of String values.
- new String[3] creates space for 3 String values.
- playlists[0] accesses the first value because array indexing starts from 0.
10. Compile Time vs Runtime
This is one of the most important Java concepts.
Compile time
Compile time is when the Java compiler checks and converts your source code into bytecode.
javac Main.java
At compile time, Java checks:
- Syntax errors
- Missing semicolons
- Wrong variable names
- Wrong data types
- Missing methods
- Access modifier mistakes
Example:
int age = "twenty"; // Compile-time error
Error:
incompatible types: String cannot be converted to int
Runtime
Runtime is when the JVM actually executes the compiled bytecode.
java Main
At runtime, Java handles:
- User input
- File reading
- Database connection
- Network calls
- Arithmetic errors
- Null pointer errors
- Garbage collection Example:
public class Main {
public static void main(String[] args) {
int a = 10;
int b = 0;
int result = a / b; // Runtime error
System.out.println(result);
}
}
Output:
Exception in thread "main" java.lang.ArithmeticException: / by zero
The code compiles successfully because the syntax is correct. But when the program runs, dividing by zero causes a runtime exception.
Diagram
Write code
↓
Compile time: javac checks syntax
↓
If no error → Main.class bytecode
↓
Runtime: JVM executes bytecode
↓
Output OR runtime exception
11. Class and Object
A class is a blueprint.
An object is a real instance created from that blueprint.
class Student {
String name;
int age;
void display() {
System.out.println(name + " - " + age);
}
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student();
s1.name = "Rakesh";
s1.age = 20;
s1.display();
}
}
Output:
Rakesh- 20
- Student is the class.
- s1 is the object.
- new Student() creates the object in heap memory.
- s1.name accesses the object’s attribute.
12. Variables in Java
Local variable
A local variable is declared inside a method and can be used only inside that method.
void show() {
int localNumber = 10; // Local variable
System.out.println(localNumber);
}
Global variable
A global variable is declared inside a class but outside methods.
class Student {
String name; // Global / instance variable
}
Static variable
A static variable belongs to the class, not to one object. All objects share the same static variable.
class College {
static String collegeName = "ABC College";
}
Non-static variable
A non-static variable belongs to each object separately.
class Student {
String name; // Each object has its own name
}
Example:
class Student {
static String collegeName = "ABC College";
String name;
Student(String name) {
this.name = name;
}
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student("Arun");
Student s2 = new Student("Priya");
System.out.println(s1.name);
System.out.println(s2.name);
System.out.println(Student.collegeName);
}
}
Output:Arun
Priya
ABC College
13. Methods
A method is a block of code that performs a specific task.
returnType methodName(parameters) {
// method body
}
Example:
public class Main {
static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
int result = add(5, 3);
System.out.println(result);
}
}
Output:
8
Method return type
- int: Returns an integer.
- String: Returns text.
- void: Returns nothing.
static String getName() {
return "Arun";
}
14. Static Method vs Non-Static Method
Static method
A static method belongs to the class. You can call it without creating an object.
class MathUtils {
static int square(int number) {
return number * number;
}
}
public class Main {
public static void main(String[] args) {
System.out.println(MathUtils.square(5));
}
}
Output:
25
Non-static method
A non-static method belongs to an object. You must create an object to call it.
class Student {
void greet() {
System.out.println("Hello Student");
}
}
public class Main {
public static void main(String[] args) {
Student s = new Student();
s.greet();
}
}
Output:
Hello Student
15. Access Modifiers
Access modifiers control who can access a class, method, or variable.
| Modifier | Access level |
|---|---|
| public | Accessible everywhere |
| private | Accessible only inside the same class |
| protected | Accessible in the same package and subclasses |
| default | Accessible only in the same package |
public class Student {
private int marks = 90;
public int getMarks() {
return marks;
}
}
Here, marks is private, so another class cannot directly access it. But it can access it through the public method getMarks().
This is called encapsulation.
16. this Keyword
this refers to the current object.
class Student {
String name;
Student(String name) {
this.name = name;
}
}
Here:
- name inside Student(String name) is the parameter.
- this.name is the object variable.
- this.name = name; assigns the parameter value to the object variable.
17. super Keyword
super refers to the parent class.
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
void sound() {
super.sound(); // Calls parent class method
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.sound();
}
}
Output:
Animal makes sound
Dog barks
18. new Keyword
The new keyword creates an object in memory.
Student s1 = new Student();
- Student is the class.
- s1 is the reference variable.
- new allocates memory.
- Student() calls the constructor.
19. Constructor in Java
A constructor is a special method used to initialize an object. It has the same name as the class and has no return type.
class Student {
String name;
Student(String name) {
this.name = name;
}
}
When is a constructor used?
A constructor is used when an object is created.
Student s1 = new Student("rakesh");
Here, Student("rakesh") calls the constructor and sets the name immediately.
Why use a constructor?
Without a constructor:
Student s1 = new Student();
s1.name = "Arun";
With a constructor:
Student s1 = new Student("Arun");
The constructor makes object creation cleaner and safer.
Default constructor
If you do not write any constructor, Java automatically provides a default constructor.
class Student {
String name;
}
public class Main {
public static void main(String[] args) {
Student s = new Student(); // Default constructor
}
}
Empty constructor
An empty constructor is written by the programmer but has no code inside.
class Student {
Student() {
// Empty constructor
}
}
Difference:
- Default constructor: Automatically provided by Java when no constructor is written.
- Empty constructor: Manually written by the developer with no statements inside.
Constructor overloading
Constructor overloading means having multiple constructors with different parameters.
class Student {
String name;
int age;
Student() {
this.name = "Unknown";
this.age = 0;
}
Student(String name) {
this.name = name;
this.age = 0;
}
Student(String name, int age) {
this.name = name;
this.age = age;
}
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student("Arun");
Student s3 = new Student("Priya", 21);
System.out.println(s1.name + " " + s1.age);
System.out.println(s2.name + " " + s2.age);
System.out.println(s3.name + " " + s3.age);
}
}
Output:
Unknown 0
Arun 0
Priya 21
20. Destructor in Java
Java does not have a destructor like C++.
Instead, Java uses the Garbage Collector to remove unused objects from memory.
Student s1 = new Student();
s1 = null; // Object becomes unused
Later, the Garbage Collector may remove that unused object automatically. You normally do not need to manually destroy objects in Java.
21. Method Overriding
Overriding means a child class provides its own version of a parent class method.
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
class Cat extends Animal {
@Override
void sound() {
System.out.println("Cat meows");
}
}
public class Main {
public static void main(String[] args) {
Animal a = new Cat();
a.sound();
}
}
Output:
Cat meows
We use overriding when a child class needs different behavior from the parent class.
22. Interface in Java
An interface is a contract. It defines what a class should do, but not always how it should do it.
interface Vehicle {
void start();
}
class Car implements Vehicle {
public void start() {
System.out.println("Car starts with key");
}
}
public class Main {
public static void main(String[] args) {
Car car = new Car();
car.start();
}
}
Output:
Car starts with key
Why use an interface?
- To achieve abstraction
- To define common behavior
- To support multiple inheritance of type
- To make code flexible
Types of interface methods
interface Example {
void normalMethod(); // Abstract method
default void defaultMethod() {
System.out.println("Default method");
}
static void staticMethod() {
System.out.println("Static method");
}
}
23. extends vs implements
| Keyword | Used with | Purpose |
|---|---|---|
| extends | Class | Inherit from a parent class |
| implements | Interface | Implement interface methods |
class Animal { }
class Dog extends Animal { }
interface RunnableTask {
void run();
}
class Task implements RunnableTask {
public void run() {
System.out.println("Task running");
}
}
24. Lambda Function
A lambda function is a short way to write a method, especially for functional interfaces.
Normal way:
interface Calculator {
int add(int a, int b);
}
class Main {
public static void main(String[] args) {
Calculator c = new Calculator() {
public int add(int a, int b) {
return a + b;
}
};
System.out.println(c.add(5, 3));
}
}
Lambda way:
interface Calculator {
int add(int a, int b);
}
public class Main {
public static void main(String[] args) {
Calculator add = (a, b) -> a + b;
System.out.println(add.add(5, 3));
}
}
Output:
8
We use lambda functions to write shorter, cleaner code.
25. Memory Management: Primitive vs Non-Primitive
Primitive variables
Primitive variables store the actual value directly.
int a = 10;
Here, a stores 10 directly.
Non-primitive variables
Non-primitive variables store the reference or address of an object.
String name = new String("rakesh");
Here, name stores a reference to the String object in memory.
Primitive:
a → 10
Non-primitive:
name → String object in heap memory
26. Naming Variables
Use clear and meaningful names.
Good:
int studentAge;
String studentName;
double monthlySalary;
Bad:
int a;
String x;
double m;
Rules:
- Class names start with a capital letter: Student
- Variable and method names start with a small letter: studentName
- Constants use capital letters: MAX_MARKS
- Do not use reserved keywords as names.
27. Static Class Information
A static variable stores class-level information.
class School {
static String schoolName = "Green School";
}
All objects share the same schoolName.
A non-static variable stores object-specific information.
class Student {
String name;
}
Each student object can have a different name.
28. Operators and Comparison Operators
Arithmetic operators
int a = 10;
int b = 3;
System.out.println(a + b); // 13
System.out.println(a - b); // 7
System.out.println(a * b); // 30
System.out.println(a / b); // 3
System.out.println(a % b); // 1
Comparison operators
int a = 10;
int b = 20;
System.out.println(a == b); // false
System.out.println(a != b); // true
System.out.println(a > b); // false
System.out.println(a < b); // true
System.out.println(a >= b); // false
System.out.println(a <= b); // true
29. Reserved Keywords
Reserved keywords are special words already defined by Java.
Examples:
public, private, class, static, void, int, if, else, for, while, return, new, this, super, extends, implements
You cannot use them as variable names.
int class = 10; // Error
- Arrays An array stores multiple values of the same type.
int[] numbers = {10, 20, 30, 40};
System.out.println(numbers[0]); // 10
System.out.println(numbers.length); // 4
Enhanced for loop
int[] numbers = {10, 20, 30};
for (int number : numbers) {
System.out.println(number);
}
Output:
10
20
30
31. Loops
while loop
int i = 1;
while (i <= 3) {
System.out.println(i);
i++;
}
Output:
1
2
3
do-while loop
A do-while loop runs at least once.
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 3);
Output:
1
2
3
for loop
for (int i = 1; i <= 3; i++) {
System.out.println(i);
}
32. break and continue
break
break stops the loop.
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break;
}
System.out.println(i);
}
Output:
1
2
continue
continue skips the current iteration.
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.println(i);
}
Output:
1
2
4
5
33. Taking User Input
Use the Scanner class.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.print("Enter your age: ");
int age = scanner.nextInt();
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
Sample output:
Enter your name: Arun
Enter your age: 20
Name: Arun
Age: 20
34. Printing Object Attributes
class Student {
String name;
int age;
Student(String name, int age) {
this.name = name;
this.age = age;
}
void printDetails() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student("rakesh", 20);
s1.printDetails();
}
}
Output:
Name: rakesh
Age: 20
- Random Numbers
import java.util.Random;
public class Main {
public static void main(String[] args) {
Random random = new Random();
int number = random.nextInt(10); // 0 to 9
System.out.println(number);
}
}
For a number from 1 to 10:
int number = random.nextInt(10) + 1;
36. printf
printf is used for formatted output.
public class Main {
public static void main(String[] args) {
String name = "rakesh";
int age = 20;
double marks = 89.567;
System.out.printf("Name: %s, Age: %d, Marks: %.2f%n", name, age, marks);
}
}
Output:
Name: rakesh, Age: 20, Marks: 89.57
- %s: String
- %d: Integer
- %.2f: Decimal number with 2 digits
- %n: New line
37. substring
substring() extracts part of a String.
String name = "JavaScript";
System.out.println(name.substring(0, 4));
Output:
Java
Here, index 0 is included, but index 4 is not included.
38. Enhanced Switch
Modern Java supports enhanced switch syntax.
public class Main {
public static void main(String[] args) {
int day = 2;
String dayName = switch (day) {
case 1 -> "Monday";
case 2 -> "Tuesday";
case 3 -> "Wednesday";
default -> "Invalid day";
};
System.out.println(dayName);
}
}
Output:
Tuesday
39. Types of Arguments
Formal parameters
Parameters written in the method definition.
static void greet(String name) {
System.out.println("Hello " + name);
}
Actual arguments
Values passed while calling the method.
greet("rakesh");
Varargs
Varargs allows a method to receive any number of values.
static int sum(int... numbers) {
int total = 0;
for (int number : numbers) {
total += number;
}
return total;
}
public static void main(String[] args) {
System.out.println(sum(1, 2));
System.out.println(sum(1, 2, 3, 4));
}
Output:
3
10
40. Math Class
The Math class provides built-in mathematical methods.
public class Main {
public static void main(String[] args) {
System.out.println(Math.max(10, 20)); // 20
System.out.println(Math.min(10, 20)); // 10
System.out.println(Math.sqrt(25)); // 5.0
System.out.println(Math.pow(2, 3)); // 8.0
System.out.println(Math.abs(-10)); // 10
}
}
Output:
20
10
5.0
8.0
10
Top comments (0)