Java String

String is a sequence of characters which are enclosed in double quotes.

Here is an example of a String variable greeting to which we assign a String literal "Hello, have a nice day!" enclosed in double quotes.

Example

package com.example;

public class StringExample {

    public static void main(String[] args) {

        String greeting = "Hello, have a nice day!";
        System.out.println(greeting);
    }
}
Output
Hello, have a nice day!

String Class

Java also provides the String class to create and manipulate strings. The String class is immutable, which means once it is created, it cannot be changed. A String object can be created by using the new keyword with a constructor.

Here is an example of creating a String object from String class:


package com.example;

public class StringExample {

    public static void main(String[] args) {

        String stringObject = new String("I am learning Java.");

        System.out.println(stringObject);
    }
}
Output
I am learning Java.

Methods in String class

Some of the methods of String class are:

The length() Method

The length() method is used to obtain the number of characters contained in the string object.

Example

package com.example;

public class StringExample {

    public static void main(String[] args) {

        String message = "The cloud is blue";
        int len = message.length();
        System.out.println(len);
    }
}
Output
17

Note: Methods which are used to obtain information about an object are known as accessor methods.

The concat() method

The String class provides a concat() method for concatenating two strings:

Example

package com.example;

public class StringExample {

    public static void main(String[] args) {

        String string1 = "The sky is blue.";
        String string2 = "The earth is round.";

        String result = string1.concat(" ").concat(string2);
        System.out.println(result);
    }
}
Output
The sky is blue. The earth is round.

You can also use the concat() method with string literals as shown below:


"The sky is ".concat("blue");

You can also use the + operator to concatenate Strings in Java.

Example

"Hello," + " world" + "!"

The + operator can also be used to concatenate the end of each line in a multi-line string.

Example

String message = "Do not keep your knowledge to your self " + 
"let it travel as far as it can.";

The format() method

The String class has static format() method that allows to create a formatted string which can be reused.

Example

package com.example;

public class StringExample {

    public static void main(String[] args) {

        float floatValue = 300;
        int intValue = 40;
        String stringMsg = "Hello world";

        String formattedMsg = String.format("The value of the float variable is %f,"
                + " while the value of the integer variable is %d," 
                + " and the string is %s", floatValue, intValue, stringMsg);

        System.out.println(formattedMsg);
    }
}
Output
The value of the float variable is 300.000000, while the value of the integer variable is 40, and the string is Hello world