Java Strings

Informative infographic illustrating the key features of Java Strings. Learn about immutability, common methods, and efficient string manipulation. A comprehensive guide for developers

Strings in Java

Java programming language comes up with the String class that can be used for creating strings in programs.

The strings are widely used in programming languages that are a combination of characters.

As the String is a class in Java, you have to create objects for creating any string.

//Creating a simple string
String Str_demo = "Java";
//Creating by ‘new’ keyword
String Str_demo = new String();

Before going into further details, have a look at how you may create strings in Java.

An example of creating a simple string

A string can be created just like primitive type variables in the Java program as follows:

Code for creating a simple string:

public class String_demo {
    public static void main(String []args) {
        String Str_demo = "This is a Java String tutorial!"; // creating a simple string

        System.out.println(Str_demo);
 } 

}

Output:

This is a Java String tutorial!

So, a simple string can be created without using the new keyword:

String Str_demo = “A string example”;

As this code executes and the compiler finds any string literal, it creates a string object with its value.

Creating string with new keyword example

As such, String is the class; just like creating other objects in Java programming, you may create a string object by using the new keyword and constructor as follows:

The code for creating a string with new keyword:

public class String_demo {

    public static void main(String []args) {
                String Str_demo = new String();
                Str_demo = "The string class has 40 methods!";
        System.out.println(Str_demo);
 } 
}

Result:

The string class has 40 methods!

An example of creating string from an array

You may use different sources for creating the strings. In the above examples, I created a string that is enclosed in double quotes. You may create a string from an array or other sources.

See an example below:

The code:

public class String_demo {

    public static void main(String []args) {

                char[] charArr = { 'S', 'a', 'y', ' ', 'h', 'e','l','l','o' };

                String Str_demo = new String(charArr);

        System.out.println(Str_demo);
 } 
}

Output:

Say hello

Main points about Java strings

  • A string is a sequence of characters.
  • The String is a class in Java.
  • The string class is immutable. That means a string object cannot be changed after it is created.
  • Number of constructors of the string class is eleven.
  • The string class has plenty of useful functions. A few are demonstrated below.

An example of using the string length function

The string length method can be used to get the total number of characters in the given string object. See this example where I have created the following string object:

String Str_demo = “The string in Java is immutable”;

See the code and output for getting the length of that string in Java program:

The code:

public class String_demo {

    public static void main(String []args) {

        String Str_demo = "The string in Java is immutable"; // creating a simple string
        int str_len = Str_demo.length();      
        System.out.println("The length of the string is = " + str_len);
 } 
}

Output:

The length of the string is = 31

The example of using the concat method

The strings can be concatenated by using the string class method concat().

You may concatenate string objects including string literals. Have a look at this example where three strings are created.

First is the string object. The second is a string object with a value. The third string is the concatenated string of the other two strings along with its own value:

The code:

public class String_demo {

    public static void main(String []args) {

 

        String Str_1 = "series of characters and "; // creating a simple string
        String Str_2 = new String();
        String Str_3 = new String();
        Str_2 = "immutable in Java.";
        Str_3 = "The Strings are " .concat(Str_1) .concat(Str_2);

        System.out.println(Str_3);

  } 

}

Output:

The Strings are series of characters and immutable in Java.
You may also use the + for concatenating strings, as used in many of the examples in the series of Java. The + not only allows concatenating strings but you may also combine other objects as well.

A demo of using the replace method of Strings

The string replace method can be used for replacing the given characters with new characters in the given string.

See this example where the following string is created “The java string is an object.”

By using the replace method, the ‘j’ is replaced by capital ‘J’:

The string replace method code:

public class String_demo {

    public static void main(String []args) {

 

        String Strex = "The java string is an object."; // creating simple

        System.out.println("Actual String = " + Strex);
        System.out.println("After using replace method: " + Strex.replace('j', 'J'));
 }    
}

Output:

Actual String = The java string is an object.
After using replace method: The Java string is an obJect.

An example of using the string split method

The split method of the String class returns an array of broken strings. The split method breaks the string by the given word or regular expression.

For example, you may specify “and” “or” etc. to break the string.

See this example where I have broken the string by specifying the “and” word. As the split method returns an array, the for loop is used to display the returned array elements:

The split method code:

public class String_demo {

    public static void main(String []args) {
        String StrSplit;

        StrSplit ="Java string class has 40 methods and among those is a split method.";
         for (String Strbroken: StrSplit.split(" and ")){

                 System.out.println(Strbroken);
              }
 }    
}

Output:

Java string class has 40 methods
among those is a split method.

A demo of using the format method of String class

For string formatting, you may use the format method of the String class.

In the following example, the format method is used where “%d” formatting character is used that will replace the integer values in the given string:

The code for formatting the string:

public class String_demo {

    public static void main(String []args) {

        int int_method;
        int int_constructors;

        int_method = 40;

        int_constructors= 11;

        String formatted_String;

        formatted_String =String.format("The String class has %d methods and %d constructors" ,int_method,int_constructors);

        System.out.println(formatted_String);

 }  

}

Output:

The String class has 40 methods and 11 constructors
You may also use the printf for formatting the string. The advantage of using the format function is you may reuse the formatted string.

There are other useful methods in the String class like substring, equals, indexof, compare, etc.

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!