我正在窥探Java API并在Integer类中找到它
for (;;) {
q = (i * 52429) >>> (16+3);
r = i - ((q << 3) + (q << 1)); // r = i-(q*10) ...
buf [--charPos] = digits [r];
i = q;
if (i == 0) break;
}
我想知道为什么开发人员采用这种方法,而不是像
那样do {
q = (i * 52429) >>> (16+3);
r = i - ((q << 3) + (q << 1)); // r = i-(q*10) ...
buf [--charPos] = digits [r];
i = q;
} while (i != 0);
为什么有人会赞成使用循环的break语句?这有点效率吗?
答案 0 :(得分:3)
更有可能是开发商青睐或首先想到的模式。字节代码和性能不太可能有很大不同。
答案 1 :(得分:2)
除了风格之外,两者没有区别。它们都分解成相同的字节码。
package SO37060005;
public class LoopStyle {
int q, i, r, charPos, digits[], buf[];
public void loop1() {
for (;;) {
q = (i * 52429) >>> (16 + 3);
r = i - ((q << 3) + (q << 1)); // r = i-(q*10) ...
buf[--charPos] = digits[r];
i = q;
if (i == 0) {
break;
}
}
}
public void loop2() {
do {
q = (i * 52429) >>> (16 + 3);
r = i - ((q << 3) + (q << 1)); // r = i-(q*10) ...
buf[--charPos] = digits[r];
i = q;
} while (i != 0);
}
}
$ javap -c LoopStyle.class
Compiled from "LoopStyle.java"
public class SO37060005.LoopStyle {
int q;
int i;
int r;
int charPos;
int[] digits;
int[] buf;
public SO37060005.LoopStyle();
Code:
0: aload_0
1: invokespecial #16 // Method java/lang/Object."<init>":()V
4: return
public void loop1();
Code:
0: aload_0
1: aload_0
2: getfield #23 // Field i:I
5: ldc #25 // int 52429
7: imul
8: bipush 19
10: iushr
11: putfield #26 // Field q:I
14: aload_0
15: aload_0
16: getfield #23 // Field i:I
19: aload_0
20: getfield #26 // Field q:I
23: iconst_3
24: ishl
25: aload_0
26: getfield #26 // Field q:I
29: iconst_1
30: ishl
31: iadd
32: isub
33: putfield #28 // Field r:I
36: aload_0
37: getfield #30 // Field buf:[I
40: aload_0
41: dup
42: getfield #32 // Field charPos:I
45: iconst_1
46: isub
47: dup_x1
48: putfield #32 // Field charPos:I
51: aload_0
52: getfield #34 // Field digits:[I
55: aload_0
56: getfield #28 // Field r:I
59: iaload
60: iastore
61: aload_0
62: aload_0
63: getfield #26 // Field q:I
66: putfield #23 // Field i:I
69: aload_0
70: getfield #23 // Field i:I
73: ifne 0
76: return
public void loop2();
Code:
0: aload_0
1: aload_0
2: getfield #23 // Field i:I
5: ldc #25 // int 52429
7: imul
8: bipush 19
10: iushr
11: putfield #26 // Field q:I
14: aload_0
15: aload_0
16: getfield #23 // Field i:I
19: aload_0
20: getfield #26 // Field q:I
23: iconst_3
24: ishl
25: aload_0
26: getfield #26 // Field q:I
29: iconst_1
30: ishl
31: iadd
32: isub
33: putfield #28 // Field r:I
36: aload_0
37: getfield #30 // Field buf:[I
40: aload_0
41: dup
42: getfield #32 // Field charPos:I
45: iconst_1
46: isub
47: dup_x1
48: putfield #32 // Field charPos:I
51: aload_0
52: getfield #34 // Field digits:[I
55: aload_0
56: getfield #28 // Field r:I
59: iaload
60: iastore
61: aload_0
62: aload_0
63: getfield #26 // Field q:I
66: putfield #23 // Field i:I
69: aload_0
70: getfield #23 // Field i:I
73: ifne 0
76: return
}