Java_Basics_Quick_Notes
Java_Basics_Quick_Notes
Java Basics
Java:
Java is a platform-independent, object-oriented programming language. It runs on the Java Virtual
Machine (JVM).
JVM (Java Virtual Machine):
JVM runs Java bytecode on any device. It handles memory and platform independence.
JRE (Java Runtime Environment):
JRE provides libraries and JVM to run Java programs. It is used for executing Java applications.
JDK (Java Development Kit):
JDK includes JRE plus tools like compiler and debugger. Required to write and run Java code.
Variable:
A variable stores data in memory. Types: local, instance, static.
Data Types:
Java has primitive types (int, float, boolean, etc.) and reference types (String, arrays). They define
the nature of data a variable holds.
Constants:
Constants are declared using final. Their values cannot be changed once assigned.
3. Operators
Arithmetic Operators:
Used for mathematical operations like +, -, *, /. Work with numeric data types.
Relational Operators:
Compare values (==, !=, >, <, etc.). Return boolean results.
Logical Operators:
Perform logical operations (&&, ||, !). Used in decision-making conditions.
If-Else:
Used to execute code based on conditions. if (condition) { } else { }
Switch:
Used for multi-way branching based on a variable's value. Better than multiple if-else statements.
Loops (for, while, do-while):
Used to repeat code blocks. Loop continues until a condition is false.
Break and Continue:
break exits a loop early, continue skips to the next iteration. Used to control loop flow.
5. OOPs Concepts
Class:
A blueprint for creating objects. Contains fields and methods.
Object:
An instance of a class. Has state and behavior.
Constructor:
Special method to initialize objects. Called when an object is created.
Inheritance:
One class inherits features of another using extends. Promotes code reusability.
Polymorphism:
Same method behaves differently (overloading & overriding). Achieved at compile-time or runtime.
Encapsulation:
Hides data using private fields and public methods. Protects object integrity.
Abstraction:
Hides complex implementation and shows only essential features. Done using abstract classes or
interfaces.
Interface:
A contract of abstract methods. Implemented by classes using implements.
String:
A sequence of characters, immutable in Java. Handled using the String class.
StringBuffer / StringBuilder:
Mutable alternatives to String. Used when frequent modifications are needed.
Arrays:
Collection of fixed-size elements of the same type. Indexed starting from 0.
7. Exception Handling
Try-Catch:
Catches and handles runtime errors. Prevents program from crashing.
Finally:
Always executes after try-catch. Used for cleanup code.
Throw/Throws:
Used to manually throw or declare exceptions. Helps manage checked exceptions.
8. Access Modifiers
Public:
Accessible everywhere. Used for shared components.
Private:
Accessible only within the class. Used to protect data.
Protected:
Accessible within package and subclasses. Supports inheritance.
Default (no modifier):
Accessible only within the package. Used for package-level access.
Static:
Belongs to the class, not objects. Shared across all instances.
Final:
Used to declare constants or prevent modification. Final class can't be extended; final method can't
be overridden.
Package:
Groups related classes and interfaces. Helps in organizing code.
Import:
Used to access classes from other packages. Syntax: import package.ClassName;