Java Programming Notes
Data Types in Java
Primitive Data Types:
- byte (1 byte)
- short (2 bytes)
- char (2 bytes)
- boolean (1 bit)
- int (4 bytes)
- long (8 bytes)
- float (4 bytes)
- double (8 bytes)
User-Defined Data Types:
- class
- Array
Type Casting in Java:
- Conversion between types is necessary when different data types interact.
- Two Types of Type Casting:
- Upcasting: Automatic conversion from a smaller type to a larger type (e.g. int to float).
- Downcasting: Manual conversion from a larger type to a smaller type (e.g. float to int).
- Implicit Casting: Automatically done when no data loss occurs.
- Explicit Casting: Required when there is potential data loss (e.g.
(int) f).
Wrapper Classes:
- Classes that encapsulate primitive data types:
- Byte, Integer, Character, Boolean
Access Modifiers
- Private: Members are accessible only within the class.
- Default (Package-Private): Accessible only within the same package.
- Protected: Accessible within the same package and by subclasses.
- Public: Accessible from anywhere.
Inheritance in Java
- Definition: A mechanism where one object acquires the properties and behaviors of another.
- Types of Classes:
- Subclass (Child class): Inherits from another class.
- Superclass (Parent class): The class from which properties are inherited.
- Syntax:
class SubclassName extends SuperclassName {
// methods
}
- Types of Inheritance:
- Single Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Multiple and Hybrid Inheritance (supported via interfaces only).
Example of Inheritance
class Employee {
float salary = 40000;
}
class Programmer extends Employee {
int bonus = 10000;
}
public class TestInheritance {
public static void main(String[] args) {
Programmer p = new Programmer();
System.out.println("Programmer salary is:" + p.salary);
System.out.println("Bonus of Programmer is:" + p.bonus);
}
}
- Output:
- Programmer salary is: 40000
- Bonus of Programmer is: 10000
Exception Handling in Java
Definition: A mechanism to handle runtime errors, allowing normal flow of the application to continue.
Key Concepts:
- Exception: An event that disrupts the normal flow of the program.
- Error: Serious issues that a typical application should not try to catch.
Types of Exceptions:
- Checked Exceptions: Checked at compile-time. (e.g., IOException)
- Unchecked Exceptions: Checked at runtime. (e.g., ArithmeticException)
Try-Catch Blocks:
- Structure to handle exceptions.
try {
// code that might throw exception
} catch (ExceptionType e) {
// code to handle exception
} finally {
// code that will always execute
}
User Interface in Java: AWT and Swing
AWT (Abstract Window Toolkit):
- Platform-dependent GUI components.
- Components are heavyweight, slower and utilize native OS components.
Swing:
- Built on AWT; provides lightweight components.
- Components are platform-independent, faster, and can be customized with different looks.
Key Differences:
- AWT components are heavyweight vs.swing components are lightweight.
- Swing components use a pluggable look and feel depending on the L&F.
JDBC (Java Database Connectivity)
JDBC Overview: A Java API that allows Java programs to connect to a database.
JDBC Drivers: Different types include:
- Type I Driver: JDBC-ODBC bridge (OBSOLETE).
- Type II Driver: Native API driver.
- Type III Driver: Network protocol driver.
- Type IV Driver: Thin driver (fastest and pure Java).
Steps for JDBC Operations:
- Load the driver:
Class.forName("com.mysql.cj.jdbc.Driver"). - Establish the connection.
- Create statement:
Statement stmt = connection.createStatement();. - Execute query and handle results.
- Close the connection.
- Load the driver:
Networking in Java
- Sockets: An endpoint for communication. It combines IP address and port number.
- TCP vs. UDP:
- TCP (Transmission Control Protocol): Connection-oriented, reliable, error-checking.
- UDP (User Datagram Protocol): Connectionless, suitable for faster data transmission, less overhead.
RMI (Remote Method Invocation)
RMI Overview: Allows Java objects to invoke methods of an object running on another Java Virtual Machine.
Steps:
- Define the remote interface.
- Implement the interface on the server side.
- Start the RMI registry.
- Bind the implementation object.
- Create client to look up the remote object.
Advantages of RMI:
- Simplicity in building distributed applications.
- Language-independent communication.
Memory Management
- Garbage Collection in Java: Automatic memory management and resource reclamation provides efficient memory utilization.