As we have learned about Primitive Data Types and Classes in Java. It is a good time to dig into the details of String class in Java. String is a special Class in Java which is so closely bound with the Java library that it seems more of a Primitive type in Java.

But there is more of what String  Class provides and we will cover the details in this article.

Creating a String Object

String is a special Class in Java that allows us to store sequence of characters inside double quotes. Since String is a Class whenever we initialise a String we are creating a new object to type String

String myString = "5Balloons";

The above code creates a new String object with name myString and stores value "5Balloons" inside that. Since String is a Class we can also create new String object using new keyword

String myString  = new Stirng("5Balloons");

As a part of String Class Java has provided many utility methods for the String Class which we can see in the below code.

String Utility Methods

public class StringUtility {

    public static void main(String args[]) {
        String myString = "5Balloons";

        // Get length of String
        System.out.println(myString.length());

        // Concatenate String
        myString = myString + ", Java Tutorials !";
        System.out.println(myString);

        // Index of a particular character
        System.out
                .println("Index of , in myString is " + myString.indexOf(','));

        // Convert to Upper Case
        System.out.println(myString.toUpperCase());

    }

}

The above code shows uses of utility methods available in Java String Class. lenght() returns the length of String, indexOf returns the indexOf particular character inside the String.There are various utility methods available with String Class and you will use it more often in your Java Programming. String Doc (JAVA SE 8)

 

 

Comments