Use the local environment
The parameters passed when calling the method and the temporary variables created during the call are saved in the stack (Stack), which is faster. Other variables, such as static variables, instance variables, etc., are created in the Heap and are slower.
The following is a test case
// private static int a = 0; public static void main(String[] args) { int a = 0; long start = System.currentTimeMillis(); for (int i = 0; i < 1000000; i++) { a = a + 1; System.out.println(i); } System.out.println(System.currentTimeMillis() - start); }
The running results are obvious. It takes 15677ms to use static variables and 13509ms to use local variables. It can be seen that the access speed of local variables is higher than that of class member variables.
The above is the detailed content of How Java uses local environments. For more information, please follow other related articles on the PHP Chinese website!