Advanced Java Programming Guide
A 10-page intermediate to advanced guide on Java.
Java Packages & Access Modifiers
Packages organize classes into namespaces.
Access Modifiers:
- public: accessible everywhere
- protected: accessible in same package + subclasses
- default: accessible only in same package
- private: accessible only within the class
Interfaces vs Abstract Classes
- Interface: Defines a contract, only method signatures (Java 8+ allows default and static
methods).
- Abstract Class: Can have both abstract and concrete methods.
Use interfaces for multiple inheritance, abstract classes for shared base functionality.
Generics in Java
Generics allow type-safe code.
Example:
List<Integer> list = new ArrayList<>();
[Link](10);
// [Link]("String"); // Compile-time error
Java I/O (File Handling)
Java provides [Link] package.
Example:
import [Link].*;
public class FileExample {
public static void main(String[] args) throws Exception {
FileWriter fw = new FileWriter("[Link]");
[Link]("Hello Java I/O");
[Link]();
}
Serialization & Deserialization
Serialization: converting object into byte stream.
Deserialization: restoring object from byte stream.
Example:
class Student implements Serializable {
int id; String name;
}
JDBC (Java Database Connectivity)
JDBC allows Java programs to interact with databases.
Steps:
1. Load driver
2. Establish connection
3. Create statement
4. Execute query
5. Close connection
Example:
Connection con = [Link](url,user,pass);
Lambda Expressions & Functional Interfaces
Introduced in Java 8.
Example:
interface Drawable { void draw(); }
public class LambdaExample {
public static void main(String[] args) {
Drawable d = () -> [Link]("Drawing...");
[Link]();
}
Streams API
Streams allow functional-style operations on collections.
Example:
List<Integer> numbers = [Link](1,2,3,4);
[Link]().filter(n -> n%2==0).forEach([Link]::println);
Annotations in Java
Annotations provide metadata.
Examples: @Override, @Deprecated, @SuppressWarnings.
Custom annotations can be created using @interface.
Java 8+ Features
- Optional class: avoid NullPointerException.
- LocalDateTime: modern date/time API.
- Modules (Java 9): better modularization.
- Records (Java 16+): concise data carriers.