Kentucky88
January 16th, 2012, 07:32 PM
Is there any overhead in using local variables in Java in an effort to enhance readability? For example, is the following function:
public static void main(String args[])
{
System.out.println(getNumber());
}
public int getNumber()
{
return 1;
}any more efficient than:
public static void main(String args[])
{
int somenumber = getNumber();
System.out.println(somenumber);
}
(using same getNumber() as defined above.)I would think that the Oracle or OpenJDK Java compilers would be smart enough to realize that the local variable "somenumber" in the function above is only used as a holder for the result of calling getNumber(). So that if the result of getNumber() as well as the variables used by getNumber() are not changed by the code, then the optimized compiled code would not have a local variable and simply pull the result from getNumber(). Is that what really happens?
public static void main(String args[])
{
System.out.println(getNumber());
}
public int getNumber()
{
return 1;
}any more efficient than:
public static void main(String args[])
{
int somenumber = getNumber();
System.out.println(somenumber);
}
(using same getNumber() as defined above.)I would think that the Oracle or OpenJDK Java compilers would be smart enough to realize that the local variable "somenumber" in the function above is only used as a holder for the result of calling getNumber(). So that if the result of getNumber() as well as the variables used by getNumber() are not changed by the code, then the optimized compiled code would not have a local variable and simply pull the result from getNumber(). Is that what really happens?