Java中的字符串实习

时间:2019-02-27 15:26:07

标签: java string string-interning

我正在查看有关Udemy的在线课程,其中讲师提供了String Interning的以下示例。

示例1:

Private Sub Worksheet_Change(ByVal Target As Range)     
    Dim rngWatch As Range     
    Dim strOld As String     
    Dim strNew As String

    'What cell is the invoice number in?     
    Set rngWatch = Range("A1")      

    'Did user change it?     
    If Intersect(rngWatch, Target) Is Nothing Then Exit Sub      

    strOld = rngWatch.Value      

    'Are there already hypens?     
    If Len(strOld) = Len(Replace(strOld, "_", "")) Then strNew = Left(strOld, 3) & "_" & Mid(strOld, 4, 3) & "_" & Mid(strOld, 8)          

    'Turn this off for the momenet                                 
    Application.EnableEvents = False                         
    rngWatch.Value = strNew         
    Application.EnableEvents = True     
  End If 
End Sub 

示例2:

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

以上两个代码段之间有什么区别?为什么一个人被拘留而另一个人没有被拘留?

根据对讲师的解释,在第一个示例中,String s1 = "lo"; String s2 = "hel" + s1 ; // This is not interned automatically by JVM 的值在编译时是已知的,但是在第二个示例中,s1的值在运行时是已知的。为什么会这样?

1 个答案:

答案 0 :(得分:3)

您可以使用javap -v <file>.class命令查看生成的字节码。

看看Java 11字节码,第一个示例由<ol class="numbered-list"> <li> <p> <strong><span style="color:#01A7E5;">Invoice Number</span></strong> </p> </li> <li> <p> <strong><span style="color:#01A7E5;">Service Address:</span></strong> The address where you receive Entrust Energy electricity service. </p> </li> <li> <p> <strong><span style="color:#01A7E5;">Account Number:</span></strong> Your Account Number identifies each Entrust Energy account you may have and is often used to pay your electric bill or set up recurring payments. You may have more than one Account Number. </p> </li> <li> <p> <strong><span style="color:#01A7E5;">Bill Date: </span></strong>The date your electric bill is processed. You will receive one electric bill per month. </p> </li> <li> <p> <strong><span style="color:#01A7E5;">Account Summary:</span></strong> An itemization of your balance, payments <g class="gr_ gr_18 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" data-gr-id="18" id="18">and</g> charges for electric service as disclosed in your electricity facts label (EFL), including applicable taxes and fees. Please refer to the EFL you received when you signed up with Entrust Energy for more information about your applicable electric bill rates. </p> </li> <li> <p> <strong><span style="color:#01A7E5;">Total Amount Due:</span> </strong>This is the total amount that you currently owe Entrust Energy, including past due balances from your previous electric bills. When paying your electric bill by mail, please do so at least five days prior to the due date so that we receive your bill on time. Click here to learn about your other payment options. </p> </li> </ol>编译器进行了优化:

javac

第二个示例尚未完全优化,Code: stack=1, locals=2, args_size=1 0: ldc #2 // String hello 2: astore_1 3: return 用于构造第二个字符串:

invokedynamic