String in Java

String in Java is one of the most important topics of java. Java string can be created in several ways. In this tutorial, we will see how to create a string. Here we will see substring in java and how to get substring java.

Here is the table content of the article will we will cover this topic.
1. What is the String?
2. String class in java
3. What is immutable String in java
4. Why string is immutable in java?
5. What is a mutable string?
6. String concatenation

7. String comparison in Java
8. Substring in Java
9. String constructor in java
10. Convert string to int
11. string methods in java
12. StringBuilder in java

13. StringBuffer in java

What is String in Java?

A String is a sequence of characters or groups of characters. E.g. “Hello” is a string containing 5 characters that are a sequence of characters ‘h’, ‘e’, ‘l’, ‘l’, and ‘o‘. Basically, a string is an array of characters, and each character stored in a specific index.
String in java are objects, but internally it is an array of characters. Whenever we create a String, the JVM automatically creates the Array of characters.

A character always is written in a single quote. Whatever we write in the quote is considered as a character. In the below example, we are declaring char with a single quote. for example:

char ch = 'a';
char ch1 = '1';
char ch2 = '\'

But a String can contain many characters. A String should be enclosed in double quotes.

String s = "abc";
String s1 = "123"
Strig s2 = "//"

There are many ways to create a String. Java provides some classes to create a String. Hence, We will discuss it one by one in a separate post. Some classes used to create immutable objects and some for mutable.

1. String class: In java, String is a class that utilizes to create string objects. An object created by String is immutable. By mean of immutable if you change anything string the JVM create new String. So, Please read it in detail.

2. StringBuffer class: We can create a string by use of the StringBuilder class. The StringBuilder class places in java.lang package.
i). StringBuilder creates a mutable string. It means you can modify the String.
ii). StringBuilder represents the growable character sequence.
iii). StringBuilder class allowed to insert the character/string in the middle of the string.

3. StringBuilder class:

We can create a string by use of the StringBuffer class. The StringBuffer class is placed in java.lang package.
i) StringBuffer creates a

mutable string. It means you can modify the String.
ii) StringBuffer represents the growable character sequence.
iii) StringBuffer class allowed to insert the character/string in the middle of the string.

4. StringTokenizer class: In Java, StringTokenizer class employs to break a string into tokens.
A token is returned by taking a substring of the string that was used to create the StringTokenizer object.

5. StringJoiner class: StringJoiner class apply to construct a sequence of characters(strings) separated by a delimiter. Hence, We can provide an optionally starting with a supplied prefix and ending with a supplied suffix.

All these classes are implementing some common interfaces:

  • Serializable
  • Comparable
  • CharSequence
  • Serializable: The serializability of a class is enabled by the class implementing the Serializable interface. If any Class that does not implement this interface will not have any of their state serialized or deserialized. Another, All subtypes of a serializable class are themselves serializable. The serialization interface doesn’t contain any variables or methods. So, The compiler identifies the semantics of being serializable.
  • Comparable: This interface is implemented by many classes. It uses to compare the objects of the class. By use of this, we can perform sorting. Also, It has only one method.
  • CharSequence: The CharSequence interface practices to represent the sequence of characters. It has many methods that are used by the implemented class.
string in java

In the next post, we will see how to create a String and how many ways to Create a string. Therefore several things need to learn about the String, like string storage, how the String is immutable, memory representation, java substring, etc.

Leave a Comment