example 1:
1. final int a = 1;
2. final int b;
3. b=5;
4. int x=0;
5. switch(x)
6. {
7. case a:
8. case b://Exception
9. }
Switch(condition)中condition只能为byte、short、int和enum(1.5版本)类型。
example 2:
1. switch(x)
2. {
3. case 1:
4. {
5. System.out.print("123");
6. break;
7. }
8. case 2:
9. {
10. System.out.print("456");
11. break;
12. }
13. }
example 3:
for标签例子
1. boolean isTrue=true;
2. foo:
3. for(int i=0;i<5;i++)
4. {
5. while(isTrue)
6. {
7. System.out.print("123");
8. break foo;
9. }
10. }
11. System.out.print("456");
example 4:
1. byte b=2;
2. switch(b)
3. {
4. case 22:
5. System.out.print("123");
6. break;
7. case 128: //Exception超过了byte的范围
8. System.out.print("456");
9. break;
10. }
example 5:
在try……catch……finally语句当中,finally中的语句并不是一定会运行到的.比如在System.exit(1);语句里面就不会运行到。具体见下面所示:
public class Beat {
public static void main(String[] args){
try {
System.out.print("1");
System.exit(1);
}catch(Exception e) {
System.out.print("2");
}finally {
System.out.print("3");
}
}
}
第5句如果没有则会运行到第8、9行里面。
example 6:
一般的异常应该放到具体异常的后面,如果不这样将会发生异常,不能编译。
example 7:
在方法中定义的内部类如果要访问方法中的变量,则变量前必须加final,但是此final并不是表示是一个常量,只是一个简单的标记而已
class A{
private int a=1;
public void fun(final int log){ //没有final将是错误的,不能编译.
class B{
public void print() {
System.out.println("a="+a+",log="+log);
}
}
new B().print();
}
}
public class Text0{
public static void main(String args []) {
new A().fun(10);
}
}
example 8:
class Outer{
private String info="wyp-->397090770";
class Inner{
public void print() {
System.out.print("INFO="+info);
}
}
}
public class Text0{
public static void main(String wyp[]) {
Outer o=new Outer();
Outer.Inner in =o.new Inner(); //注意内部类的调用
in.print();
}
}
example 9:
抽象类里面可以实现一个一般的方法,可以有构造函数;也可以有带参数的构造函数,但是当不存在无参的构造函数时,不可以使用带参数的构造函数
example 10:
在if(condition)……else、while(condition){} 等语句里面,它们的condition都是一个boolean类型的,运行时(int类型为例)会出现类型不匹配:不能从 int 转换为 boolean,比如:
public class Beat {
public void test(String str){
int check=4;
if(check = str.length()){ //本句出错,无法编译
System.out.print(str.charAt(check-=1)+"");
}else{
System.out.print(str.charAt(0)+"");
}
}
public static void main(String[] args){
Beat c = new Beat();
c.test("four"); c.test("tee"); c.test("to");
}
}