下週二小考:
this不能出現在靜態範圍(設計藍圖)
在實體中才會自動給this.
所以寫在靜態範圍時,會編譯失敗
屬性變數 初始值
boolean -- false
CLASS -- null
區域變數不會給初始值
~ 反向運算 (0變1,1變0)。 只能用在整數
! 第二種反向運算 。 只能用在boolean
XOR 可用於密碼,用兩次後會回復原來數字
>> signed right shift 右移=除二,補原來的正負號
>>> unsigned right shift 一律補0
<< 左移,乘2。一律補0
內部簡化:
int a = 128;
int b = a >> 32; (java內部簡化 32%32=0)
int c = a >> 9999 (java內部簡化 9999%32)
所以
b = 128
long a = 128;
long b = a >> 32; (32%64)
byte、short 、char 遇到運算符號,會先升級成int
所以做位移,內部簡化也是用 %32
this不能出現在靜態範圍(設計藍圖)
在實體中才會自動給this.
所以寫在靜態範圍時,會編譯失敗
屬性變數 初始值
boolean -- false
CLASS -- null
區域變數不會給初始值
~ 反向運算 (0變1,1變0)。 只能用在整數
! 第二種反向運算 。 只能用在boolean
XOR 可用於密碼,用兩次後會回復原來數字
>> signed right shift 右移=除二,補原來的正負號
>>> unsigned right shift 一律補0
<< 左移,乘2。一律補0
內部簡化:
int a = 128;
int b = a >> 32; (java內部簡化 32%32=0)
int c = a >> 9999 (java內部簡化 9999%32)
所以
b = 128
long a = 128;
long b = a >> 32; (32%64)
byte、short 、char 遇到運算符號,會先升級成int
所以做位移,內部簡化也是用 %32
考古題
(3) Click the exhibit button:
1. public class Test {
2. private static int j = 0;
3.
4. private static boolean methodB(int k){
5. j += k;
6. return true;
7. }
8.
9. public static void methodA(int i){
10. boolean b;
11. b = i < 10 | methodB(4);
12. b = i < 10 || methodB(8); //不會執行到
13. }
14.
15. public static void main(String args[]){
16. methodA (0);
17. System.out.println(j);
18. }
19. }
What is the result?
A. The program prints "0"
B. The program prints "4"
C. The program prints "8"
D. The program prints "12"
E. The code does not complete
(1) Given:
1. public class Test {
2. public static void main(String args[]){
3. int i = 0xFFFFFFF1; //視為int
4. int j = ~i;
5.
6. }
7. }
What is the decimal(十進位) value of j at line 5?
A. 0
B. 1
C. 14
D. -15
E. An error at line 3 causes compilation to fail.
F. An error at line 4 causes compilation to fail.
public class Test {
public static void main(String[] args){
double a = 9879823.23698;
int b = (int)a ^ 23908;
System.out.println(calc(b));
}
public static int calc(int a) {
int b = a <<< 4;
return b;
}
}
What's the result ?
編譯失敗,沒有<<<這種
public class Foo {
public int calc(int a) {
int b=0;
if(a%5==0) {
if(a--<25); //注意這
b+=10;
} else if(a%5==1) {
b=1;
} else {
b--;
}
return b;
}
public static void main(String[] args) {
Foo a = new Foo();
System.out.println(a.calc(25));
}
}
What's the output ?
A. 10
B. 0
C. -1
D. 1
E. compile error
switch 可接受int、short、byte,不收long、String、boolean
default 不一定要擺哪,沒差。 一定會最後再判斷。
(102) Click the exhibit button:
1. int i=1, j=0;
2.
3. switch(i){
4. case 2:
5. j+=6;
6.
7. case 4:
8. j+=1;
9.
10. default:
11. j +=2;
12.
13. case 0:
14. j +=4;
15.}
16.
What is the value of j at line 16?
A. 0
B. 1
C. 2
D. 4
E. 6 // j +=2;後 不看case 0 直接做 j +=4; 所以 j =6
class Language{
public static final int java=1;
public static final int pascal=2;
public static final int csharp=3;
public static final String dotNet = "4";
}
public class Mgos {
static String lang="4";
public static void main(String argv[]){
switch(lang){
case Language.java:
System.out.println("java");
break;
case Language.dotNet:
System.out.print("dotNet");
case Language.pascal:
System.out.println("pascal");
break;
case Language.csharp:
System.out.println("csharp");
break;
}
}
}
What's the output ?
A) java
B) dotNet
C) pascal
D) csharp
E) None of the above // switch 不收 String
可 for(;;)
不可 for()
可 int i = 0;
for(test(); true ; test(), i++)
編譯失敗
for(double a = 3; a <= 4 ; a+=0.3);
System.out.println(a); //離開a的有效範圍了
編譯失敗
for(int i = 0, long j = 1; ; )
do{
}while(); 一定要加分號
while() 括號內不可以有變數宣告,只會有boolean
label 迴圈可以取名
用在多層迴圈時,可以break中斷在特定迴圈
例:
1.
name:do{....
2.
或 name:
do{.....
3.
不可
name:
int i=0;
do{....(47) Click the exhibit button:
1. public class Test {
2. public static void main(String args[]) {
3. int i= 0;
4. while (i){
5. if (i==4) {
6. break;
7. }
8. ++i;
9. }
10.
11. }
12.}
What is the value of i at line 10?
A. 0
B. 3
C. 4
D. 5
E. the code will not compile
(140) Given:
1. public class ForBar {
2. public static void main(String []args){
3. int i = 0, j = 5;
4. tp: for ( ; ; ) {
5. i ++;
6. for(;;)
7. if(i > --j) break tp;
8. }
9. System.out.println("i = " + i + ", j = "+ j);
10. }
11.}
What is the result?
A.The program runs and prints "i=1, j=0"
B.The program runs and prints "i=1, j=4"
C.The program runs and prints "i=3, j=4"
D.The program runs and prints "i=3, j=0"
E.An error at line 4 causes compilation to fail
F.An error at line 7 causes compilation to fail
(48) Given:
1. int i= 1, j= 10 ;
2. do {
3. if (i++> --j) continue;
4. } while (i<5);
After execution, what are the values for i and j?
A. i = 6 and j= 5
B. i = 5 and j= 5
C. i = 6 and j= 4
D. i = 5 and j= 6
E. i = 6 and j= 6
請先 登入 以發表留言。