0

I was viewing an online course on Udemy where the instructor provided the following examples for String Interning.

Example 1:

String s1 = "hel" + "lo"; // This is interned by JVM

Example 2:

String s1 = "lo";
String s2 = "hel" + s1 ; // This is not interned automatically by JVM

What is the difference between the above two code snippets? Why one is interned but the other is not interned?

As per the explanation of the instructor, in the first example, the value of s1 is known at compile time, but in the second example the value of s2 is known at run time. Why is that?

Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
  • 1
    Because the compiler is not that smart. Why do you need to worry? – Ole V.V. Feb 27 '19 at 15:27
  • 1
    could you please elaborate? – Soumyadeep Ganguly Feb 27 '19 at 15:28
  • 3
    Having programmed Java for more than 20 years, all I’ve needed to know is that identical strings are sometimes distinct objects, sometimes the same object. When and whether they are interned I can safely ignore. My anecdote: The only time it’s bothered me was when I told my students that `==` would not work between strings, and when they used it anyway, it *sometimes* worked. – Ole V.V. Feb 27 '19 at 15:30
  • 1
    what if somehow `s1` (second example) get changed by some Thread on the background? would be hard for the compiler to check all possibilities (including code weaving and so) **OR** it is just the way it was specified/implemented – user85421 Feb 27 '19 at 15:33
  • 2
    Pretty funny that questions about String interning _always_ generates upvotes, because no one can be bothered to actually do a little bit of research. There are many duplicates on Stack Overflow, how about closing such questions instead of answering them over and over again? – Tom Feb 27 '19 at 15:34
  • @Tom it is already closed.... [:-) – user85421 Feb 27 '19 at 15:35
  • 2
    @CarlosHeuberger Yes, after I searched for a dupe and casted a closed vote. – Tom Feb 27 '19 at 15:36
  • @Tom It's pretty funny that I encounter at least 3 questions about json parsing per day which are exact duplicates. Some are not just that good with researching, probably due to not being native english speakers, which makes it a lot harder. And asking duplicate questions is not bad at all either – Lino Feb 27 '19 at 15:37
  • 1
    @CarlosHeuberger You’re indirectly answering the question: On one hand, if `s1` and `s2` are local variables in the same method, what you say can’t happen. However, having separate rules for local and instance variables would be unmanageable, I’d say a catastrophe. So they made simple rules that work. This is why. – Ole V.V. Feb 27 '19 at 15:37
  • @OleV.V sure it can't happen? with code weaving and all that weird stuff? but agreed on second part – user85421 Feb 27 '19 at 15:38
  • 2
    @Lino You can apply that excuse for OPs (and I still would argue that this is a pretty weak excuse), but not for the people who keep answering such questions, especially if they already have either a lot of rep points or a gold badge on the corresponding tag. – Tom Feb 27 '19 at 15:39
  • 1
    @Tom I know... have you noted the [:-) at the end of my comment? – user85421 Feb 27 '19 at 15:39
  • 1
    @Tom I am agreeing 100% to that – Lino Feb 27 '19 at 15:40
  • Wow! Though I joined a long time ago, I am neither a professional coder nor a stack overflow regular.. so I guess I will need some time before getting used to the rules.. Thanks anyways guys for pointing me to the right direction – Soumyadeep Ganguly Feb 27 '19 at 15:51
  • @SoumyadeepGanguly Like I said, my comment is more meant for experienced users who should know how to search for a proper duplicate, like in this situation. And if you want to do some research prior asking (which is a very good thing, thank you), then you should use one of the common search engines our there (they work much better than the search here on Stack Overflow) and you could add `site:stackoverflow.com` in the search bar (like "string interning concatenation site:stackoverflow.com") to narrow the search results to Stack Overflow. – Tom Feb 27 '19 at 16:23
  • @Tom thanks for the suggestion – Soumyadeep Ganguly Feb 27 '19 at 16:39

1 Answers1

3

You can use javap -v <file>.class command to see the generated bytecode.

Looking at Java 11 bytecode the first example is optimized by the javac compiler:

Code:
  stack=1, locals=2, args_size=1
     0: ldc           #2                  // String hello
     2: astore_1
     3: return

The second example is not fully optimized, invokedynamic is used to construct the second string:

Code:
  stack=1, locals=3, args_size=1
     0: ldc           #2                  // String lo
     2: astore_1
     3: aload_1
     4: invokedynamic #3,  0              // InvokeDynamic #0:makeConcatWithConstants:(Ljava/lang/String;)Ljava/lang/String;
     9: astore_2
    10: return
Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111