Wednesday, 16 March 2016

Interview Question


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
 String class are immutable.
 String Buffer class are mutable.
Object of String class are created using both new operator and string literal.
Object of  String Buffer class is created using only new operator .
In String class equals() and hash code method are override. 
 In String Buffer  equals() and hash code method are not override.
In Case of String consume more memory.
 In case of String Buffer consume less memory.
In case of String object are stored String constant pool as well as heap memory.
 In case of String Buffer object are stored only in heap memory

No comments:

Post a Comment

No of Occurrence in Array

package com.tutorial4u; import java.util.HashMap; /************************************************************  No of Occurrence in Array...