Socket communication in Java enables communication between two endpoints over a network. There are two main types of sockets: TCP sockets and UDP sockets. Let's explain both types with examples: TCP Socket Communication: 1. **Server Side**: - The server creates a `ServerSocket` object to listen for incoming connections on a specific port. - When a client connects, the server accepts the connection and creates a `Socket` object to communicate with the client. - The server reads from and writes to the socket's input and output streams to communicate with the client. import java.io.*; import java.net.*; public class TCPServer { public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(12345); System.out.println("Server started. Waiting for client..."); Socket clientSocket = serverSocket.accept(); System.out.println("Client connected."); BufferedReader in = new Bu
Garbage collection (GC) is a fundamental feature of the Java Virtual Machine (JVM) that manages memory allocation and deallocation in Java programs. It automatically identifies and reclaims memory that is no longer in use, freeing it up for future allocation. Here's an overview of garbage collection in Java: 1. Automatic Memory Management: - Unlike languages like C and C++, where developers manually allocate and deallocate memory using `malloc()` and `free()`, Java employs automatic memory management through garbage collection. - With automatic memory management, developers don't need to explicitly deallocate memory, reducing the risk of memory leaks and segmentation faults. 2. Heap Memory: - In Java, objects are allocated memory from the heap, which is a region of memory managed by the JVM. - The heap is divided into two main areas: the young generation and the old generation (also known as the new generation and the tenured generation, respectively). - The youn