Just as an experiment I tried;
class FinalTest
{
static int number=1;
static final int FINAL_NUMBER=1;
public static void main(String[] args)
{
long a;
long startTime,endTime;
long count=1000000000;
a=0;
startTime=System.currentTimeMillis();
for (long i=0;i<count;i++) {a+=number;}
endTime=System.currentTimeMillis();
System.out.println(""+count+" adds int took "+(endTime-startTime)+" ms");
a=0;
startTime=System.currentTimeMillis();
for (long i=0;i<count;i++){a+=FINAL_NUMBER;}
endTime=System.currentTimeMillis();
System.out.println(""+count+" adds final int took "+(endTime-startTime)+" ms");
}
}
Results for java version 1.6.0_03;
1000000000 adds int took 5547 ms
1000000000 adds final int took 2968 ms
Just under twice as fast - pretty much what I’d expected.
Then I tried;
class FinalTest
{
public static void main(String[] args)
{
long a;
long startTime,endTime;
long count=1000000000;
a=0;
startTime=System.currentTimeMillis();
for (long i=0;i<count;i++){a=addInt(a,1);}
endTime=System.currentTimeMillis();
System.out.println(""+count+" adds int took "+(endTime-startTime)+" ms");
a=0;
startTime=System.currentTimeMillis();
for (long i=0;i<count;i++){a=addFinal(a,1);}
endTime=System.currentTimeMillis();
System.out.println(""+count+" adds final int took "+(endTime-startTime)+" ms");
}
private static long addInt(long a, int b){return a+b;}
private final static long addFinal(long a, int b){return a+b;}
}
and got;
1000000000 adds int took 2953 ms
1000000000 adds final int took 3047 ms
Very slightly slower!