• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String object!

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi:
All,there is a question :
1.How many String objects are created when the following class is run?
Code ��
 public class TestClass {
public static void main(String args[])  {
String hello = "Hello"; //1
 String world = new String("World!");//2
   String helloworld = hello +" "+world;//3
   String alias = helloworld ; //4
   System.out.println(alias); //5
 }
}
I think there is only 5 object!
//1 has one in literal pool "Hello",
//2 has two ,one is in programm space,other is in literal pool,
//3 "" is one , the last is "Hello World!" as helloworld reference ! So only 5 ! But tha answer is 6 !
Who can tell me Why ? Thanks,very!!
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tsui:I think
//1 has one in literal pool "Hello",
//2 has two ,one is in programm space,other is in literal pool,
//3 "" is one , hello + "" is two, the object created by hello + "" plus world is three(three objects altogether)
Hope this help
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think there are five:
//1 "Hello"
//2 world and "World!"
//3 helloworld and ""
please correct if wrong
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jose,
Lu has already explained how we get 6 strings out of the example. In case you missed it here it is again:
Literal Pool (compiler filled):
"Hello" -- from //1
"World!" -- from //2
" " -- from //3
Runtime creations:
"World!" -- from //2
"hello " -- from //3
"hello world" -- from //3
For a total of 6 string objects.
Regards,
Manfred.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Manfred thanks for aswering
I have run javap and I still see only 5 strings:


0 ldc #2 (String "Hello")
2 astore_1
3 new #3 (Class java.lang.String)
6 dup
7 ldc #4 (String "World!")
9 invokespecial #5 (Method java.lang.String(java.lang.String))
12 astore_2
13 new #6 (Class java.lang.StringBuffer)
16 dup
17 invokespecial #7 (Method java.lang.StringBuffer())
20 aload_1
21 invokevirtual #8 (Method java.lang.StringBuffer append(java.lang.String))
24 ldc #9 (String " ")
26 invokevirtual #8 (Method java.lang.StringBuffer append(java.lang.String))
29 aload_2
30 invokevirtual #8 (Method java.lang.StringBuffer append(java.lang.String))
33 invokevirtual #10 (Method java.lang.String toString())
36 astore_3
37 aload_3
38 astore 4
40 getstatic #11 (Field java.io.PrintStream out)
43 aload 4
45 invokevirtual #12 <Method void println(java.lang.String)>
48 return


Lines 0, 7 and 24 shows three String literals.
Lines 3 and 33 shows two strings more.
There is no "Hello " because all the additions to the StringBuffer are performed via the append method, and only after the last addition the content of the StringBuffer is returned as aString (line 33)
Please correct if wrong.
 
Ranch Hand
Posts: 782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What is javap??
Bye
Viki.

------------------
Count the flowers of ur garden,NOT the leafs which falls away!
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
javap is a disasambler converting from bytecodes to JVM instructions.
It is in the same directory as javac and the instructions are in the SDK Docs
Enjoy
 
juan Tsui
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks All ! Thanks for you!
I think the answer perhaps has some problem ! I am going to find it out !!
Thanks !
juantsui
 
lu v thuan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jose:
In the bytecode you got five String objects because of String
Optimization(An implementation may choose to perform conversion and concatenation in one step to avoid creating and concatenation in one step to avoid creating and then discarding an intermediate String objectsJLS 15.18.1.2)
you are correct
Thank Jose
 
It is an experimental device that will make my mind that most powerful force on earth! More powerful than this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic