Hi Armando,
With the following line you are not creating a new
String object in the heap but reusing a string literal .
String str = "one";
This will search in the string pool whether there is already object exist with the value one or not.. If it find the value then jvm simly assigns the reference but if jvm dont find it will create new object in string pool. There is a concept of string pool which you need to refer.
While if you are creating string object with
String str = new String("one") , it will create a new object for str dosent matter if the string pool contains the value one.
So you need to read the concept of string pool first to understand this. And for integer you need to create object with Integer before
java 5 Integer i = 5 used to give error but now its working as autoboxing is there.
Please try to get idea from this example:
Try to do same for the String as well.
Regards
Jatan