In this blog, we will discuss what is string in java and different types of String methods.
A String is an unavoidable type of variable and we use wide often while we write our application program.
Generally, String is a sequence of characters. But in Java, string is an object that represents a sequence of characters. The java. lang. String class is used to create a string object.
An Array of characters works the same as Java String. For Example:
is the same as:
Note: Strings in java are not primitive data type (like int, char, double, etc). Instead, all Strings are objects of a pre-defined class name called "String". And all String variables are instances of the String class.
We Can Create String Objects in two ways:
1. By String Literal
Java String literal is created by using double (" " )quotes. For example:
String S = "Welcome";
Each time when you create String Literal, the JVM will check the "String Constant Pool" first to check if the string already exists in the pool. If it exists the reference of the instance will be returned. If String doesn't exist, a new string instance will be created in the String pool. For example:
String S1 = "Welcome";
String S2 = "Welcome";
In the above example, a new instance is not created because the string is already present in the pool.
In the above picture, only one object "welcome" is created. At First, when JVM looks for the object name "welcome" it has not existed so it has created a new object in the pool. After it finds the String with the value "welcome" in the pool, so it will not create a new object rather it will return only the reference of the same instance.
All the String objects are stored in the special Heap memory are known as "String Constant Pool".
Java uses the String Literal concept to make it more memory efficient.
2. By new Keyword:
String s = new String("Welcome")
In the above example, JVM will create a new string object in heap memory, and the literal "Welcome" will be placed in the pool. The variable 's' will refer to the object in a pool.
Code Snippet of String Methods:
Different Java String Class Methods:
The String class has some built-in methods to help us perform operations on Strings and data manipulations. We don't have to import any libraries to use string class, because it is part of java.lang package which is available by default.
It returns the length of the string
It returns character value for a particular index
It appends a string to the end of another string
It checks whether a string contains a sequence of character or not
It compares two strings and ignores case-sensitive
Compares two strings, returns a true if strings are equal and false if strings are not equal.
Searches a string for a specified value, and replaces it with a new value of either single character or word
It removes whitespaces from both ends of a string
It returns the index of first occurrence of a character in string
This method extracts a substring from string
Converts a string to lowercase letters
Converts a string to uppercase letters
To check if string is Empty
There are over 60 String Methods in java and we have covered some important methods that are used in program frequently.
In this blog, we have learned about String in Java, different ways of creating String Objects, and String Methods with some examples.