Overview
This lecture introduces Java programming, focusing on core concepts, object-oriented principles, data types, exception handling, file I/O, Java Collections, and XML/JSON processing.
Introduction to Java
- Java is an object-oriented programming (OOP) language where everything is represented as objects with state and behavior.
- Java was developed by Sun Microsystems in 1995, is open source, and currently maintained by Oracle.
- Key features: simple syntax, high performance, secure (no pointers), robust, portable (write once, run anywhere), dynamic, distributed, and multi-threaded.
- Widely used in Android apps, finance, web applications, embedded systems, scientific computing, big data, and IoT.
Java Setup and Internals
- Install JDK specific to your OS; add it to the system PATH to run Java commands.
- Use an IDE (Eclipse, IntelliJ) for code development.
- Java source code (.java) is compiled to bytecode (.class), then executed by JVM.
- JVM is platform-specific and enables Java’s platform independence.
Basic Java Concepts
- Variables: Hold values; types include local (method scope), instance (object scope), static (class scope).
- Data Types: Eight primitives (byte, short, int, long, float, double, char, boolean); non-primitives include String, arrays, and user-defined classes.
- Type Conversion: Implicit (automatic), explicit (type casting).
- Operators: Arithmetic, relational, logical, assignment, bitwise, unary, and ternary.
Control Statements and Methods
- Control Statements: if, if-else, switch, for, while, do-while, break, continue, return.
- Methods: Define class behavior, take parameters, may return values, improve code reuse and readability.
- Method Overloading: Multiple methods with same name, different parameters.
Object-Oriented Programming
- Class: Blueprint defining state (fields) and behavior (methods).
- Object: Instance of a class.
- Access Modifiers: public, private, protected, default—control visibility.
- Constructor: Special method for object initialization; can be overloaded.
- Inheritance: "is-a" relationship; enables method overriding and code reuse.
- Polymorphism: Same interface, different implementations (overloading/overriding).
- Abstraction: Hiding implementation details via abstract classes/interfaces.
- Encapsulation: Bundling data/methods, restricting access using access modifiers.
- Interfaces: 100% abstract; used for specifications and multiple inheritance.
Arrays, Strings, and Wrappers
- Arrays: Fixed-size, same data type; single/multi-dimensional.
- String: Immutable, widely used, supports many utility methods.
- StringBuffer/StringBuilder: Mutable string variants for efficiency.
- Wrapper Classes: Object representation of primitives (Integer, Double, etc.).
Exception Handling
- Exceptions: Disrupt program flow; checked (compile-time) or unchecked (runtime).
- Handling: try-catch-finally blocks; throw/throws for custom/error propagation.
- Custom Exceptions: User-defined by extending Exception.
Java Collections Framework
- List: Ordered, allows duplicates (ArrayList, LinkedList, Vector).
- Set: Unordered, unique elements (HashSet, LinkedHashSet, TreeSet).
- Queue: FIFO (PriorityQueue, ArrayDeque), supports typical queue operations.
- Map: Key-value pairs, unique keys (HashMap, LinkedHashMap, TreeMap).
- Generics: Type safety for collections.
- Comparable/Comparator: Sorting mechanisms for user-defined classes.
File I/O and Serialization
- Use java.io package for reading/writing files.
- Stream-based I/O: InputStream/OutputStream for bytes, Reader/Writer for characters.
- Serialization: Convert object state to byte stream for storage/sharing; use Serializable interface.
XML and JSON Processing
- XML: Hierarchical, platform-independent data format; supports DTD/XSD schemas for validation.
- XML Parsers: DOM (tree-based), SAX/StAX (event-based), XPath for querying.
- JSON: Lightweight data exchange format using key-value pairs; preferred for web APIs.
- Java supports reading/writing XML and JSON via external libraries (dom4j, JSON.simple, etc.).
Key Terms & Definitions
- JVM — Java Virtual Machine; executes Java bytecode.
- JDK — Java Development Kit; tools for Java development.
- OOP — Object-Oriented Programming; paradigm centered on objects.
- Polymorphism — Ability of methods to behave differently based on context.
- Encapsulation — Restricting direct access to class internals.
- Serialization — Converting objects to byte streams.
- Collection — Data structure for storing groups of objects.
Action Items / Next Steps
- Set up JDK and IDE (Eclipse/IntelliJ) on your machine.
- Practice writing, compiling, and running basic Java programs.
- Experiment with variables, methods, control statements, and OOP concepts.
- Implement and manipulate data using Java Collections and arrays.
- Write programs for file I/O and exception handling.
- Read and write sample XML and JSON files using Java libraries.
- Review these notes regularly and try coding exercises on each topic.