On Fri, 3 Jun 2022 11:19:04 GMT, ExE Boss <[email protected]> wrote:
>> You would think that, but javac doesn't do anything fancy once you store to
>> a local
>>
>> javap output for lines 108 through 111:
>>
>> 449: ldc #148 // float 0.1f
>> 451: fstore 31
>> 453: fload 31
>> 455: invokedynamic #149, 0 // InvokeDynamic
>> #4:makeConcatWithConstants:(F)Ljava/lang/String;
>> 460: astore 32
>> 462: fload 31
>> 464: aload 4
>> 466: invokedynamic #152, 0 // InvokeDynamic
>> #7:makeConcatWithConstants:(FLjava/lang/String;)Ljava/lang/String;
>> 471: astore 33
>> 473: aload 4
>> 475: fload 31
>> 477: invokedynamic #155, 0 // InvokeDynamic
>> #10:makeConcatWithConstants:(Ljava/lang/String;F)Ljava/lang/String;
>
> I guess it only happens for `final` locals.
Yes, looks like javac does constant fold when the local is explicitly final:
final float f = 0.1f;
System.out.println("const folding? " + f);
javap:
0: getstatic #2 // Field
java/lang/System.out:Ljava/io/PrintStream;
3: ldc #3 // String const folding? 0.1
5: invokevirtual #4 // Method
java/io/PrintStream.println:(Ljava/lang/String;)V
Maybe this constant folding is something javac could be enhanced to do on
effectively final locals, though I think we can defer adjusting
`HelloClasslist` to break that optimization when that time comes.
-------------
PR: https://git.openjdk.java.net/jdk/pull/8855