String in Java – Java Fresher Tutorial in Tamil

This tutorial Explain about String in Java. What is the String? String Methods, Where to use the String ? Each method is Explained with a brief Description, Syntax and an Example:

In Java, the String class is used to represent a sequence of characters. String in Java are used for storing text. Here are some key points about using strings in Java.

Declaring and Initializing String in Java:

You can create a String in Java using the following methods:

Using String Literal:

String str1 = "Hello, World!";

Using the new keyword:

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

String Concatenation:

You can concatenate strings in Java using the + operator or the concat method:

string in java payilagam

String Methods:

The String in Java provides numerous methods for manipulating strings. Some common methods include:

length(): Returns the length of the string.

int length = str.length();

charAt(int index): Returns the character at the specified index.

char firstChar = str.charAt(0);

substring(int beginIndex):

It seems like you’re referring to a method or function named substring that takes an integer parameter beginIndex. This is a common method in many programming languages, particularly in those that support string manipulation.
The substring will extend from the specified beginIndex to the end of the original string.

Here’s a general explanation of how it might work in a few programming languages:

String originalString = "Hello, World!";
String substring = originalString.substring(7); // "World!"
equals(String anotherString): 

In this Java example, substring(7) extracts the substring starting from index 7 to the end of the string.

Java String class equals()

In String in Java, the equals() method is a part of the java.lang.String class and is used to compare the content of two strings for equality. Here’s Fresher tutorial in Tamilan explanation of how it works:

public class StringEqualsExample {
    public static void main(String[] args) {
        String str1 = "Hello";
        String str2 = "Hello";
        String str3 = new String("Hello");

        // Using equals() to compare strings
        System.out.println(str1.equals(str2)); // true
        System.out.println(str1.equals(str3)); // true
    }
}

In this example:

  1. str1 and str2 are two string literals, and str3 is a string created using the new keyword, which explicitly creates a new string object.
  2. The equals() method compares the content of the strings, not their memory addresses.

The output of the code above will be:

true
true

This is because the equals() method compares the characters of the strings and returns true if the content is the same, and false otherwise.

It’s important to note that the == operator in Java compares object references, not the content of the strings. So, while str1 == str2 may be true due to string interning, str1 == str3 would be false because str3 is a new object created with new.

For string comparison based on content, it’s recommended to use the equals() method. Additionally, for case-insensitive comparison, you can use equalsIgnoreCase():

String str4 = "hello";
System.out.println(str1.equalsIgnoreCase(str4)); // true

This will output true because equalsIgnoreCase() performs a case-insensitive comparison.

indexOf(String str):

the indexOf(String str) method is a part of the java.lang.String class. It is used to find the index of the first occurrence of a specified substring (str) within the calling string. If the substring is not found, it returns -1.

public class StringIndexOfExample {
    public static void main(String[] args) {
        String mainString = "Hello, World!";
        
        // Using indexOf() to find the index of a substring
        int index1 = mainString.indexOf("World"); // 7
        int index2 = mainString.indexOf("Java");  // -1
        
        System.out.println("Index of 'World': " + index1);
        System.out.println("Index of 'Java': " + index2);
    }
}

In this example:

  • index1 will be set to the index of the substring “World” within the mainString. In this case, it is 7 because “World” starts at index 7 in the string “Hello, World!”.
  • index2 will be set to -1 because the substring “Java” is not found in the mainString.

Keep in mind that indexOf is case-sensitive. If you want a case-insensitive search, you can use toLowerCase() or toUpperCase() to normalize the strings before calling indexOf.

String mainString = "Hello, World!";
int index3 = mainString.toLowerCase().indexOf("world"); // 7

In this example, toLowerCase() is used to convert the original string to lowercase, and then indexOf is called on the lowercase version. This ensures a case-insensitive search.

toUpperCase() Method:

The toUpperCase() method is a string manipulation method in many programming languages that converts all characters in a string to uppercase. This means that if the original string contains lowercase letters, they will be converted to their uppercase equivalents, while any characters that are already uppercase or non-alphabetic will remain unchanged.

Here are examples of using toUpperCase() in various programming languages:

String upperCaseStr = str.toUpperCase();


Example:-
String original = "Hello, World!";
String uppercased = original.toUpperCase();
System.out.println(uppercased);

Output:-
HELLO, WORLD!

toLowerCase() Method:-

The toLowerCase() method is a string manipulation method in many programming languages that converts all characters in a string to lowercase.

Here are examples of using toLowerCase() in various programming languages:

String original = "Hello, World!";
String lowercased = original.toLowerCase();
System.out.println(lowercased);

Output:-
hello, world!

String Immutability:

Strings in Java are immutable, meaning their values cannot be changed after they are created. When you perform operations on a string, a new string is created.

String original = "Hello";
String modified = original.concat(", World!"); // Creates a new string

System.out.println(original); // Output: Hello
System.out.println(modified); // Output: Hello, World!

Where to Use Strings:

String in Java are used in various scenarios, including:

Text Representation:

  • User Interface (UI): Displaying text in user interfaces, such as labels, buttons, and messages.
  • Web Development: Representing HTML content, handling form data, and managing URLs.
  • Console Output: Printing information to the console or terminal for debugging or user interaction.

Data Storage and Manipulation:

  • Database Operations: Storing and retrieving textual data from databases.
  • File Handling: Reading and writing text files, parsing data from files.
  • Serialization: Converting data structures to a string representation for storage or transmission.

Data Processing:

  • Text Processing: Manipulating and analyzing textual data, such as searching, replacing, and formatting.
  • Regular Expressions: Pattern matching and manipulation of strings based on specific patterns.
  • Parsing: Extracting relevant information from structured or formatted strings.

Error Handling and Logging:

  • Error Messages: Providing informative error messages to users or developers.
  • Logging: Recording events and information in log files for debugging and monitoring.

https://payilagam.com/java/string-in-java-tamil-tutorial-for-freshers/

Reference: chat.openai.com