Friday, 31 August 2018

Very famous question of String class in java is, When create a String using new String(""); How many objects will be created on the same time?


Ans : For exp.


class Test{
public static void main(String[] args) {//1
String s = new String("pawan");//2
}
}
Here first of all "pawan" will be created as string first if pawan is not string pool only then,
and after that 's' will be created in heap and it points to created object in string pool for "pawan"

So finally at line 2 two objects of String will be created on the same time.

No comments:

Post a Comment