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.

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:

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.

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.

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:

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.