Java Important Notes (Detailed)
Features of Java (Buzz Words)
Java is a popular programming language due to its rich features:
1. Simple: Java has a clean and easy-to-understand syntax similar to C++, without complex
features like pointers.
2. Object-Oriented: Everything in Java is treated as an object, allowing modular
programming and code reuse.
3. Platform Independent: Java code is compiled into bytecode which runs on any system
having JVM (Java Virtual Machine).
4. Secure: Java provides a secure environment through features like bytecode verification,
sandboxing, and lack of pointer access.
5. Robust: Java handles errors through exception handling and has features like garbage
collection to manage memory.
6. Multithreaded: Java supports multithreading, allowing programs to do multiple tasks at
the same time.
7. Architecture Neutral: Java code behaves the same on any architecture due to the JVM.
8. High Performance: Java's Just-In-Time (JIT) compiler improves the performance of
applications.
9. Dynamic: Java supports dynamic loading of classes at runtime and interfaces with native
code libraries.
Data Types, Sizes, and Ranges
Java has 8 primitive data types, each with fixed memory size:
- byte: 8-bit, range -128 to 127
- short: 16-bit, range -32,768 to 32,767
- int: 32-bit, range -2^31 to 2^31-1
- long: 64-bit, range -2^63 to 2^63-1
- float: 32-bit, decimal numbers with ~6-7 digits precision
- double: 64-bit, decimal numbers with ~15 digits precision
- char: 16-bit Unicode character
- boolean: 1-bit, values true or false
Class vs Object
Class: A class is a blueprint or template used to create objects. It defines properties (fields)
and behaviors (methods).
Object: An object is an instance of a class. It occupies memory and represents a real-world
entity.
Example:
Class: Car (fields: color, speed; methods: accelerate())
Object: Car c1 = new Car();
Features of OOP
1. Encapsulation: Wrapping data and code into a single unit (class).
2. Inheritance: Mechanism by which one class inherits the properties of another. Promotes
code reusability.
3. Polymorphism: Ability of a method to behave differently based on the object calling it
(compile-time and runtime).
4. Abstraction: Hiding internal details and showing only the necessary features of an object.
String Handling Methods
Strings in Java are immutable objects. Some useful methods are:
- length(): Returns the number of characters in the string.
- charAt(index): Returns the character at the given index.
- substring(start, end): Returns a substring between given indices.
- toLowerCase()/toUpperCase(): Converts the string case.
- equals(): Compares two strings for value equality.
- compareTo(): Lexicographically compares two strings.
- concat(): Combines two strings.
- replace(old, new): Replaces character/substring.
Matrix Application Program
Java supports multi-dimensional arrays. Example: matrix addition:
int[][] a = {{1,2}, {3,4}}; int[][] b = {{5,6}, {7,8}};
Use nested loops to iterate through rows and columns and add corresponding elements.
Packages
A package is a namespace for organizing classes and interfaces.
- Built-in packages: java.util, java.io, java.lang.
- User-defined package:
package mypack; public class MyClass { ... }
- Use 'import' to access classes from a package.
Interfaces vs Abstract Class
Interface:
- Can contain only abstract methods (default and static from Java 8).
- Supports multiple inheritance.
- All variables are public static final by default.
Abstract Class:
- Can contain abstract and concrete methods.
- Supports single inheritance only.
- Can have variables of any type.
Hierarchy of Exception Classes
All exceptions in Java are subclasses of the Throwable class:
- Throwable
|- Error: Serious problems, not handled by programmers (e.g., OutOfMemoryError).
|- Exception
|- Checked Exceptions: Must be handled (e.g., IOException).
|- Unchecked Exceptions: Runtime errors (e.g., ArithmeticException).
Multi-Catch Example
Allows catching multiple exceptions in one block:
try { int a = 5/0; int[] b = new int[2]; b[5] = 10; }
catch (ArithmeticException | ArrayIndexOutOfBoundsException e) { System.out.println(e); }
Thread Life Cycle
Java thread states:
- New: Thread created.
- Runnable: Ready to run.
- Running: Currently executing.
- Blocked/Waiting: Waiting for resource.
- Terminated: Execution finished.
Thread Priorities
Each thread has a priority:
- MIN_PRIORITY = 1
- NORM_PRIORITY = 5 (default)
- MAX_PRIORITY = 10
Higher priority threads get more CPU time.
Thread Synchronization
Used to prevent conflicts when multiple threads access shared resources.
Use 'synchronized' keyword to allow only one thread to execute a block of code at a time.
Inter-Thread Communication
Helps threads coordinate using:
- wait(): Makes the thread wait.
- notify(): Wakes up one waiting thread.
- notifyAll(): Wakes up all waiting threads.
Must be called from a synchronized block.
What is an Applet? What is the Life Cycle?
Applet: A small Java program that runs inside a web browser.
It extends java.applet.Applet or javax.swing.JApplet.
Life Cycle Methods:
- init(): Initializes the applet. Called once.
- start(): Called every time the applet is started.
- paint(): Handles GUI drawing using Graphics class.
- stop(): Called when applet is stopped.
- destroy(): Final cleanup before applet is removed.