Brandon Tom, SCJP, MCP<br />Programmer/Graphic Designer<br /><a href="http://mailto:[email protected]" target="_blank" rel="nofollow">[email protected]</a>
Brandon Tom, SCJP, MCP<br />Programmer/Graphic Designer<br /><a href="http://mailto:[email protected]" target="_blank" rel="nofollow">[email protected]</a>
Originally posted by Brandon Tom:
Strings are immutable. They can't be changed, so when toUpperCase () is executed, a new objected is created. After that s1 points to "ABC" and not "abc".
[ January 26, 2006: Message edited by: Brandon Tom ]
Brandon Tom, SCJP, MCP<br />Programmer/Graphic Designer<br /><a href="http://mailto:[email protected]" target="_blank" rel="nofollow">[email protected]</a>
Originally posted by Brandon Tom:
Ah, indeed you are correct.
hmmm.. but, the question does ask how many String objects are created.. does it count the String returned by s1.toUpperCase ()?
[ January 26, 2006: Message edited by: Brandon Tom ]
Each string literal is a reference to an instance of class String
For example:
String str = "abc";
is equivalent to:
char data[] = {'a', 'b', 'c'};
String str = new String(data);
Abid Sami<br />SCJP,SCWCD
Abid Sami<br />SCJP,SCWCD
Abid Sami<br />SCJP,SCWCD
In such a case, references to the two String literals are still put into the constant table (the String Literal Pool), but, when you come to the keyword "new," the JVM is obliged to create a new String object at run-time (...)
Parallax - Change in observational position that provides a new line of sight.
Parallax - Change in observational position that provides a new line of sight.
This will create a new object obj 2, There won't be a "String Literal object") (
[You can also construct a String by explicitly calling the constructor� however, doing so causes extra memory allocation...At runtime, the new String() statement is executed and a fresh instance of String is constructed, duplicating the String in the literal pool.
�because we used the new keyword, Java will create a new String object in normal (nonpool) memory�In addition, the literal� will be placed in the pool.
Parallax - Change in observational position that provides a new line of sight.
Abid Sami<br />SCJP,SCWCD
Boldface two written by me. Taken from Fundamentals of the Java Programming Language, Sun Mycrosystem 2005. SL-110, Student Guide, Page 5-20."Creating a String object using the new keyword creates two String Objects in memory and puts the character representation of the String literal in an area of heap memory reserved for literals called the literal pool."
Originally posted by Enrique Villamizar:
[QB]What about using a tool for simulating objects in memory. I know there is one called ObjectTool but I don't have it. Maybe JProfiler.
I also found this: Boldface two written by me. Taken from Fundamentals of the Java Programming Language, Sun Mycrosystem 2005. SL-110, Student Guide, Page 5-20.
QB]
Abid Sami<br />SCJP,SCWCD
Forget Steve. Look at this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|