forked from hotwax/training-assignment
-
Notifications
You must be signed in to change notification settings - Fork 0
Module 2: Strings
Rishabh Malviya edited this page Feb 7, 2023
·
1 revision
- A string is a sequence of characters.
- The
Stringclass is immutable, which means that once aStringobject is created, it cannot be changed. - Strings are stored in the
String Poolin the heap memory. - There are different methods are available in the
Stringclass to perform operations on strings.
To create a string, use the following syntax:
class Main {
public static void main(String[] args) {
String str = "Hello World";
String str1 = new String("Hello World");
String str2 = new String(new char[]{'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd'});
String str3 = new String(new byte[]{72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100});
System.out.println(str, str1, str2, str3);
}
}String Buffer
- The
StringBufferclass in Java is used to create mutable (modifiable) string. - The
StringBufferclass in Java is not-synchronized means two threads can't call the methods of StringBuffer simultaneously. - It is less efficient than
StringBuilderclass.
class Main{
public static void main(String[] args){
StringBuffer str = new StringBuffer("Hello");
str.append(" World");
System.out.println(str);
}
}String Builder
- The
StringBuilderclass in Java is used to create mutable (modifiable) string. - The
StringBuilderclass in Java is same asStringBufferclass except that it is non-synchronized. - It is more efficient than
StringBufferclass. - Both
StringBufferandStringBuilderhas different methods to perform operations on strings.
class Main{
public static void main(String[] args){
StringBuilder str = new StringBuilder("Hello");
str.append(" World");
System.out.println(str);
}
}- The
Stringclass in Java is immutable, which means it cannot be changed. - String manipulation is changing the value of a string.
- There are lots of methods available in the
Stringclass to perform operations on strings. - The result of the operation is always a new string.
String s = new String("Hello World");
s.length(); // returns the length of the string
s.charAt(0); // returns the character at the specified index
s.indexOf('o'); // returns the index of the specified character
s.substring(0, 5); // returns the substring from the specified index
s.toUpperCase(); // returns the string in uppercase
s.toLowerCase(); // returns the string in lowercase
s.trim(); // removes the leading and trailing spaces
s.replace('o', 'a'); // replaces the specified character with another character
s.startsWith("Hello"); // checks if the string starts with the specified string
s.endsWith("World"); // checks if the string ends with the specified string
s.equals("Hello World"); // checks if the string is equal to the specified stringSimilarly more methods are available in the
Stringclass to perform operations on strings.Click here to view all the methods available in the
Stringclass.
- Java stores strings in the
String Poolin the heap memory. TheString Poolis a special memory area in the heap memory. - Whenever we perform any operation on string a new string is created in the
String Pool. And the old one deletes everytime. - Operations on strings are more time taking as compared to other data types. to overcome this java introduced
StringBufferandStringBuilderclasses. - The Operations on
StringBufferandStringBuilderare less time taking as compared toStringclass. - There are two ways to create a
stringin Java.- String Literal
- When we are creating a new string using string literal, if the string is already present in the
String Poolthen the string is not created again.
- When we are creating a new string using string literal, if the string is already present in the
- Using
newkeyword- When we are creating a new string using
newkeyword, a new string is always created in theString Pool.
- When we are creating a new string using
- String Literal