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
In Java, the `instanceof` operator is used to check whether an object is an instance of a particular class or implements a particular interface. It returns `true` if the object is an instance of the specified class or implements the specified interface; otherwise, it returns `false`. Syntax: object instanceof ClassName or object instanceof InterfaceName - `object`: The object whose type is to be checked. - `ClassName`: The name of the class. - `InterfaceName`: The name of the interface. Example: class Animal {} class Dog extends Animal {} class Cat extends Animal {} public class Main { public static void main(String[] args) { Animal a = new Dog(); System.out.println(a instanceof Animal); // true System.out.println(a instanceof Dog); // true System.out.println(a instanceof Cat); // false } } In this example: - `a instanceof Animal` returns `true` because `a` is an instance of `Animal`. - `a instanceof Dog` returns `true` because `a` is also