String Constant Pool

1

I'm pretty sure you understand exactly how the String constant pool concept works.

If we create an object with new , will an object be created in the heap and a reference in the pool but not an object?

If we create an object with "" , is an object created in the pool (not in the heap) but not a reference?

    
asked by silvia Dominguez 28.07.2017 в 10:06
source

1 answer

0

To my This link I clarified a lot. But it is in English. (although just looking at the diagrams is quite explanatory.

With new it creates an Object, and from the variable it refers to the created object.

Sring s = new String("Hola");

On the other hand only with quotes is a primitive type (it's like int or Integer and Boolean or Boolean)

String s = "hola";
String s2 = "hola2";

s and s2 if you do equals or "==" are equal because their content is the same. If on the contrary you use the String object.

String s = new String("hola")
String s2 = new String("hola")

Both objects will not be equal, because the comparison is between objects, and each object will have its id (I think with .equals () if I would recognize you as equals by the comparator you have, but with "==" no)

    
answered by 28.07.2017 в 10:22