Q : In how many ways you can create string 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
2
| String s1 = new String( "ashish" ); //Creating string object using new operator String s2 = "ashish" ; //Creating string object using string literal |
In case of new operator two object is created one is in heap and other one is in string constant Pool and object is pointing to heap object.
Q: What is difference between String and String Buffer ?
String
|
String Buffer
|
In String class equals() and hash code method are override.
|
|
In Case of String consume more memory.
|
|
In case of String object are stored String constant pool as well as heap memory.
|
No comments:
Post a Comment