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(); ...
The `Object` class in Java serves as the root of the class hierarchy. Here are some key points to understand about the `Object` class along with examples: 1. Default Superclass: If a class doesn't extend any other class explicitly, it implicitly inherits from the `Object` class. public class MyClass { // MyClass inherits from Object implicitly } 2. toString() Method: Provides a string representation of the object. It is commonly overridden to return meaningful information about the object. public class Student { private String name; private int age; // Constructor and other methods... @Override public String toString() { return "Student{name='" + name + "', age=" + age + '}'; } } 3. equals() Method: Compares...