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 19951995.
  • 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):
    1. Identifiers: Names for classes and variables. Must start with a letter, underscore _, or dollar sign \. Case-sensitive.
    2. Keywords: 5050 reserved words (e.g., abstract, static, void, int).
    3. Literals: Fixed values (e.g., int num = 25;).
    4. Operators: Symbols for operations (e.g., +, ==, &&).
    5. Separators: Punctuators like ;, ,, and { }.
    6. Comments: Single-line (//), Multi-line (/* */), and Documentation (/** */).

Data Types and Variables

  • Primitive Data Types:
    • byte: 1 byte1\,byte, range −128-128 to 127127.
    • short: 2 bytes2\,bytes, range −32,768-32,768 to 32,76732,767.
    • int: 4 bytes4\,bytes, range −231-2^{31} to 231−12^{31}-1.
    • long: 8 bytes8\,bytes, range −263-2^{63} to 263−12^{63}-1.
    • float: 4 bytes4\,bytes, range approximately ±3.4×10−38\pm 3.4 \times 10^{-38} to ±3.4×10+38\pm 3.4 \times 10^{+38}.
    • double: 8 bytes8\,bytes, range approximately ±1.7×10−308\pm 1.7 \times 10^{-308} to ±1.7×10+38\pm 1.7 \times 10^{+38}.
    • char: 2 bytes2\,bytes, range 00 to 65,53565,535 (Unicode).
    • boolean: 1 bit1\,bit, values truetrue or falsefalse.
  • 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.PIMath.PI and Math.EMath.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.