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
Sorting arrays of objects in Java involves implementing the `Comparable` interface or providing a custom `Comparator`. Here's an overview along with some notes and explanations: Sorting Arrays of Objects Using Comparable Interface: 1. Implementing Comparable Interface: To enable natural ordering, objects in the array must implement the `Comparable` interface and override the `compareTo()` method to define the sorting logic. public class MyClass implements Comparable<MyClass> { private int id; private String name; // Constructor, getters, setters @Override public int compareTo(MyClass other) { return Integer.compare(this.id, other.id); } } 2. Using `Arrays.sort()`: Once the `compareTo()` method is implemented, you can simply call `Arrays.sort()` to sort the array. MyClass[] myArray = {obj1, obj2, obj3}; Arrays.sort(myArray); Sorting Arrays of Objects Using Custom Comparator: 1. Impl