String a = "Hello";
String b = new String("Hello World");
Can someone please tell me how many objects are created and elaborate.
Thank you.
String a = "Hello";
String b = new String("Hello World");
Can someone please tell me how many objects are created and elaborate.
Thank you.
String greeting = "Hello world!";
In this case, "Hello world!" is a string literal—a series of characters in your code that is enclosed in double quotes. Whenever it encounters a string literal in your code, the compiler creates aString
object with its value—in this case, Hello world!.
String a = "Hello"; // 1 object
String b = new String("Hello World");
// 1 object with new String(),
// 1 object with "Hello World"
in total you created 3 objecs.