This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!
See this thread for details.
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Is Constructor is invoked during object creation

 
Ranch Hand
Posts: 32
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Oracle PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Oracle PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76825
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
pie. tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic