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

String Class and Methods in Java

String Class and Methods in Java

The `String` class in Java is part of the `java.lang` package and is used to represent a sequence of characters. Strings in Java are immutable, meaning that once a `String` object is created, its value cannot be changed. The `String` class provides various methods to manipulate and perform operations on strings.


1. Creating Strings:

Strings can be created in Java using the following methods:

// Using string literal

String str1 = "Hello, World!";

// Using the new keyword

String str2 = new String("Hello, World!");


2. String Length:

The `length()` method returns the length of the string (number of characters).


String str = "Hello, World!";

int length = str.length(); // Returns 13


3. Concatenation:

The `concat()` method or the `+` operator is used for string concatenation.


String str1 = "Hello";

String str2 = "World";

String result = str1.concat(", ").concat(str2); // or, str1 + ", " + str2


4. Substring:

The `substring()` method is used to extract a portion of the string.

String str = "Hello, World!";

String subStr = str.substring(7); // Returns "World!"


5. String Comparison:


String comparison can be done using the `equals()` method for content comparison and `compareTo()` for lexicographical comparison.


String str1 = "Hello";

String str2 = "World";

boolean isEqual = str1.equals(str2); // Returns false

int result = str1.compareTo(str2); // Returns a negative value


6. Changing Case:

The `toUpperCase()` and `toLowerCase()` methods change the case of the string.

String str = "Hello, World!";

String upperCase = str.toUpperCase(); // Returns "HELLO, WORLD!"

String lowerCase = str.toLowerCase(); // Returns "hello, world!"


7. String Formatting:

The `format()` method is used to format strings similarly to `printf` in C.

String formattedStr = String.format("Value: %d, Text: %s", 42, "Java");


8. Checking Substring:

The `contains()` method checks if a string contains a specified sequence of characters.


String str = "Hello, World!";

boolean containsHello = str.contains("Hello"); // Returns true



9. Trimming Whitespace:

The `trim()` method removes leading and trailing whitespaces.


String str = "   Hello, World!   ";

String trimmedStr = str.trim(); // Returns "Hello, World!"


10. Converting to Other Types:

The `valueOf()` method is used to convert other data types to strings.


int number = 42;

String str = String.valueOf(number); // Converts int to String



These are some of the commonly used methods provided by the `String` class in Java. Understanding and using these methods effectively will enhance your ability to work with strings in Java.

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