Java for Everyone...Strings
Strings are a sequence of characters, used to represent textual data. In Java its an object provided by the java.lang.String class which is part of Java's core library.
Strings are immutable, once created values cannot be modified, any operation that appears to modify a string creates a new string.
?Key characteristics-
Ordered-Strings are made up of a sequence of characters, such as letters, digits, symbols, or spaces and these sequences of characters that have a unique position for each character.
Indexable -Individual characters within a string can be accessed using a numerical index.
Comparable-Strings can be compared to each other to determine their relative order or equality.
Immutable-In some programming languages, such as Java, Python, JavaScript, and C#, strings are immutable as mentioned above.
Enclosed by quotation marks-Strings are usually indicated by single or double quotation marks.
Creating Strings in Java
Using String Literals: All collection of characters surrounded by double quotes.
public class HelloWorld {
public static void main(String[] args) {
String s = "Hello World!";
System.out.println(s);
}
}
In this case, "Hello world!" is a?string literal—a series of characters in your code that is enclosed in double quotes. Whenever it encounters a string literal in your code, the compiler creates a?String?object with its value—in this case,?Hello world!
Strings defined with double quotes are automatically placed in the string pool.
Using the new Keyword:
You can create?String?objects by using the?new?keyword and a constructor.
public class stringHelloWorld {
public static void main(String[] args) {
/*Strings defined with double quotes are automatically placed in the string pool.
*/
String s1 = "Hello World!";
/*
* Creates a new string object in the heap, even if the same value exists in the string pool
*/
String s2= new String("Hello World!");
System.out.println(s1);
System.out.println(s2);
}
}
Please note -Creates a new string object in the heap, even if the same value exists in the string pool.
The String class provides various methods for string manipulation…
1.????? length()->Returns the length of the string
2.????? charAt(int index)->Returns the character at the specified index
3.????? substring(int start, int end)->Extracts a substring from the string
4.????? contains(CharSequence seq)->Checks if the string contains the specified sequence
5.????? equals(Object obj)->Compares the string for content equality
6.????? equalsIgnoreCase(String str)->Compares strings, ignoring case differences
7.????? toUpperCase()->Converts all characters to uppercase
8.????? toLowerCase()->Converts all characters to lowercase
9.????? trim()->Removes leading and trailing spaces.
10. replace(char oldChar, char newChar)->Replaces occurrences of a character.
11. split(String regex)->Splits the string into an array based on the regex
?Special Characters in Strings -since string must be written within quotes, Java will misunderstand this string, and generate an error, to address this issue backslash is used as escape character.
Escape character??????????? Result Description
\'??????????? '???????????? ??????????? Single quote
\"?????????? "???????????? ???????????? Double quote
\\?????????? \???????????? ??????????????? Backslash
Some Examples-
1.????? String Length-length () is used to identify the length of the input string.
?public class StringLengthdemo {
public static void main(String[] args) {
String s = "This is my first string";
System.out.println("Length of the string is :" + s.length());
}
}
?
2.????? Finding index of character in a String- The indexOf() method returns the index (the position) of the first occurrence of a specified text in a string, this includes the whitespace as well.
?public class indexOf {
public static void main(String[] args) {
String s = "This is my first string";
System.out.println("IndexOf input string is "+s.indexOf("m"));
}
}
?
3.????? String Concatenation - The + operator is used be used to combine two strings , called concatenation.
?public class StringConcat {
public static void main(String[] args) {
String FirstName= "Mr Arjun";
String LastName= "M";
System.out.println("Full Name is- "+FirstName+" "+LastName);
}
}
?
Please note- Java uses the + operator for both addition and concatenation, Numbers are added. Strings are concatenated
Food for thoughts-
·?????? How are strings stored in Java?
·?????? Is String a primitive data type in Java?
·?????? Why is String immutable in Java?
·?????? How is a String object created in Java?
·?????? What is the difference between String literal and new String()?
·?????? How does the JVM handle string literals?
·?????? What is the String Pool in Java?
·?????? Why is String immutable and final in Java?
·?????? Can a String contain null values?
·?????? How does the concat() method differ from using the + operator?
·?????? How does the equals() method differ from the == operator in strings?
·?????? How do immutable strings improve security in Java?
·?????? Are String objects thread-safe?
·?????? What happens if you compare a String with null?
/*
This is article is based on JDK 8, any changes on features, enhancement or deprecated options can be referred to official release notes-
*/
?