Java Programming - Detailed Notes
Unit I: Introduction and Fundamentals of Java
Features of Java:
- Platform-independent, Object-oriented, Secure, Robust, Multithreaded, Distributed.
JVM, JDK, and JRE:
- JVM: Converts bytecode to machine code; ensures platform independence.
- JDK: Includes development tools and JRE.
- JRE: Contains JVM and standard libraries.
Java Program Structure:
- A Java program contains a `class`, the `main()` method, and statements.
Tokens in Java:
- Keywords, Identifiers, Literals, Operators, Separators.
Data Types:
- Primitive (e.g., int, float, char, boolean).
- Reference (e.g., objects, arrays, strings).
Variables:
- Local: Declared within methods.
- Instance: Non-static, tied to objects.
- Static: Shared by all instances of a class.
Operators and Expressions:
- Arithmetic, Logical, Relational, Bitwise, Assignment.
- Operator Precedence: Determines order of operations.
Control Structures:
- Decision-making: `if`, `switch` statements.
- Looping: `for`, `while`, `do-while` loops.
Wrapper Classes:
- Convert primitives to objects (e.g., Integer, Double).
Mathematical Functions:
- Examples: `Math.sqrt()`, `Math.pow()`, `Math.abs()`.
Unit II: Arrays and Strings
Arrays:
- One-dimensional: `int[] arr = new int[5];`.
- Two-dimensional: `int[][] arr = new int[2][3];`.
String Class:
- Immutable.
- Common methods: `length()`, `substring()`, `charAt()`, `equals()`.
String Operations:
- Concatenation: `str1 + str2`.
- Comparison: `str1.equals(str2)`, `compareTo()`.
Unit III: Class, Objects, and Methods
Classes and Objects:
- Classes: Templates for creating objects, containing fields and methods.
- Objects: Instances of classes, created using the `new` keyword.
Access Modifiers:
- Public, Private, Protected, Default.
Special Keywords:
- `this`: Refers to the current object.
- `super`: Access superclass members.
Method Overloading and Constructors:
- Overloading: Same method name, different parameters.
- Constructors: Initialize objects (default and parameterized).
Garbage Collection:
- JVM clears unreferenced objects automatically.
Inheritance and Polymorphism:
- Types of Inheritance: Single, Multilevel, Hierarchical.
- Polymorphism:
- Compile-time: Method overloading.
- Runtime: Method overriding.
Interfaces:
- Define methods; implemented by classes for multiple inheritance.
Unit IV: Packages
Java API Packages:
- Common packages: `java.util`, `java.io`, `java.lang`.
Creating and Using Packages:
- Define a package: `package mypackage;`.
- Access a package: Use `import` statement.
Hiding Classes:
- Protect specific classes from external access.