String in Java – Everything You Need to Know

What is String in Java?

In Java, string is an object and a sequence of characters. A sequence of char value is represented by it. Java string works the same way as an array of characters. There are several methods provided by the Java string to carry out operations. Some of them are compare(), substring(), concat(), intern(), equals(), compareTo(), split(), replace(), and length().

There are three interfaces implemented by the java.lang.String class. They are Serializable, Comparable, and CharSequence interfaces.

CharSequence Interface

String, StringBuffer, and StringBuilder classes implement the CharSequence interface in order to represent the sequence of characters. This means that these three classes are used in Java to create strings.

The Java String class is unchangeable. In other words, it is immutable. A new instance gets created each time a string is changed. The StringBuffer and StringBuilder classes are used for mutable strings.

The immutable string will be dealt with later. Let’s first understand the concept of string and the way to create a string in Java.

Methods to create a string object

There are two methods that are used to create a string object and they are:

  1. By string literal
  2. By new keyword
  3. String Literal

A string literal is a sequence of characters that are created by using double quotation marks.

Why is the concept of string literal used in Java?

String literal is used in Java to save up more memory as new objects are not created if it already exists in the string constant pool.

  1. New Keyword

In this method, a new string object is created by the JVM in heap memory. The object in the heap memory will be referred by the variables.

The various methods supported by Java string to perform several operations on a sequence of characters are:

  1. char chartA(int index):

This method returns the character value for the particular index.

  1. int length():

This method returns the length of the string.

  1. static String format(String format, Object…args):

This method returns the string in a particular format.

  1. static String format(Locale I, Strict format, Object…args):

This method returns the string in a particular format along with a given locale.

  1. static substring(int beginIndex):

This method returns a substring of the given beginning index.

  1. string substring(int begin Index, int endIndex):

This method returns a substring of the given beginning and ending index.

  1. boolean contains(CharSequence s):

This method returns whether the Boolean statement is true or false after matching the sequence of the character value.

  1. static String join(CharSequence delimiter, CharSequence…elements):

This method returns a joint string.

  1. static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements):

This method also returns a joint string.

  1. boolean equals(Object another):

This method is used to check the equality between the string and a given object.

  1. boolean isEmpty():

This method is used to check whether the string is empty or not.

  1. String concat(String str):

This method is used to link specified strings.

  1. String replace(char old, char new):

This method is used to replace all occurrences of the particular character value.

  1. String replace(CharSequence old, CharSequence new):

This method is used to replace all occurrences of the particular character sequence.

  1. static String equalsIgnoreCase(String another):

This method is used to make a comparison between two strings.

  1. String[] split(String regex):

This method returns a split string that matches the regex.

  1. String[] split(String regex, int limit):

This method returns a split string that matches the regex and the limit as well.

  1. String intern():

This method returns an interned string.

  1. Int indexOf(int ch):

This method returns the particular character value index.

  1. int indexOf(int ch, int fromIndex):

This method returns the particular character value index beginning with the given index.

  1. int indexOf(String substring):

This method returns the particular substring index.

  1. int indexOf(String substring, int fromIndex):

This method returns the particular substring index beginning with the given index.

  1. String toLowerCase():

This method returns the string in a lowercase.

  1. String toLowerCase(Locale l):

This method returns the string in lowercase using a particular locale.

  1. String toUpperCase():

This method returns the string in uppercase.

  1. String toUpperCase(Locale l):

This method returns the string in uppercase using a particular locale.

  1. String trim():

This method removes the starting and ending spaces of the string.

  1. static String valueOf(int value):

This method is an overloaded method that converts given type into a string.

Immutable string in Java

The string objects are immutable in Java. In other words, it cannot be modified or be changed. The data and state of the string object cannot be changed once it is created; instead, a new string object is created.

Why are string objects immutable in Java?

String objects are immutable in Java because of the use of the string literal. When one reference variable gets changed, then it will affect all other reference variables that refer to the same object as well. So in order to avoid this, string objects are immutable in Java.

Java String Comparison

A string in Java can be compared on the basis of its content and reference. The string comparison is used in authentication, reference matching, and sorting through the use of equals() method, ==operator, and compareTo() method respectively.

The three methods used to compare string in Java are:

  1. By equals() method
  2. By == operator
  3. By compareTo() method

1. String comparison through equals() method

The string equals() method compares the value of the string for equality. The original content of the string is compared through this method. There are further two types of equals() method and they are:

  • public boolean equals(Object another): This method compares the string to the specified object.
  • public boolean equalsIgnoreCase(String another): This method compares one string to another string and ignores the case.

2. String comparison through == operator

The == operator does not compare the value of the string rather it compares the references.

3. String comparison through compareTo() method

This method compares the values of the string lexicographically. It then returns an integer value that reports whether the string is greater than, equal to or less than the second string.

String concatenation in Java

String concatenation is the process of creating a new string by combining various different strings. The two methods used to combine strings to make a new string are:

  1. By + (string concatenation) operator
  2. By concat() method

1.  String concatenation by + (string concatenation) operator

The string concatenation operator (+) is used in Java to combine strings. After combing the strings, the StringBuilder or StringBuffer classes are used to implement string concatenation. Here, a new string is created by appending the second operand onto the end of the first operand. The operator not only combines strings but also the primitive values.

2. String concatenation by concat() method

This method creates a new string by concatenating a particular string to the end of the current string.

Substring

Substring is a small part of a string. This means a subset of a string is called a substring. Here the startIndex is inclusive whereas the endIndex is exclusive.

A substring can be obtained from a string by the following two ways:

  1. public String substring(int startIndex):

This method returns a new string with the substring of a particular string from a specific startIndex.

  1. public String substring(int startIndex, int endIndex):

This method returns a new string with the substring of a particular string from a specific startIndex to endIndex.

Java string class methods

The java.lang.String class contains various methods that can be used to perform several operations on a string. Some of the functions of such methods include trimming, concatenating, comparing, converting, and replacing strings. The string is an important concept in Java as everything is treated as a string.

Some of the most used methods of string class are:

Java String toUpperCase() and toLowerCase() method:

In this method, the toUpperCase() method converts the string into the uppercase letter and the toLowerCase() method converts it into the lowercase letter.

Java String trim() method:

This method removes white spaces at the beginning and at the end of the string.

Java String startsWith() and endsWith() method

Java String charAt() method:

This method returns a character value at a particular index.

Java String length() method:

This method returns the length of the specified string.

Java String intern() method:

This method returns the string from the pool in the case in which the pool has a string equal to the string object as resolved by the equals() method. The reference to the string object is returned after the string object is added in the opposite case.

Java String valueOf() method:

This method converts int, long, double, float, boolean, char array, and char into a string.

Java String replace() method:

This method replaces every occurrence of a sequence of characters with another sequence of characters.

Leave a Reply

Your email address will not be published. Required fields are marked *