In this post we will study what is strings in Java and why they are immutable. Let's start by understanding what strings are in Java
In Java, a string is a sequence of characters that is used to represent text.. Strings are immutable in Java that means once a string is created, its value cannot be changed. We will explore it later in the article.
In Java, there are different ways to create strings. You can use double quotes "" or the new keyword. We will see how to put strings together, which is called string combination. We will also discover efficient ways to combine strings using the + operator and the concat() method. Let's learn about these methods and when to use them.
Here is the Example of Creating String:
String greeting = "Hello!"; // Creating a string using double quotes
String name = new String("John"); // Creating a string using 'new' keyword
Let's see how to combine above strings using + and concat() method.
String example1 = greeting + " My name is " + name; // Combining strings using the '+' operator
String example2 = greeting.concat(name); // Combining strings using concat() method
Methods for Manipulating Strings:
Strings have built-in methods that allow us to manipulate them in various ways. Like some commonly used methods are length(), charAt(), substring(), toLowerCase(), toUpperCase(), and more. Understanding and using these methods effectively will give us a good foundation for working with text in Java.
Example 1:
//Example of length()
String message = "Java is awesome!";
int length = message.length();
System.out.println("Length of the string is: "+length);//16
// Example of charAt() method
char secondChar = message.charAt(1);
System.out.println("Second character: " + secondChar); // Output is: a (because string index starts from 0)
// Example of substring() method
String substring = message.substring(8);
System.out.println("Substring: " + substring); // Output: awesome!
length(): returns length of the given string
//Example of toLowerCase() and toUpperCase()
String text = "CodeForHunger";
String lowercase = text.toLowerCase(); // Converts the string to lowercase ("codeforhunger")
String uppercase = text.toUpperCase(); // Converts the string to uppercase ("CODEFORHUNGER")
Understanding String Immutability:
Strings in Java are immutable, that simply means their value cannot be changed once created. The value that the string holds when it is first made stays the same throughout its existence . In other words, you cannot modify the characters or text within a string once it is set.
Here is the Example:
String message = "Hello";
message = message + " Welcome to codeforhunger.com!"; // This creates a new string object
Comparing Strings and Searching:
We can also perform operations on strings such as comparing strings and searching for specific patterns within them. We'll explore different ways to compare strings using methods like equals() and compareTo().
String str1 = "apple";
String str2 = "banana";
boolean isEqual = str1.equals(str2); // Checks if the two strings are equal (in this case, false)
int compareResult = str1.compareTo(str2); // Compares the two strings lexicographically (-1, as "apple" comes before "banana")
Strings are essential for text manipulation in Java, and understanding how to use them effectively can greatly enhance our programming skills. In this article, we've explored the basics of strings, creating and combining them, manipulating methods, immutability, string comparison and searching. Now that you have this knowledge, you can confidently use strings to make your coding journey even better.
For more in-depth understanding of strings and to test your knowledge with interview questions, check out our comprehensive article on strings interview questions and answers.
No comments:
Post a Comment
If you have any doubts, please discuss here...👇