It's really a matter of opinion. In your example, System.out.println(5)
would be slightly more efficient, as you only refer to the number once and never change it. As was said in a comment, int
is a primitive type and not a reference - thus it doesn't take up much space. However, you might want to set actual reference variables to null only if they are used in a very complicated method. All local reference variables are garbage collected when the method they are declared in returns.