Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Query with String statement

 
PavanPL KalyanK
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String s = new String("abc"); // creates two objects

// Asite it says this statement creates two Objects??

But i think its only one Object created. Am i right or wrong ??


Please indicate.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11854
185
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Define "created"

There are 2 string objects in that line of code:
  • The original "abc" that is hardcoded and can be said to be implicitly created in this line.
  • The new String that is explicitly created in this line


  • - Andrew
     
    PavanPL KalyanK
    Ranch Hand
    Posts: 212
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    I think you are trying to create a Joke ?Am i right ?
     
    Henry Wong
    author
    Marshal
    Pie
    Posts: 20892
    75
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    PavanPL KalyanK wrote:
    I think you are trying to create a Joke ?Am i right ?


    How is the answer a joke???

    Henry
     
    PavanPL KalyanK
    Ranch Hand
    Posts: 212
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Is that really answer ??

    so i guess onr Object will be created on to heap and another on Constant pool .

    Am i right ?
     
    Henry Wong
    author
    Marshal
    Pie
    Posts: 20892
    75
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Technically, there are no objects in the String Constant Pool -- it's just references.

    Henry
     
    PavanPL KalyanK
    Ranch Hand
    Posts: 212
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hey Henry ,

    so where are the two Objects created?
     
    Andrew Monkhouse
    author and jackaroo
    Marshal Commander
    Pie
    Posts: 11854
    185
    C++ Firefox Browser IntelliJ IDE Java Mac Oracle
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Perhaps it might help if you break out the code into two lines:

    Then it is easier to see that there is one string created on line 2, and another on line 3.

    Or, if we compile that method, and then look at the resultant bytecode in the created class file:

    We can see here that the hard coded String "abc" is stored in location 1 (astore_1) at bytecode line 2 (physical line 4 in the above code block), and then a new String is created based on the contents of store location 1 then stored in location 2 (astore_2) at bytecode line 11 (physical line 9 in the above code block).

    So there are 2 Strings: one in memory location 1 and one in memory location 2.

    Does this help?

    Andrew
     
    PavanPL KalyanK
    Ranch Hand
    Posts: 212
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Andrew , thanks for the time you have taken to explain me the things .

    But you know i still got more confused with youe new code
     
    Andrew Monkhouse
    author and jackaroo
    Marshal Commander
    Pie
    Posts: 11854
    185
    C++ Firefox Browser IntelliJ IDE Java Mac Oracle
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sorry, that wasn't intentional.

    Did you understand the first part, where I had the very simple Java method:

    Which shows two strings - the first being on line 2 and the second being on line 3?

    Regards, Andrew
     
    Andrew Monkhouse
    author and jackaroo
    Marshal Commander
    Pie
    Posts: 11854
    185
    C++ Firefox Browser IntelliJ IDE Java Mac Oracle
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I'm about to go offline for a few days, so I'll quickly follow up on the bytecode, which I kind of dived right in without any explanation. If you don't understand this or care to understand this, then that's ok. If it makes the whole thing too confusing then just skip the bytecode explanations.

    Lets start with a simple class:

    Compile it:

    Then look at what the HelloWorld.class file actually contains:

    Where the command I entered to look at the contents of the class file was "javap -p <class name>"

    You can see in that code that there is a default constructor (the first block of code) and then the method I created (public static void main).

    In the main method:
  • At bytecode line 0 we get the reference to the static PrintStream that is stored in System.out
  • At bytecode line 3 we load from the runtime constant pool (ldc) the String "Hello, world"
  • At bytecode line 5 we invoke the println() method to print the string
  • At bytecode line 8 we do return from the "main" method (even though I had not put an explicit "return" statement in my code, Java puts one in for me


  • What I was trying to show with the bytecode from the compilation of my "someMethod()" class was that there are 2 Strings involved.

    Regards, Andrew
     
    PavanPL KalyanK
    Ranch Hand
    Posts: 212
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you very much Andrew .

    You really made a difference in understanding the concept.

    (It took time as i did pratical as this requires some time rather than scan the article)

    Thanks once again.
     
    Wee Keong Soh
    Greenhorn
    Posts: 18
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    HI,

    How do you view the bytecodes?

    Thanks
     
    PavanPL KalyanK
    Ranch Hand
    Posts: 212
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I ran this from command prompt .

    compile the program and use javap -c classname
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic