Skip to main content

Posts

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

The final keyword

The `final` keyword in Java is used to define constants, prevent method overriding, and create immutable classes. Here are the main uses of the `final` keyword: 1. Final Variables: When applied to a variable, the `final` keyword indicates that the variable's value cannot be changed once initialized. 2. Final Methods: When applied to a method, the `final` keyword indicates that the method cannot be overridden by subclasses. This is commonly used in class inheritance to enforce certain behaviors that should not be modified. 3. Final Classes: When applied to a class, the `final` keyword indicates that the class cannot be subclassed. This is often used to create immutable classes, which cannot be extended or modified. Here are some examples to illustrate the usage of the `final` keyword: Final Variables: public class FinalExample {     final int MAX_VALUE = 100; // Constant variable     public static void main(String[] args) {         FinalExample obj = new FinalExample();         //

Getter and setter methods

Getter and setter methods, also known as accessor and mutator methods, are used to retrieve and modify the values of private variables (fields) in a class. They provide controlled access to the class attributes, allowing encapsulation and maintaining data integrity. Here's an example demonstrating getter and setter methods in Java: public class Person {     private String name;     private int age;     // Getter method for the name attribute     public String getName() {         return name;     }     // Setter method for the name attribute     public void setName(String name) {         this.name = name;     }     // Getter method for the age attribute     public int getAge() {         return age;     }     // Setter method for the age attribute     public void setAge(int age) {         if (age >= 0 && age <= 120) { // Validate age             this.age = age;         } else {             System.out.println("Invalid age! Age should be between 0 and 120.");  

Java Code for Matrix Operations (Addition, Subtraction & Multiplication)

  Copy import java.util.Scanner; public class MatrixOperations { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Input matrix A System.out.println("Enter the number of rows and columns of matrix A:"); int rowsA = scanner.nextInt(); int colsA = scanner.nextInt(); int[][] matrixA = new int[rowsA][colsA]; System.out.println("Enter the elements of matrix A:"); for (int i = 0; i < rowsA; i++) { for (int j = 0; j < colsA; j++) { matrixA[i][j] = scanner.nextInt(); } } // Input matrix B System.out.println("Enter the number of rows and columns of matrix B:"); int rowsB = scanner.nextInt(); int colsB = scanner.nextInt(); int[][] matrixB = new int[rowsB][colsB]; System.out.println("Enter the elements of matrix B:");

Second Largest number in the Array

  Copy import java.util.Scanner; public class SecondLargestInArray { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the size of the array: "); int size = scanner.nextInt(); int[] arr = new int[size]; System.out.println("Enter the elements of the array:"); for (int i = 0; i < size; i++) { arr[i] = scanner.nextInt(); } int largest = arr[0]; int secondLargest = Integer.MIN_VALUE; for (int i = 1; i < size; i++) { if (arr[i] > largest) { secondLargest = largest; largest = arr[i]; } else if (arr[i] > secondLargest && arr[i] != largest) { secondLargest = arr[i]; } } if (secondLargest == Integer.MIN_VALUE) { System.out.println(&qu

Basic Java Programs

  Copy public class BasicJavaProgram { public static void main(String[] args) { // Variables int num1 = 10; int num2 = 5; // Arithmetic Operators int sum = num1 + num2; int difference = num1 - num2; int product = num1 * num2; int quotient = num1 / num2; int remainder = num1 % num2; // Comparison Operators boolean isEqual = num1 == num2; boolean isNotEqual = num1 != num2; boolean isGreater = num1 > num2; boolean isLess = num1 < num2; boolean isGreaterOrEqual = num1 >= num2; boolean isLessOrEqual = num1 <= num2; // Logical Operators boolean logicalAnd = (num1 > 0) && (num2 < 10); boolean logicalOr = (num1 > 0) || (num2 < 10); boolean logicalNot = !(num1 == num2); // Conditional Statements if (num1 > num2) { System.out.println("num1 is grea

Basic Java program for implementation operators, variables, control flow statements

 public class BasicJavaProgram {     public static void main(String[] args) {         // Variables         int num1 = 10;         int num2 = 5;         // Arithmetic Operators         int sum = num1 + num2;         int difference = num1 - num2;         int product = num1 * num2;         int quotient = num1 / num2;         int remainder = num1 % num2;         // Comparison Operators         boolean isEqual = num1 == num2;         boolean isNotEqual = num1 != num2;         boolean isGreater = num1 > num2;         boolean isLess = num1 < num2;         boolean isGreaterOrEqual = num1 >= num2;         boolean isLessOrEqual = num1 <= num2;         // Logical Operators         boolean logicalAnd = (num1 > 0) && (num2 < 10);         boolean logicalOr = (num1 > 0) || (num2 < 10);         boolean logicalNot = !(num1 == num2);         // Conditional Statements         if (num1 > num2) {             System.out.println("num1 is greater than num2");