I was watching a video on basic Java game programming and saw this bit of code
private void render() {
    BufferStrategy bs = getBufferStrategy();
    if (bs == null){
        createBufferStrategy(3);
        return;
    }
}
It seems to me like the code would run faster by declaring the reference "bs" outside of the render method so it isn't recreated every single frame, but then I actually tested it myself and found the opposite was true.
package test;
public class tm {
    static String s;
    public static void loop1(){
        s = "hi";
    }
    public static void loop2(){
        String d;
        d = "hi";
    }
    public static void main(String[] args) {
        long x1 = System.nanoTime();
        for(int i = 0;i<100000;i++)
            loop1();
        long x2 = System.nanoTime();
        for(int i = 0;i<100000;i++)
            loop2();
        long x3 = System.nanoTime();
        System.out.println(x2-x1);
        System.out.println(x3-x2);
    }
}
After running this a few times it became apparent that loop1 took on average an order of magnitude longer. It seems counter intuitive that simply accessing an instance field takes longer than creating a new variable. Am I missing something or is it that simple? How can I learn which equivalent actions take longer so I can better optimize my code?
1937701
284818
2036061
599144
3189017
675694
1971058
608125
there are a few runs. Did I benchmark incorrectly?
 
    