Java Unit 1 Notes
Salient Features of Java
- Simple: Easy to learn, similar to C++, but without complex features like pointers.
- Object-Oriented: Everything in Java is treated as an object.
- Platform Independent: Compiled into bytecode which runs on JVM.
- Secure: Provides runtime security through bytecode verification.
- Robust: Strong memory management and exception handling.
- Multithreaded: Supports multithreaded programming.
- Portable: Write once, run anywhere (with JVM).
- Distributed: Supports distributed computing.
- High Performance: JIT compiler improves runtime performance.
Object-Oriented Programming Approach
- Class: Blueprint for creating objects.
- Object: Instance of a class.
- Encapsulation: Wrapping data and code together.
- Inheritance: One class can inherit from another.
- Polymorphism: Same interface, different implementations.
- Abstraction: Hiding internal details.
Byte Code and JVM
- Bytecode: Intermediate code compiled by Java compiler.
- JVM: Converts bytecode to machine code and enables platform independence.
Basic Constructs
- Variables: Store data. e.g., int x = 10;
- Data Types: Primitive (int, char, boolean), Reference (arrays, objects).
- Operators: Arithmetic, Logical, Relational, etc.
- Control Statements: if, switch, loops (for, while), break, continue.
- Input: Scanner sc = new Scanner([Link]);
Typecasting
- Widening (Implicit): int to double.
- Narrowing (Explicit): double to int, with data loss possible.
Arrays
- One-Dimensional: int[] arr = new int[5];
- Multi-Dimensional: int[][] matrix = new int[3][3];
String and StringBuffer
- String: Immutable. e.g., String s = "Hello";
- StringBuffer: Mutable. e.g., [Link](" World");
Classes
- Class Fundamentals: class Car { int speed; void drive() {} }
- Declaring Objects: Car c = new Car();
- Object Reference Variables: Hold address of the object.
- Constructors: Initialize object. Can be default or parameterized.
- this Keyword: Refers to current object instance.
- Garbage Collection: Automatic memory management.
- finalize(): Called before object is destroyed by GC.