Getting Started with the Java String Class
A string is a sequence of characters including alphabet letters, special characters, and white spaces. For instance, “How are you?” is a string that contains letters, white spaces and a special character ‘?’. In most of programming languages, strings are declared and managed in the form of character arrays. However, this is not the case with Java where strings are dealt in the form of classes. Java contains a String class packaged in java.lang package. The benefit of implementing strings in the form of objects of a String class is that several important string functionalities can be packaged in the form of utility methods.
To learn more about Java, take a course at Udemy.com.
Instantiating Java String class
The Java String class comes with several constructors that can be used to instantiate string class. Following are some of the ways through which an object of String class can be obtained in Java.
1- Creating an empty string
String str = new String();
The above constructor would create an object str of type String which contains an empty string.
2- Instantiating String object with char array
char strarray [] = {'w','e','l','c','o','m','e'}; String str = new String(strarray);
In the above method a character array is instantiated with some characters and then passed to the constructor of the String class. This constructor will return String type object that contains a string made up of characters of the array passed to the constructor. Now, if String str is displayed on console using the following method:
System.out.println(str);
It will display “welcome” on the console.
The above method has an overload. For instance, to instantiate a string object with a sub-range of characters in a char array, the following method can be used:
char strarray [] = {'w','e','l','c','o','m','e'}; String str = new String(strarray, 2, 4); System.out.println(str);
In the above example, the String constructor receives three parameters, a character array, the starting index of range of character and the number of characters with which the String object has to be instantiated. For instance, in the above case, the “new String(strarray, 2, 4)” constructor returns a String object that contains string “lcom” because ‘l’ is the second index of the strarray and the four characters, starting from ‘l’ are l, c, o and m.
3- Instantiating String object with another object
A string object can also be instantiated by passing another string object to the constructor of the String class. The new object contains the same string as contained by the object that has been passed to the constructor of the new String object. The following example demonstrates this concept.
char strarray [] = {'w','e','l','c','o','m','e'}; String str = new String(strarray); String str2 = new String (str);
In above example, str2 object will contain “welcome” since the object passed to its constructor is str which also contains “welcome”.
4- Instantiating Java String object via string literals
Though, fully qualified constructors can be used to instantiate a String class, there is a simpler way to get a string object. This is using a string literal. A string literal is simply a string value. The following example demonstrates how a string object can be created using string literals:
String str = “Welcome to Udemy”;
You can see from the above line of code how simple it actually is to create a String object via string literal. Here the line “Welcome to Udemy” is a string literal. Behind the scene an object of type String is automatically created by Java.
Want to learn Java in detail? Take a course at Udemy.com.
Java String Class in action (A Basic Example)
The following example contains a complete working example of Java String class. All the concepts that have been studied till now have been implemented in the following example:
class StringTutorial { public static void main (String args[]) { char strarray [] = {'w','e','l','c','o','m','e'}; String str = new String(strarray); String str2 = new String(strarray, 3,3); String str3 = new String(str); String str4 = "Welcome literal"; System.out.println(str); System.out.println(str2); System.out.println(str3); System.out.println(str4); } }
In the above example, a Java class StringTutorial is defined. The class contains one static main method and inside the main method four String class objects have been instantiated through four different constructors. The String object str is initialized by passing a char array strarray. String object str2 has been instantiated by passing a sub-range of the char array strarray. The object str3 is instantiated by passing object str to its constructor, both str and str3 would contain same string. Finally, the str4 object has been instantiated via string literal “Welcome literal”. The output of the above example would be:
welcome
com
welcome
welcome literal
Some Useful Java String Class Functions
Though Java String class contains myriads of extremely helpful built-in functions, some of them are worth mentioning here.
- length()
This method is used to calculate the total number of characters in a string.
- equals(String Object)
Returns true if the String object passed as parameter contains same string as the object on which equals method is called.
- Concatenation “+”
The “+” operator is used to concatenate two strings.
- charAt(index)
This method is used to get the character located at specified index.
- startsWith(String) / endsWith(String)
These methods return true if the string on which they are called contain similar strings at the start and end respectively as the strings that are passed to these methods as a parameter.
In the following example, all of the above discussed methods have been implemented:
class StringTutorial { public static void main (String args[]) { String str = "Welcome literal"; System.out.println("Length of str: "+str.length()); String str2 = "Welcome literal"; System.out.println("str is equal to str2: "+str.equals(str2)); str = str + " concatenation"; System.out.println("str after concatenation: "+str); System.out.println("charAt(0) :"+str.charAt(0)); System.out.println("Starts with Wel : "+str.startsWith("Wel")); } }
In the above example, the length of string str has been displayed by calling str.length() method. Then strings str and str2 have been compared using equals() method which would return true. Next, concatenation operator “+” has been used to concatenate the string “concatenation” with str. Similarly, charAt(0) method has been called on str to get the character at the 0th index of the str string. This would return “W”. Finally, startsWith() method has been used to check if the str string starts with “Wel” which also returns true.
For more interesting Java tutorials, check out a course at Udemy.com.
Recommended Articles
Top courses in Java
Java students also learn
Empower your team. Lead the industry.
Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.