String is an object that represents sequence of characters. In Java, String is represented by String class which is located into java.lang
package
It is probably the most commonly used class in java library. In java, every string that we create is actually an object of type String. One important thing to notice about string object is that string objects are immutable that means once a string object is created it cannot be changed.
The Java String class implements Serializable, Comparable and CharSequence interface that we have represented using the below image.
In Java, CharSequence Interface is used for representing a sequence of characters. CharSequence interface is implemented by String, StringBuffer and StringBuilder classes. This three classes can be used for creating strings in java.
What is an Immutable object?
An object whose state cannot be changed after it is created is known as an Immutable object. String, Integer, Byte, Short, Float, Double and all other wrapper classes objects are immutable.
Creating a String object
String can be created in number of ways, here are a few ways of creating string object.
1) Using a String literal
String literal is a simple string enclosed in double quotes " "
. A string literal is treated as a String object.
Hello Java
2) Using new Keyword
We can create a new string object by using new operator that allocates memory for the object.
Output:Hello Java
Each time we create a String literal, the JVM checks the string pool first. If the string literal already exists in the pool, a reference to the pool instance is returned. If string does not exist in the pool, a new string object is created, and is placed in the pool. String objects are stored in a special memory area known as string constant pool inside the heap memory.
String object and How they are stored
When we create a new string object using string literal, that string literal is added to the string pool, if it is not present there already.
And, when we create another object with same string, then a reference of the string literal already present in string pool is returned.
But if we change the new string, its reference gets modified.
Concatenating String
There are 2 methods to concatenate two or more string.
- Using concat() method
- Using
+
operator
1) Using concat() method
Concat()
method is used to add two or more string into a single string object. It is string class method and returns a string object.
HelloJava
2) Using + operator
Java uses "+"
operator to concatenate two string objects into single one. It can also concatenate numeric value with string object. See the below example.
HelloJava
String Comparison
To compare string objects, Java provides methods and operators both. So we can compare string in following three ways.
- Using
equals()
method - Using
==
operator - By
CompareTo()
method
Using equals() method
equals()
method compares two strings for equality. Its general syntax is,
Example
It compares the content of the strings. It will return true if string matches, else returns false.
Output:truefalse
Using == operator
The double equal (==)
operator compares two object references to check whether they refer to same instance. This also, will return true on successful match else returns false.
true false
Explanation
We are creating a new object using new operator, and thus it gets created in a non-pool memory area of the heap. s1 is pointing to the String in string pool while s3 is pointing to the String in heap and hence, when we compare s1 and s3, the answer is false.
The following image will explain it more clearly.
By compareTo()
method
String compareTo()
method compares values and returns an integer value which tells if the string compared is less than, equal to or greater than the other string. It compares the String based on natural ordering i.e alphabetically. Its general syntax is.
Syntax:
Example:
Output:-21
0 Comments