✅ Java Programming – Notes
📌 What is Java?
Java is a high-level, object-oriented, class-based programming language.
Developed by James Gosling at Sun Microsystems in 1995.
Java is known for its WORA (Write Once, Run Anywhere) capability – due to the
Java Virtual Machine (JVM).
Widely used for web applications, desktop apps, Android apps, games, and
enterprise systems.
🧱 Features of Java
Feature Description
Simple Easy to learn, especially for programmers with C/C++ background.
Object-Oriented Everything in Java is part of a class or object.
Platform Independent Compiled Java code runs on any platform with a JVM.
Secure Java provides built-in security features.
Robust Strong memory management and exception handling.
Multithreaded Supports concurrent execution of two or more threads.
High Performance With the help of JIT (Just-In-Time) compiler.
Distributed Supports distributed computing (e.g., RMI, EJB).
📚 Basic Structure of a Java Program
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
Explanation:
public class HelloWorld → Class definition.
main(String[] args) → Entry point of the program.
System.out.println() → Used to print output.
🧩 Java Data Types
1. Primitive Data Types:
Type Size Example
int 4 bytes 10, -20
float 4 bytes 3.14f
double 8 bytes 3.14159
char 2 bytes 'A'
boolean 1 bit true, false
byte 1 byte -128 to 127
short 2 bytes -32,768 to 32,767
long 8 bytes 100000L
2. Non-Primitive (Reference) Types:
String, Arrays, Classes, Interfaces, etc.
🔁 Control Statements
1. Conditional Statements:
if (condition) { ... }
else if (condition) { ... }
else { ... }
2. Switch Statement:
switch (variable) {
case value1: ...
break;
case value2: ...
break;
default: ...
}
3. Loops:
// for loop
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
// while loop
while (condition) {
...
}
// do-while loop
do {
...
} while (condition);
🧰 Object-Oriented Programming (OOP) in Java
4 Main OOP Principles:
Principle Description
Encapsulation Wrapping data and methods in a class (use of private, public etc.)
Inheritance One class inherits properties from another using extends keyword.
One method behaves differently based on object (overloading,
Polymorphism
overriding)
Hiding internal details and showing only functionality (abstract classes,
Abstraction
interfaces)
📦 Java Class and Object
public class Car {
String color = "Red";
void drive() {
System.out.println("Car is driving");
}
}
// Main method
public class Main {
public static void main(String[] args) {
Car myCar = new Car(); // Object creation
myCar.drive();
System.out.println(myCar.color);
}
}
🔐 Access Modifiers
Modifier Accessible Within Same Package Subclass Everywhere
private ✅ ❌ ❌ ❌
default ✅ ✅ ❌ ❌
protected ✅ ✅ ✅ ❌
public ✅ ✅ ✅ ✅
🧪 Exception Handling
try {
// risky code
} catch (Exception e) {
// handle error
} finally {
// runs regardless of exception
}
🗃️Packages in Java
A package is a group of related classes and interfaces.
Used to avoid name conflicts and for easier maintenance.
package mypackage;
Importing a package:
import java.util.Scanner;
📦 Common Java Libraries
Library/Package Purpose
java.util Data structures (ArrayList, HashMap)
java.io Input/output (files, streams)
java.net Networking (sockets, URLs)
java.sql Database connectivity
🛠️Basic Java Tools
Tool Use
javacJava compiler (compiles .java to .class)
java Runs Java bytecode (.class files)
jar Creates Java Archive files
📱 Applications of Java
Android App Development
Web Applications (using JSP/Servlets)
Enterprise Applications (Spring, Hibernate)
Desktop GUI Applications (JavaFX, Swing)
Game Development