• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Interned Strings?

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In textbook I came across below statements:

the JVM uses a unique instance for string literals with the same character sequence in order to improve efficiency and save memory. Such an instance is called an interned string.



In the attached images you can see author mentioning s1 = s3 but s2 not equal to s4.

My question is,

When String reference varialble is assigned to hardcoded literal, it will still be treated as String object holding literal value.

for example. String s1 = "Welcome to Java" ---Here "Welcome to Java" should be treated as Strong object holding "Welcome to Java" literal.
               
In the s3 its an object holding literal. In the image examples, they are objects. Why does s1, s3 not treated as objects while s2, s4 are treated as separate objects?
Screen-Shot-2021-05-11-at-11.07.11-PM.png
[Thumbnail for Screen-Shot-2021-05-11-at-11.07.11-PM.png]
Screen-Shot-2021-05-11-at-11.07.20-PM.png
[Thumbnail for Screen-Shot-2021-05-11-at-11.07.20-PM.png]
 
Saloon Keeper
Posts: 12992
281
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

milind k das wrote:When String reference varialble is assigned to hardcoded literal, it will still be treated as String object holding literal value.


No. A reference variable NEVER holds a literal. Reference variables ALWAYS hold references. If you assign a string literal to a variable of type String, Java simply creates an object for that string literal (but only once per distinct literal) and assigns a reference to that String object to the variable. In the image you attached you can see that s1 and s3 are both references that point to the same String object.

for example. String s1 = "Welcome to Java" ---Here "Welcome to Java" should be treated as Strong object holding "Welcome to Java" literal.


No. s1 should be treated as a variable that holds a reference to an object that is equal to "Welcome to Java".
               

In the s3 its an object holding literal.


Again, no. s3 is a variable that holds a reference to an object that is equal to "Welcome to Java". But because the programmer used the same string literal they used for s1, s3 and s1 simply refer to the same String instance.

Why does s1, s3 not treated as objects while s2, s4 are treated as separate objects?


None of s1, s2, s3 and s4 are objects. They are all variables. They all hold references to objects. Which objects they refer to depends on whether you used a string literal or not.

Variables that were assigned the same string literal will refer to the same object. Variables that aren't assigned a string literal will refer to separate objects, unless you explicitly assigned them a reference to an interned string. You can do this in two ways:
OR:
 
milind k das
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the response.

May I know which constructor in String class is responsible to create object incase of String s1 = "Welcome to Java" and which constructor incase of  String s2 = new String("Welcome to Java"). ?

Why are only string literal hardcoded are treated like "intern" way? Why not the Objects created with new keyword?
 
Marshal
Posts: 72926
330
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

milind k das wrote:. . . which constructor in String class . . . create object . . . String s1 = "Welcome to Java"

None. The JLS (=Java® Language Specification) tells you that, “At run time, a string literal is a reference to an instance of class String (§4.3.3) that denotes the string represented by the string literal.”

and which constructor incase of  String s2 = new String("Welcome to Java"). ?

This one, which I think it is usually mistaken to use because String is an immutable type.

Why are only string literal hardcoded are treated like "intern" way? Why not the Objects created with new keyword?

Because the instance created from a constructor has no existence before the code is executed. Because the constructor's documentation as above says it, “Initializes a newly created String object . . . a copy of the argument string.” They acknowledge that it may not be necessary to create a copy of a String object, but supplied fuinctionality to conver all possibilities, including that somebody might want a copy of a String object.
 
milind k das
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. I will be doing more reading for understanding. I have another question.

What is the difference between inheritance and object instantiation(then using the instantiated object)? They both seem to be performing same tasks. What's need for inheritance when using objects itself are helping in achieving the tasks?
 
Saloon Keeper
Posts: 8222
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a different question and should be asked in a new topic with an appropriate description header. This makes it easier for people to find.
 
If you live in a cold climate and on the grid, incandescent light can use less energy than LED. Tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic