Introduction to Object-Oriented Programming and Java
Object-Oriented Programming Concepts
- Definition: OOP is a programming paradigm that organizes code into objects containing data and functions. Programs act as collections of objects communicating to perform tasks.
- Core Features:
- Focus on data rather than functions.
- Bottom-up approach in program design.
- Programs are divided into entities called objects.
- Key Concepts:
- Object: A basic unit representing a real-world entity (e.g., a car with properties like weight and methods like drive).
- Class: A blueprint for creating objects, defining their attributes and behaviors.
- Encapsulation: Combining data and methods into a single unit (class) to hide data from external access (e.g., an ATM machine).
- Abstraction: Hiding complex implementation details and showing only essential features (e.g., an online payment button).
- Inheritance: A mechanism where a child class acquires properties from a parent class (e.g., Employee Management Systems).
- Polymorphism: Allows a method to perform different behaviors based on the calling object ("many forms").
- Pros and Cons:
- Advantages: Modularity, reusability, security, and scalability.
- Disadvantages: Higher complexity, slower execution, and increased memory usage.
Comparison: POP vs. OOP
- Procedure Oriented Programming (POP):
- Follows a top-down approach.
- Focuses on algorithms and control flow.
- Data is shared globally and is less secure.
- Examples: C, Pascal, FORTRAN, COBOL.
- Object-Oriented Programming (OOP):
- Follows a bottom-up approach.
- Focuses on objects and data protection.
- Data is hidden and accessed through methods.
- Examples: C++, Java, Python, C#, JavaScript.
Introduction to Java
- Origin: Developed by James Gosling at Sun Microsystems in 1995.
- Principle: "Write Once, Run Anywhere" (WORA) via the Java Virtual Machine (JVM).
- Key Features:
- Platform Independence: Bytecode runs on any system with a JVM.
- Robust and Secure: Extensive error handling and runtime verification.
- Multithreaded: Supports concurrent task execution.
- Automatic Memory Management: Includes a garbage collector to manage memory automatically.
- Comparison with C:
- Java is object-oriented and platform-independent; C is procedural and platform-dependent.
- Java uses a JVM (compiled and interpreted); C is compiled directly to machine code.
The Java Environment: JDK, JRE, and JVM
- JDK (Java Development Kit): A full software kit used by developers to create programs. Includes JRE and tools like
javac (compiler) and jdb (debugger). - JRE (Java Runtime Environment): Provides the environment to execute Java applications. Includes JVM and class libraries.
- JVM (Java Virtual Machine): An abstract component that converts Java bytecode into machine-specific code. It handles memory through garbage collection and ensures security via bytecode verification.
- Structure relationship: JDK = JRE + Development Tools; JRE = JVM + Library Files.
Java Program Structure and Tokens
- Program Sections: Documentation, Package Statement, Import Statements, Interface Statement, Class Definition, and the Main Method Class.
- Java Tokens (6 types):
- Identifiers: Names for classes and variables. Must start with a letter, underscore
_, or dollar sign \. Case-sensitive. - Keywords: 50 reserved words (e.g.,
abstract, static, void, int). - Literals: Fixed values (e.g.,
int num = 25;). - Operators: Symbols for operations (e.g.,
+, ==, &&). - Separators: Punctuators like
;, ,, and { }. - Comments: Single-line (
//), Multi-line (/* */), and Documentation (/** */).
Data Types and Variables
- Primitive Data Types:
- byte: 1byte, range −128 to 127.
- short: 2bytes, range −32,768 to 32,767.
- int: 4bytes, range −231 to 231−1.
- long: 8bytes, range −263 to 263−1.
- float: 4bytes, range approximately ±3.4×10−38 to ±3.4×10+38.
- double: 8bytes, range approximately ±1.7×10−308 to ±1.7×10+38.
- char: 2bytes, range 0 to 65,535 (Unicode).
- boolean: 1bit, values true or false.
- Variable Types:
- Local: Declared inside methods, no default values.
- Instance: Declared in a class, unique to each object.
- Static (Class Variables): Shared among all instances of a class.
- Final: Constant values that cannot be modified after assignment.
Operators and Control Structures
- Operator Categories: Arithmetic, Relational, Logical, Bitwise, Assignment, Unary, Ternary (
?:), and Shift (<<, >>). - Selection Controls:
if, if-else, if-else-if ladder.switch: Alternative to multiple if-else blocks.
- Iteration Controls (Loops):
for: Used when the number of iterations is known.while: Used when the iteration depends on a condition.do-while: Executes at least once.
- Jump Statements:
break (exits loop) and continue (skips current iteration).
Methods and Constructors
- Methods: Blocks of code for specific tasks. Can be Predefined (e.g.,
Math.sqrt()) or User-defined. - Method Overloading: Multiple methods with the same name but different parameter lists within one class.
- Constructors: Special methods to initialize objects. Must match the class name and have no return type.
- Default: No parameters; provided by Java if none defined.
- Parameterized: Accepts arguments to initialize specific values.
- No-Argument: Manually defined constructor with no parameters.
- Constructor Overloading: Having multiple constructors with different parameter lists.
Built-in Classes and Memory
- Math Class: Provides static methods such as
Math.abs(), Math.sqrt(), Math.pow(), and constants like Math.PI and Math.E. - Character Class: A wrapper for the
char type; includes methods like isLetter(), isDigit(), and toUpperCase(). - Strings:
- String Class: Immutable; values cannot change once created. Java uses a String Pool in heap memory to optimize storage of unique literals.
- StringBuffer Class: Mutable and thread-safe; suitable for frequent text modifications using
append(), insert(), or reverse().
- Wrapper Classes: Convert primitives into objects (e.g.,
int to Integer).- Autoboxing: Automatic primitive-to-object conversion.
- Unboxing: Automatic object-to-primitive conversion.
Advanced Keywords and Utility
- this Keyword: References the current object. Used to resolve naming conflicts between instance and local variables, or to pass/return the current instance.
- Arrays Class: Located in
java.util. Provides static methods for array manipulation:Arrays.toString(): Converts array to a string.Arrays.sort(): Orders elements.Arrays.copyOfRange(): Copies a specific sub-section.Arrays.equals(): Compares two arrays for equality.