Skip to main content

Socket (TCP & UDP) communication in Java

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

Blocks in Java

In Java, a block is a set of zero or more statements surrounded by curly braces `{}`. Blocks are used to define the scope of variables, control structures, methods, and other constructs in Java. Here are some key points about Java blocks:


1.Scope:

   - A block defines a scope in Java, and any variable declared within a block is limited to that scope.

   - Variables declared inside a block are not visible outside of it.


2. Control Flow Structures:

   - Blocks are commonly used with control flow structures such as `if`, `else`, `for`, `while`, and `do-while`.

   - These control structures group multiple statements into a single block.


   if (condition) {

       // statements

   } else {

       // statements

   }



3. Method Bodies:

   - Method bodies in Java are also defined using blocks.

   - The code inside a method is enclosed within curly braces.



   public void myMethod() {

       // method body

   }



4. Initialization Blocks:

   - Initialization blocks are used to initialize instance variables.

   - There are two types: instance initialization blocks (run when an instance of the class is created) and static initialization blocks (run when the class is loaded).


   {

       // instance initialization block

   }


   static {

       // static initialization block

   }



5. Anonymous Blocks:

   - Java allows the creation of anonymous blocks for static initialization.

   - These blocks execute when the class is loaded.



   static {

       // anonymous block

   }



6. Exception Handling:

   - Blocks are used extensively in exception handling with `try`, `catch`, and `finally`.

   - Code that might throw exceptions is enclosed in a `try` block.



   try {

       // code that may throw an exception

   } catch (Exception e) {

       // exception handling

   } finally {

       // cleanup code (optional)

   }



7. Lambda Expressions:

   - Blocks are used in lambda expressions to define the body of the lambda.


   MyFunctionalInterface myLambda = () -> {

       // lambda body

   };



8. Local Variables:

   - Local variables declared inside a block have block-level scope.

   - They are only accessible within that block.


   public void myMethod() {

       int localVar = 10; // local variable

   }


Java blocks provide a structured way to organize and group code, contributing to the readability and maintainability of Java programs.

Comments

Popular posts from this blog

Method Overloading in Java

Method Overloading in Java Method Overloading  is a feature in Java that allows a class to have multiple methods with the same name but different parameter lists. The methods can have a different number or types of parameters. The decision on which method to invoke is made by the compiler based on the arguments provided during the method call.  Example: public class Calculator {     // Method to add two integers     public int add(int a, int b) {         return a + b;     }     // Method to add three integers     public int add(int a, int b, int c) {         return a + b + c;     }     // Method to add two doubles     public double add(double a, double b) {         return a + b;     }     // Method to concatenate two strings     public String concatenate(String str1, String str2) {         return str1 + str2;     } } Method Overloading in Action: public class Main {     public static void main(String[] args) {         Calculator calculator = new Calculator();         // Overloaded meth

Java Runtime Environment (JRE)

Definition : Java Runtime Environment (JRE) is a set of software tools and libraries that enables the execution of Java applications. It provides the necessary runtime support for Java programs to run on various devices and platforms. Components of Java Runtime Environment (JRE): Java Virtual Machine (JVM): Definition: The JVM is a crucial component of the JRE responsible for executing Java bytecode. Functionality: It interprets Java bytecode or, in some cases, uses Just-In-Time (JIT) compilation to translate bytecode into native machine code for improved performance. Importance: JVM abstracts the underlying hardware, allowing Java programs to be platform-independent. Class Libraries: Definition: JRE includes a set of precompiled classes and methods that Java applications can utilize. Functionality: These classes cover a wide range of functionalities, from basic data structures to networking. Importance: Class libraries provide a foundation for developers, offering reusable code