There are two ways to create String object.
String str = new String("Java");
String str2 = "Java";
I know in first case constructor will definitely be invoked. But not aware about second one. Will constructor be invoked?
String substr = new String(str.substring(2,str.length)); // str is new object
String substr2 = new String(str2.substring(2,str2.length)); //str2 is not with new keyword
want to make sure substr
and substr2
are same kind of operation and same behaviour in the heap memory.
I know one thing that String.substring()
doesn't create new object at all but uses previous char[] object with different offset.
so what happen with substr
and substr2
? Can we relate these things with constructor as offset is generated inside constructor.