As per JLS §15.2 - Forms of Expressions
Some expressions have a value that can be determined at compile time.
These are constant expressions (§15.28).
Multiplicative operators like *, /, and %
falls under constant expressions, so it would be determined at compile time.
@SergeyMorozov was faster than me to write and get byte code proof (#2 = Integer 60000
) but here is the practical proof and above is theoretical/official statement:
Try generating byte code at your end as well using 1000 * 60
and 60000
, and you will see same byte code instructions, and hence there would be same runtime performance.
Java class:
public class Test {
public static void main(String[] args) {
int compileTest = 1000 * 60;
}
}
Byte code:
Classfile /E:/Test.class
Last modified Oct 9, 2015; size 265 bytes
MD5 checksum fd115be769ec6ef7995e4c84f7597d67
Compiled from "Test.java"
public class Test
SourceFile: "Test.java"
minor version: 0
major version: 51
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Methodref #4.#13 // java/lang/Object."<init>":()V
#2 = Integer 60000
#3 = Class #14 // Test
#4 = Class #15 // java/lang/Object
#5 = Utf8 <init>
#6 = Utf8 ()V
#7 = Utf8 Code
#8 = Utf8 LineNumberTable
#9 = Utf8 main
#10 = Utf8 ([Ljava/lang/String;)V
#11 = Utf8 SourceFile
#12 = Utf8 Test.java
#13 = NameAndType #5:#6 // "<init>":()V
#14 = Utf8 Test
#15 = Utf8 java/lang/Object
{
public Test();
flags: ACC_PUBLIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 1: 0
public static void main(java.lang.String[]);
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=1, locals=2, args_size=1
0: ldc #2 // int 60000
2: istore_1
3: return
LineNumberTable:
line 3: 0
line 4: 3
}