Sorry if this is a newbie question but I couldn\'t find an answer for this. Is it better to do this:
int result = number/number2;
return result;
Compilers are usually smart enough to optimize this sort of thing as appropriate. See data-flow optimizations on Wikipedia.
In this case, it will probably need to allocate a temporary variable to store the result even if you don't specify one yourself.
Edit: Clashsoft is right about the bytecode compiler:
$ cat a.java
class a {
public static int a(int x, int y) {
return x / y;
}
public static int b(int x, int y) {
int r = x/y;
return r;
}
public static int c(int x, int y) {
final int r = x/y;
return r;
}
}
$ javap -c a
Compiled from "a.java"
class a {
a();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."":()V
4: return
public static int a(int, int);
Code:
0: iload_0
1: iload_1
2: idiv
3: ireturn
public static int b(int, int);
Code:
0: iload_0
1: iload_1
2: idiv
3: istore_2
4: iload_2
5: ireturn
public static int c(int, int);
Code:
0: iload_0
1: iload_1
2: idiv
3: istore_2
4: iload_2
5: ireturn
}