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

Java Expressions

Java Expressions

In Java, an expression is a combination of variables, operators, and method invocations that evaluates to a single value. Expressions can be as simple as a single variable or a more complex combination of operations. Understanding expressions is fundamental to programming in Java.

Types of Expressions:

1. Arithmetic Expressions

Arithmetic expressions involve numerical operations.

int a = 5, b = 3;

  • int sum = a + b;      // Addition
  • int difference = a - b; // Subtraction
  • int product = a * b;   // Multiplication
  • int quotient = a / b;  // Division
  • int remainder = a % b; // Modulus

2. Relational Expressions

Relational expressions compare values and result in a boolean value.

int x = 10, y = 20;

  • boolean isEqual = x == y;     // Equal to
  • boolean isNotEqual = x != y;  // Not equal to
  • boolean isGreaterThan = x > y; // Greater than


3. Logical Expressions

Logical expressions involve boolean operators.

boolean condition1 = true, condition2 = false;

  • boolean andResult = condition1 && condition2; // Logical AND
  • boolean orResult = condition1 || condition2;  // Logical OR
  • boolean notResult = !condition1;               // Logical NOT

4. Assignment Expressions

Assignment expressions assign values to variables.

int num = 5;

  • num += 3; // Compound assignment (num = num + 3)

5. Conditional (Ternary) Expressions

Conditional expressions provide a compact way of writing if-else statements.

int a = 10, b = 5;

  • int max = (a > b) ? a : b; // If a is greater than b, assign a; otherwise, assign b.

6. Bitwise Expressions

Bitwise expressions operate on individual bits of integer values.

int p = 5, q = 3;

  • int bitwiseAnd = p & q; // Bitwise AND
  • int bitwiseOr = p | q;  // Bitwise OR
  • int bitwiseXor = p ^ q; // Bitwise XOR
  • int bitwiseComplement=~p;  //Bitwise 1's Complement


7. Method Call Expressions

Method call expressions involve invoking methods.

String greeting = "Hello, ";

String name = "John";

  • String message = greeting.concat(name); // Method invocation


8. Object Creation Expressions

Object creation expressions create instances of classes.

Date currentDate = new Date(); // Creating a new Date object


9. Array Creation Expressions

Array creation expressions create arrays.


  • int[] numbers = new int[5]; // Creating an integer array of size 5


These are just a few examples of the diverse types of expressions in Java. Expressions form the core of Java programs, allowing developers to manipulate data, make decisions, and control the flow of their applications. Understanding how to construct and use expressions is crucial for effective Java programming.

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