Saturday 15 February 2014

java - Not initializing String vs. Making null String creating another object later? -


If I am creating a string and not initializing the value I am using the same string object Or is it reassigning the string reference to the new string, which might want to change the value.

  class Foo () {String x; String y = null; X = "Hello"; Y = "hey"; }  

My understanding is now that X would not be making an additional string object. But y would be creating another string, but it will be referenced in the context of the newly created string "O" with tap string. Is it true?

  string foo  

itself is just a reference Since it is a string-reference, it is capable of referring to a string called 'string pool', whose implementation it however creates a new string even though not .
n

  string bar = null;  

The string bar, such as Foo, is a string reference , but no new object is formed by creating a reference foo and The difference between bar is assigned to that time for zero , but the tap is definitely not a string and therefore there is no reason to create JVM A new string -
Your last impression really comes closer to what happens.

  string foo = "hello world";  

Creates a new string (assuming "Hello World" is not found in the string pool yet)
You can divide this statement into three small details: < / P>

  1. Create reference
    string foo creates a new reference, capable of referencing string objects
  2. Create an object
    "Hello World" , assign new spaces to the "new string" (string bridge in this case) And actually creates a new object.
  3. Assignment
    = string x; string y = null;

    will create a new string but both of them The statements will be

      x = "hello"; y = "hey";  

No comments:

Post a Comment