Efficiency of Java code with primitive types

前端 未结 9 750
遥遥无期
遥遥无期 2021-01-03 12:18

I want to ask which piece of code is more efficient in Java? Code 1:

void f()
{
 for(int i = 0 ; i < 99999;i++)
 {
  for(int j = 0 ; j < 99999;j++)
  {         


        
9条回答
  •  清酒与你
    2021-01-03 12:29

    No, It does not make a difference at all (speed wise). They both get compiled into the same code. And there's no allocation and deallocation going on like MasterGaurav said.

    When the method starts, the JVM allocates enough memory slots for all local variables, and no more allocations occurs until the end of the method.

    The only small tiny insignificant difference (other than the scope), is that with the first example, the memory allocated for i & j can be reused for other variables. Therefore, the JVM will allocates fewer memory slots for this method (well, yous saved some bits)

提交回复
热议问题