Core Java: Detailed Explanation of All Topics
INTRODUCTION TO JAVA
Java is a high-level, object-oriented programming language developed by Sun Microsystems in 1995, now
owned by Oracle. Java applications are compiled to bytecode, which can run on any device equipped with a
Java Virtual Machine (JVM), making Java platform-independent.
Key Features of Java (Buzzwords)
• Simple: Easy to understand and learn.
• Object-Oriented: Everything in Java is associated with objects and classes.
• Platform-Independent: Write Once, Run Anywhere (WORA).
• Secure: No explicit pointers, runs inside a virtual machine.
• Robust: Strong memory management and exception handling.
• Multithreaded: Supports concurrent execution of two or more threads.
• Architecture Neutral: Java bytecode is not dependent on processor architecture.
• High Performance: Uses Just-In-Time compilers.
• Distributed: Facilitates distributed computing using RMI and EJB.
• Dynamic: Can adapt to an evolving environment.
PART 1: BASIC JAVA CONCEPTS
Structure of a Java Program
A basic Java program has the following structure:
public class Main {
public static void main(String[] args) {
// code
}
}
• Class Declaration
• Main Method
• Statements inside the method
Members of a Class
• Fields (Variables): Store data.
• Methods: Define behaviors.
• Constructors: Initialize objects.
• Blocks: Static or instance blocks of code.
1
• Nested Classes: Class within a class.
Tokens
The smallest elements in a program:
• Keywords: class , public , static , etc.
• Identifiers: Names given to variables, classes, etc.
• Literals: Constants like numbers, strings.
• Operators: + , - , * , etc.
• Separators: ; , () , {} etc.
Variables and Data Types
• Primitive Types: byte , short , int , long , float , double , char , boolean
• Reference Types: Objects, Arrays, Interfaces
Type Casting
• Widening: Automatically done by Java ( int to double )
• Narrowing: Manual casting required ( double to int )
Operators
• Arithmetic: + , - , * , / , %
• Relational: == , != , < , > , <= , >=
• Logical: && , || , !
• Bitwise: & , | , ^ , ~ , << , >>
• Assignment: = , += , -= , etc.
• Unary: ++ , -- , + , -
Methods
• Block of code that performs a task
• Syntax:
returnType methodName(parameters) {
// body
}
Dynamic Input (Reading Data)
• Use Scanner class:
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
2
Decision Statements
• if, if-else, nested if: Used to perform different actions based on conditions
• switch: Multi-way branch statement
Looping Statements
• for loop: Definite number of iterations
• while loop: Based on condition
• do-while: Runs at least once before checking condition
Static & Static Members
• Belong to the class, not instance
• Accessed using class name
• Shared across all instances
PART 2: OBJECT-ORIENTED CONCEPTS
Object and Class
• Class: Blueprint for objects
• Object: Instance of a class
Non-Static Members
• Belong to instance of a class
• Accessed via object
Constructor
• Special method to initialize objects
• Name same as class, no return type
OOP Principles
1. Encapsulation: Wrapping data and code together (using private fields & public getters/setters)
2. Inheritance: Deriving one class from another
3. Polymorphism: Same function behaves differently:
4. Compile-time (Overloading)
5. Runtime (Overriding)
6. Abstraction: Hiding internal details using abstract classes or interfaces
Interface
• Contract with abstract methods
• A class implements an interface to follow the contract
3
Object Class
• Superclass of all classes
• Common methods: toString() , equals() , hashCode() , getClass()
String
• Immutable class representing character sequences
• Common classes: String , StringBuilder , StringBuffer
Access Modifiers
• private: Accessible within class only
• default: Accessible within package
• protected: Package + subclass
• public: Accessible everywhere
Packages
• Organize related classes/interfaces
• Avoid naming conflicts
Array
• Fixed-size data structure
• Stores multiple values of the same type
PART 3: ADVANCED CORE JAVA
Exception Handling
• Handling runtime errors
• Keywords: try , catch , finally , throw , throws
Wrapper Classes
• Convert primitive types to objects ( int → Integer , char → Character )
• Useful in collections
Collection Framework
• Interfaces: List , Set , Queue , Map
• Implementations: ArrayList , LinkedList , HashSet , HashMap , etc.
• Provides resizable and efficient data structures
File Handling
• Java I/O operations for reading and writing files
4
• Classes: File , FileReader , BufferedReader , FileWriter , PrintWriter , etc.
Multithreading
• Running multiple threads concurrently
• Thread creation:
• Extending Thread class
• Implementing Runnable interface
• Concepts:
• Lifecycle: New, Runnable, Running, Waiting, Terminated
• Synchronization: Avoid thread interference
• Methods: start() , run() , sleep() , join()
Let me know if you want code examples or practice questions for any topic.