JAVA
A Java String contains an immutable sequence of Unicode characters. Unlike C/C++, where string is simply an array of char, A Java String is an object of the class java.lang.
Java String is, however, special. Unlike an ordinary class:
String is associated with string literal in the form of double-quoted texts such as "Hello, world!". You can assign a string literal directly into a String variable, instead of calling the constructor to create a String instance.
The '+' operator is overloaded to concatenate two String operands.
String is immutable. That is, its content cannot be modified once it is created. For example, the method toUpperCase() constructs and returns a new String instead of modifying the its existing content.
How to create String object?
There are two ways to create String object:
By string literal
By new keyword
By String Literal
Java String literal is created by using double quotes. For Example:
String s="welcome";
By new keyword
String s=new String("Welcome");
String literal vs new Keyword
As mentioned, there are two ways to construct a string:
implicit construction by assigning a string literal or explicitly creating
a String
object via the new
operator
and constructor. For example,
String s1 = "Hello"; // String literal
String s2 = "Hello"; // String literal
String s3 = s1; // same reference
String s4 = new String("Hello"); // String object
String s5 = new String("Hello"); // String object
Java has provided a special mechanism for keeping the String
literals
- in a so-called string common pool. If two string literals have the
same contents, they will share the same storage inside the common pool. This
approach is adopted to conserve storage for frequently-used strings. On
the other hand, String
objects created via the new
operator
and constructor are kept in the heap. Each String
object
in the heap has its own storage just like any other object. There is no sharing
of storage in heap even if two String
objects have
the same contents.
You can use the method equals()
of
the String
class to compare the contents of
two String
s. You can use the relational equality
operator '=='
to compare the references (or pointers) of
two objects. Study the following codes:
s1 == s1; // true, same pointer
s1 == s2; // true, s1 and s1 share storage in common pool
s1 == s3; // true, s3 is assigned same pointer as s1
s1.equals(s3); // true, same contents
s1 == s4; // false, different pointers
s1.equals(s4); // true, same contents
s4 == s5; // false, different pointers in heap
s4.equals(s5); // true, same contents
Important Notes:
In the above example, I used relational equality
operator '=='
to compare the references of two String
objects.
This is done to demonstrate the differences between string literals sharing
storage in the common pool and String
objects
created in the heap. It
is a logical error to use (str1 ==
str2)
in
your program to compare the contents of two String
s.
String
can be created by directly
assigning a String
literal which is shared in a common
pool. It is uncommon and not recommended to use the new
operator
to construct a String
object in the heap.
Java String class methods
The java.lang.String class provides many useful methods to perform operations on sequence of char values.