GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Query with String statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Query with String statement" Watch "Query with String statement" New topic
Author

Query with String statement

PavanPL KalyanK
Ranch Hand

Joined: Feb 28, 2009
Posts: 212
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

Joined: Mar 28, 2003
Posts: 11465
    
  94

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


    The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
    PavanPL KalyanK
    Ranch Hand

    Joined: Feb 28, 2009
    Posts: 212

    I think you are trying to create a Joke ?Am i right ?
    Henry Wong
    author
    Sheriff

    Joined: Sep 28, 2004
    Posts: 18874
        
      40

    PavanPL KalyanK wrote:
    I think you are trying to create a Joke ?Am i right ?


    How is the answer a joke???

    Henry


    Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
    PavanPL KalyanK
    Ranch Hand

    Joined: Feb 28, 2009
    Posts: 212
    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
    Sheriff

    Joined: Sep 28, 2004
    Posts: 18874
        
      40


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

    Henry
    PavanPL KalyanK
    Ranch Hand

    Joined: Feb 28, 2009
    Posts: 212
    hey Henry ,

    so where are the two Objects created?
    Andrew Monkhouse
    author and jackaroo
    Marshal Commander

    Joined: Mar 28, 2003
    Posts: 11465
        
      94

    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

    Joined: Feb 28, 2009
    Posts: 212
    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

    Joined: Mar 28, 2003
    Posts: 11465
        
      94

    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

    Joined: Mar 28, 2003
    Posts: 11465
        
      94

    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

    Joined: Feb 28, 2009
    Posts: 212
    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

    Joined: Apr 08, 2009
    Posts: 18
    HI,

    How do you view the bytecodes?

    Thanks
    PavanPL KalyanK
    Ranch Hand

    Joined: Feb 28, 2009
    Posts: 212
    I ran this from command prompt .

    compile the program and use javap -c classname
     
    GeeCON Prague 2014
     
    subject: Query with String statement