1) in How Many Ways You Can Create String Objects in Java?
1) in How Many Ways You Can Create String Objects in Java?
objects in java?
There are two ways to create string objects in
java. One is using new operator and another one
is using string literals. The objects created using
new operator are stored in the heap memory and
objects created using string literals are stored in
string constant pool.
1 String s1 = "sachin";
2 String s2 = " sachin";
Only one object will be created and this object
will be stored in the string constant pool.
7)How do you create mutable string objects?
Using StringBuffer and StringBuilder classes.
These classes provide mutable string objects.
8)Which one will you prefer among “==” and
equals() method to compare two string
objects?
I prefer equals() method because it compares two
string objects based on their content. That
provides more logical comparison of two string
objects. If you use “==” operator, it
checks only references of two objects are equal
or not. It may not be suitable in all situations. So,
rather stick to equals() method to compare two
string objects.
1 String s1 = "abc";
2
3 String s2 = "xyz";
4
5 String s3 = "123";
6
7 String s4 = "A";
And when you create string objects using new
keyword like below, they will be stored in the
heap memory.