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

Hello World program in Java along with an explanation of each keyword

 Here's a "Hello World" program in Java along with an explanation of each keyword:

public class HelloWorld {

    public static void main(String[] args) {

        System.out.println("Hello, world!");

    }

}

Let's break down the code:

  1. public: This is an access modifier that specifies that the class HelloWorld is accessible by any other class. In analogy, think of it as a door with a sign saying "Open to everyone". Anyone can access the class from anywhere in the program.
  2. class: This keyword is used to declare a class in Java. In our example, we have a class named HelloWorld. Classes in Java are like blueprints or templates for objects. You can think of a class as a recipe for baking a cake, and objects as the actual cakes created from that recipe.
  3. HelloWorld: This is the name of our class. It follows the rules for naming identifiers in Java. It's the convention to start class names with an uppercase letter and use camel case. In our analogy, think of it as the nameplate on the door of a building.
  4. { and }: These curly braces define the beginning and end of the class body. All the code belonging to the class is enclosed within these braces. In our analogy, think of them as the walls of a building enclosing everything inside.
  5. public static void main(String[] args): This is the main method of our program. It's the entry point of execution for Java applications. Let's break down each part:
    • public: This keyword means that the main method can be called from anywhere. It's accessible to all other classes. Analogously, it's like a big sign outside a building saying "Entrance".
    • static: This keyword means that the main method belongs to the class itself, not to any specific instance of the class. You can call it without creating an object of the class. In analogy, it's like a feature of the building that can be accessed without entering it.
    • void: This keyword specifies that the main method doesn't return any value after it's executed. In analogy, it's like a door that you can go through but doesn't give you anything in return.
    • main: This is the name of the method. It's a special name recognized by the Java runtime as the starting point of execution for a Java program.
    •  (String[] args): This part is the parameter list of the main method. It specifies that the main method can accept an array of strings as arguments. In analogy, it's like a reception desk where you can provide additional information when entering the building.
  6. System.out.println("Hello, world!");: This line of code prints "Hello, world!" to the console. Let's break it down:
    • System: This is a predefined class in Java that provides access to system resources, like input, output, and error streams.
    • out: This is a static member of the System class, which represents the standard output stream. It's where data written to the console is displayed.
    • println(): This is a method of the PrintStream class (which is represented by the out object). It's used to print a string followed by a newline character to the console.
    • "Hello, world!": This is the string literal that we want to print. It's enclosed in double quotes to indicate that it's a string.


Overall, the "Hello World" program demonstrates the basic structure of a Java program, including the class declaration, the main method, and how to print output to the console. 

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