The Strings in Java

The Strings are the combination of characters that are widely used in the Java programming. For example, “Strings are supported in Java programming” is a simple Java string.

An example of string

Java comes up with the String class to work with strings. That means the strings are treated as objects. A string object once created cannot be changed i.e. the string object is immutable.

A string replace method example

A few main points about the Strings:

  • The String is an object with the sequence of characters.
  • The class String has forty methods.
  • The String class has 11 constructors.
  • String objects are immutable; once created it cannot be changed.
  • The methods provided by Java String class are very useful in performing the strings actions like string comparison, searching substring, changing the case of letters, concatenation/joining strings, splitting the string etc.

A few string methods are explained below with examples, let us first look at creating the simple strings.

Creating a simple Java string

A string can be created as follows:

String strex = “Java comes up with string class”;

This is the simple way of creating a string. In that case, as string literal is executed in the compiler, it will create a String object.

Create string by new keyword

You can also create the strings just like other objects in Java. See below:

String strex = new String();

strex = “Java comes up with string class”;

That is, using the new keyword to create a String Java class instance.

An example of creating and displaying Strings

The example below creates two strings, one with the simple declaration and other uses the new keyword to create a string class instance.

Finally, we will display both strings. Click the image or link below to see code and output side by side:

Experience this online

The output will be:

Strings are supported in java by using String class

Java comes up with string class

The String Class methods

There are around 40 utility methods in the String class like string comparison, string length, splitting string etc. A few commonly used method are given below along with examples.

The list of more methods can be found at the bottom of this chapter along with the method links.

String length method

The syntax of the string length method is:

Strex.length()

The length() method of the Java string returns the length or the total number of characters in a string.

Example of using string length method

This example displays length of two strings by using length method of string.

Experience this online

The output will be:

String length=51

String length=31

The String replace method

The syntax of the string replace method is:

Strex.replace()

The replace() method of string replaces the existing characters to the new given characters.

Example of using string replace method

The following example replaces small ‘c’ letter in the existing string to ‘C’.

Experience this online

The output will be:

The actual string: Java comes up with string class

String after replace: Java Comes up with string Class

Commonly used string methods of Java

A few commonly used string class methods with links to their respective chapters are:

  • String format
  • String split method
  • String replace method
  • String substring method
  • The String Equals
  • String compare method
  • Java valueOf and toString for int to string conversion
  • parseInt and valueOf methods