• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is Constructor is invoked during object creation  RSS feed

 
Ranch Hand
Posts: 32
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

String objects can be created as literal and also with the new operator. I want to know whether the constructor of the String class is invoked during creation of a String object by literal.

Suppose we write

String s = "abc";

Is the constructor of the String class is invoked?
 
Greenhorn
Posts: 25
Java Oracle PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For reducing the memory usage, java uses something called "String Constant Pool" which is a part of JVM memory. It is a pool of currently using Strings of JVM. If you use,

Then JVM first check whether there is any String equals to "this is a test" exists in the String Constants pool.

*) If there is a string identical to "this is a test" exists in the String Constant pool, JVM get the reference to that String and assign that reference value to "s" reference variable without creating a new String object. So in this case, there is no call to constructor happens.

*) If there is no such string with the value "this is a test" exists in the string constant pool, JVM will create a new String object with the value "this is a test", then add the newly created String object to String constant pool, and assign the reference to newly created string object to "s" variable. Therefore in this occasion, Constructor is called by the JVM to create new String object.

 
kumarjit banerjee
Ranch Hand
Posts: 32
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manjula Weerasinghe wrote:For reducing the memory usage, java uses something called "String Constant Pool" which is a part of JVM memory. It is a pool of currently using Strings of JVM. If you use,

Then JVM first check whether there is any String equals to "this is a test" exists in the String Constants pool.

*) If there is a string identical to "this is a test" exists in the String Constant pool, JVM get the reference to that String and assign that reference value to "s" reference variable without creating a new String object. So in this case, there is no call to constructor happens.

*) If there is no such string with the value "this is a test" exists in the string constant pool, JVM will create a new String object with the value "this is a test", then add the newly created String object to String constant pool, and assign the reference to newly created string object to "s" variable. Therefore in this occasion, Constructor is called by the JVM to create new String object.





Thanks Manjula.

But I still have one doubt. When I run a program containing the main method, then one instance of the jvm is present. Correct me if I am wrong. So when the program exits the String will be present in the String pool or it will be collected by the garbage collector.
If I again run the same program will the same reference of the previous String will be returned.

If the above statement is false then when the class is loaded then the string pool of the jvm is empty.
Please clarify my doubt and if I am wrong.

Say for example:




In this case when line 1 is executed then constructor of String class is invoked. For line 2 same reference is returned.
For the next time I execute the class, will there be a second constructor call.
 
Manjula Weerasinghe
Greenhorn
Posts: 25
Java Oracle PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kumarjit,
Please read this article http://www.javaranch.com/journal/200409/ScjpTipLine-StringsLiterally.html. It has more information about String literal pool (String constant pool) and garbage collection with some descriptive images.

Each time you execute "java" command a new JVM instance is created for executing relevant class main method. Therefore you will get a new string literal pool for that JVM and will not be able to get the same reference. Since each JVM instance is a separate process and because one process cannot access the memory use by another process, you will get one String literal pool for each JVM independent from other instances.

Thanks & Regards,
Manjula
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Too difficult a question for "beginning". Moving thread.
 
kumarjit banerjee
Ranch Hand
Posts: 32
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manjula Weerasinghe wrote:Hi Kumarjit,
Please read this article http://www.javaranch.com/journal/200409/ScjpTipLine-StringsLiterally.html. It has more information about String literal pool (String constant pool) and garbage collection with some descriptive images.

Each time you execute "java" command a new JVM instance is created for executing relevant class main method. Therefore you will get a new string literal pool for that JVM and will not be able to get the same reference. Since each JVM instance is a separate process and because one process cannot access the memory use by another process, you will get one String literal pool for each JVM independent from other instances.

Thanks & Regards,
Manjula



Thanks Manjula. This link describes the topic in great details. I am now able to understand it properly.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!