java编程技术 期末考试复习

来源:互联网 发布:vs2013怎么用c语言 编辑:程序博客网 时间:2024/04/27 11:04

1.类名前面可加public也可不加,加了保存的文件名必须为类名。


2.由1可知,一个文件里面最多只能有一个(最外层)类是public,否则文件名是什么呢?


3.

package fuxi1;public class Main {public class test  {  }public  static void main(String args[])  {  System.out.println("2");  }}
实践证明:

一个文件里面可以有两个public类,但除了一个之外,其余都是内部类。



package fuxi1; class Main {//4.所有外层的类可以不是public: class test  {  }public static void main(String args[])//5.但是main函数必须是public,且必须是static,否则怎样运行main呢?  {  System.out.println("2");  }}
6.牢记main函数的写法:

public static void main(String args[])

不能返回int,记住形参表!!!



7.

package fuxi1;import java.util.Scanner; class Main { class test  {  }public static void main(String [] args)  {Scanner sc=new Scanner(System.in);while(sc.hasNext())//hasNext()  ,如果如果读取的是整数可以是 hasNextint();{   String ss=sc.next();//空格和回车会使之断开,当然按了一个回车就是有输出了,类似于c++的cin   System.out.println(ss);   for(int i=0;i<ss.length();i++)//ss.length是错误的   {   char x=ss.charAt(i);         //String类型不能直接输出ss[i],需要ss.charAt(i),即String对象名.charAt(下标);//      System.out.println("ss("+i+")is"+ss[i]+"");   System.out.println("ss("+i+")is'"+x+"'");//输出的时候""自动成为一个String类型。在里面可以用+连接其他类型 。   }}  }}


8.

import java.util.Scanner;public class Main {public static void main(String args[]){   String[] ss={"sddssd","dfsfcce","2323fdf"};//一种可行的数组定义方法   System.out.println(ss);   System.out.println(ss.length);//字符串数组的字符串个数   //System.out.println(ss.length());是错误的,这个length()只能用于单个字符串   for(int i=0;i<ss.length;i++)//ss.length是错误的   {   System.out.println("ss("+i+")is'"+ss[i]+"'");   }}  }

9.

我在两个文件里面都放了main函数,结果还是可以运行,鼠标点Main.java的时候运行的是Main里面main

,点击sd的时候,运行的是sd里面的main


10.

package fuxi1;import java.util.Scanner;public class sd {public static void main(String [] args)  {int ret=7;//变量名开头可以使字母,下划线和$int _sdsd;//int 23ere;///变量名开头不能是数字//int $sd_32@#sd;//绝对不能出现 # %int $sd_32$$sd;//变量除了开头,其他地方可以使数字,字母,下划线,$//int %dsd=3;//int *dsd=3;//int sdsd sdsd=2;//不能出现空格//   System.out.println(_sdsd);//没有初始化是不能编译通过的。System.out.println(ret);  }}





11.

关键字和保留字:

java中没有sizeof 运算符。

两个保留字:

goto 和  const

关键字和保留字都是小写的


12.

java中的数据类型

分为基本类型和复合类型:

复合类型:包括字符串,数组,类,和接口。



13.

package fuxi1;import java.util.Scanner;public class sd {public static void main(String [] args)  {int ret=012;//0开头表示8进制int a=0x3f;//0x开头表示16进制,都是0不是oSystem.out.println(ret);System.out.println(a);  }}



14.

package fuxi1;import java.util.Scanner;public class sd {public static void main(String [] args)  {double ret=2e3;//double b=2E3;//大小写e均可double a=3.844;//默认为double//int c=3.855;//错误,浮点型不能复制给整形double c=3.855F;double d=3D;//然而整形可以赋值给浮点型long e=1230254905943059435L;//long f=1230254905943059435;错误会提示超出了范围System.out.println(ret);System.out.println(a);System.out.println(b);System.out.println(c);System.out.println(d);System.out.println(e);  }}


15.java中%运算符可以用于浮点型


package fuxi1;import java.util.Scanner;public class sd {public static void main(String [] args)  {    double a=3%1.6;    double b=3.5%3;    double c=3.3%1.6;System.out.println(a);System.out.println(b);System.out.println(c);  }}



16.

package fuxi1;import java.util.Scanner;public class sd {public static void main(String [] args)  {    double a=3%1.6;    double b=3.5%3;    double c=3.3%1.6;//    System.out.println(String.format("%lf+%lf",a,b));不能用%lf 用%fSystem.out.println(String.format("%f+%f",a,b));System.out.printf("%f+%f\n",a,b);  }}



17.

if和while中必须使用条件句,c++中可以有while(n--)

但是java中等效的写法是 while(n--!=0)


18.


 变量不经过初始化,不能通过编译。


package fuxi1;public class test4 {   static public void main(String args[])   {      int [] s1,s2;      int a1[],a2;//a2是变量,a1是数组名,此时的a1没有只想任何地址      s1=new int [5];      //s2=6;错,s2是数组      a2=3;      a1=new int [a2];//如果有后面的a1=s1,这句话就无效了。      s1[0]=7;      s1[3]=9;      //s2[0]=1;错误,线必须让s2指向一个地址      s2=new int [a2];      s2[0]=3;      a1=s1;//因为a1和s1指向的地址相同,所以a1[0]=s1[0];      System.out.println(String.format(s1[0]+" "+s2[0]+" "+a1[0]+" "+a2));      System.out.println(String.format(s1[0]+" "+s2[0]+" "+a1[3]+" "+a2));//虽然         }}






19.

package fuxi1;public class test5 {static public void main(String args[])   {//int [5] a={1,2,3,4,5};//数组声明的时候不能在等号左边写数组大小//int c[5]={1,2,3,4,5};int [] b={1,2,3,4,5};//int [] d=new int [5]{1,2,3,4,5};后面注明了每个元素,等号右边的括号不必注明数组大小,是不能!!!int [] e=new int []{1,2,3,4,5};for(int x:b)//for(type varname:arrayname)for-each循环只能进行读操作{   System.out.println(x);}   }}

结果:

12345


20.

package fuxi1;public class test6 {static public void main(String args[])   {int [] b={1,2,3,4,5};        System.out.println(b.length);        //System.out.println(b.length());错的   }}


结果:
5



21.

<pre name="code" class="java">package fuxi1;import java.util.Arrays;import java.util.Scanner;public class test7 {public static void main(String args[]){String s=new Scanner(System.in).next();System.out.println(s);char []a={'3','j','e',' ','d'};String s2=a.toString();//通过对象实例化来调用该函数//这种方法好像不行System.out.println(s2);int []b={3,4,5,6,7};s2=Arrays.toString(b);//类的静态函数//这个方法似乎可以System.out.println(s2);                int d=3;        //s2=d.toString();//不能调用基本类型 int 的 toString()}}

package fuxi;    import java.util.Arrays;  import java.util.Scanner;    public class test5 {      public static void main(String args[])      {          char []a={'3','j','e',' ','d'};          String s2=new String(a);//通过对象实例化来调用该函数          System.out.println(s2);        char []c=s2.toCharArray();//只能转化成char数组,不能为转化成int数组        for(char x:c)        {        System.out.print(x);        }        System.out.println();                double d=23.56;        int k=23;        s2=String.valueOf(d);//类名.方法 ,类名.valueOf();        String s=String.valueOf(k);        //String s="d";//局部变量 s 重复        System.out.println(s);        System.out.println(s2);        s=Integer.toString(k);         System.out.println(s);        s=""+k;        System.out.println(s);                    }    }  




22.

package fuxi1;public class test8 {public static void main(String args[]){        int [][] a=new int[5][];//=左边不可以写大小,可以认为左边只是代表形式        //int [5][] a=new int[][];//错,=左边不可以写大小,可以认为左边只是代表形式        a[0]=new int [3];        a[1]=new int []{1,2,3};//[]不可以掉        //可以先分配第一维,再分配第二维。        //a[1]=new int {1,2,3};//错,[]不可以掉        //错啦!int b[3][2]={ {1,3},{2,4},{3,4}};        int c[][]={ {1,3},{2,4,7},{3,4}};    for(int []x:c)//for(int x[]:c)均可    {    for(int y:x)    {    System.out.print(" "+y);    }    System.out.println();    }}}





23.

package fuxi;public class fuxi1 {int x,y;    fuxi1()    {    x=y=0;    //x=0,y=0;java里面竟然不能这么写。        }    fuxi1(int t)    {    this();//构造函数互相调用,调用了this时this必须是第一句。    x=t;    }    void show()    {    System.out.println("x="+x+",y="+y);    }    public static void main(String args[])    {    fuxi1 a=new fuxi1();    //fuxi1 a=new fuxi1;    a.show();      //fuxi1 b=fuxi1();  c++中这种写法是允许的,这里不允许    fuxi1 b=new fuxi1(3);    b.show();    }}



24.一个源文件最多只能有一条package命令,且必须放在源文件的最前面。



25.

package fuxi;public class fuxi2 {int s=2;static int a=2;   int f()   {   return 3;   }   static int f2()   {   return 4;   }public static void main(String args[])//静态函数调用静态方法和静态对象{//System.out.println(s);不能对非静态字段 s 进行静态引用System.out.println(a);//System.out.println(f());不能对类型 fuxi2 中的非静态方法 f()进行静态引用System.out.println(f2());}class T{int d;T(){d=s;}//因为不是静态的,所以这里可调用s}}


结果:

24



26.

package test;import fuxi.fuxi1;//在不同包中,所以要导入public class test1 {public static void main(String args[]){fuxi1 a=new fuxi1();//构造函数 fuxi1()不可视,默认为default,需要把fuxi1的构造函数声明为public       System.out.println(a.x);//字段 fuxi1.x 不可视,需要把int x改为public int x;//此外,fuxi1要声明为public,不能只声明要用的变量和函数}}




27.

package fuxi;//此时fuxi3和fuxi1在同一个包中,也不需要导入fuxi1这个包public class fuxi3 {public static void main(String args[]){fuxi1 a=new fuxi1(3);//因为在同一个包中,默认为default,default对同一个包的类可视,不需要把fuxi1的构造函数声明为public       System.out.println(a.x);//因为在同一个包中,默认为default,不需要把int x改为public int x;}}

3


28.

 private成员默认成成员protected成员public成员同一类中可见是是是是同一类中对子类可见否是是是同一个包中对子类可见否是是是不同包中对子类可见否否是是不同包中对非子类可见否否否是
说明:

private成员只对自己可见

默认成员对同一包中成员可见

protected在默认的技术上,加上对所有类的子类可见

public 对所有类均可见


29.

package fuxi;import test.test1;//import 包名.类名public class test2 extends test1{//继承关键字是extends不是extendpublic static void main(String args[]){//test2 a=  new test1(); //类型不匹配:不能从 test1 转换 为 test2;子=new 父()不行的 test1 b=  new test2();  //父=new 子() 可以,这里没有写test2的构造函数,却有默认构造函数,即无参数的 System.out.println(b.x);}}

/*

package test;import fuxi.fuxi1;//在不同包中,所以要导入public class test1 {public int x,y;/*test1(int x,int y):x(x),y(y) java里不能这么搞{}*/public test1(int x)  //注意这三个构造函数的顺序{this(0,0);}public test1(int x,int y){this.x=x;this.y=y;//java 里面没有指针,所有改用指针的地方均可用.,这里本来就该用.}public test1(){this(0,0);}/*public static void main(String args[]){fuxi1 a=new fuxi1();//构造函数 fuxi1()不可视,默认为default,需要把fuxi1的构造函数声明为public       System.out.println(a.x);//字段 fuxi1.x 不可视,需要把int x改为public int x;}*/}

*/

30.

这证明一个类的修饰只能使public、final、abstract

而且abstract和final不能同时使用

package test;private class test2 {//类 test2 的修饰符不合法;只允许使用“公用”、“抽象”和“终态”}


package test;public final abstract class test2 {//类 test2 的修饰符不合法;只允许使用“公用”、“抽象”和“终态”//类 test2 可以是“抽象”或“终态”,但不能同时为“抽象”和“终态”}

31.

以下两个是可行的

package test; class test2 {//少了个publicpublic int x;test2(){x=3;}}

package test;public class test3 {public static void main(String args[]){int []a=new int[5];test2 t=new test2();System.out.println(t.x);}}

32.


错误的:

package fuxi;import test.test2;//类型 test.test2 不可视,证明类前面不写是defaultpublic class test4  {}


package test;class test2 {//类 test2 的修饰符不合法;只允许使用“公用”、“抽象”和“终态”public int x;test2(){x=3;}//类 test2 可以是“抽象”或“终态”,但不能同时为“抽象”和“终态”}


33.

对于缺省构造函数,隐式超构造函数 test2()不可视。必须定义显式构造函数:

package fuxi;import test.test2;//类型 test.test2 不可视public class test4 extends test2 {}

package test;public class test2 {public int x;test2(){x=3;}//这里加上public或protected就可以}

34.

package test;public class test2 {public int x;private int y;public test2(){x=3;y=2;}}
package test;public class test5 extends test2{void p(){System.out.println(x);System.out.println(y);//字段 test2.y 不可视,因为y是private}}

35.

package test;public class test2 {public int x;private int y;void show() { System.out.println(x);}public test2(){x=3;y=2;}}


package test;public class test5 extends test2{void show() { System.out.println(6);}void p()//覆盖{System.out.println(x);}public static void main(String args[]){test5 a=new test5();a.show();}}

结果:

6



36.



package test;public class test2 {public int x;private int y;void show() { System.out.println(x);}void show2() { System.out.println(4);}public test2(){x=3;y=2;}}


package test;public class test5 extends test2{void show() { System.out.println(6);}void p(){System.out.println(x);}int show2() {                       //父类的show2函数是void,这里写int是不合法的System.out.println(9);}public static void main(String args[]){test5 a=new test5();a.show();}}



37.

子类方法不能缩小所覆盖方法的访问权限,覆盖方法不能比它所覆盖的方法访问性差。


错误的:

package test;public class test5 extends test2{private void show() { //不能降低自 test2 继承的方法的可视性System.out.println(6);}public static void main(String args[]){test5 a=new test5();a.show();}}


可以的:

package test;public class test5 extends test2{public void show() { System.out.println(6);}public static void main(String args[]){test5 a=new test5();a.show();}}

38.

package test;public class test2 {public int x;private int y;void show() { System.out.println(x);}void show2() { System.out.println(4);}public test2(){x=3;y=2;}}



package test;public class test5 extends test2{public void show() { //不能降低自 test2 继承的方法的可视性System.out.println(6);super.show();//注意父类的show()是被覆盖的函数super.show2();//show2()没有覆盖,都可以用super}public static void main(String args[]){test5 a=new test5();//a.super.show();super不是通过对象调用,实在一个类里直接调用//也不能在main里面调用a.show();}}




39.

当super用来显示调用父类构造函数时,必须放在子类构造函数的第一行。

构造函数(指的是super)调用必须是构造函数中的第一个语句



package test;public class test2 {public int x;private int y;test2(int x,int y){this.x=x;this.y=y;}void show() { System.out.println(x);}void show2() { System.out.println(4);}public test2(){x=3;y=2;}}
package test;public class test5 extends test2{test5(){}test5(int x,int y){//int k;构造函数调用必须是构造函数中的第一个语句super(x,y);}public void show() { //super(x,y);构造函数调用必须是构造函数中的第一个语句System.out.println(6);super.show();super.show2();}public static void main(String args[]){//super(x,y);构造函数调用必须是构造函数中的第一个语句test5 a=new test5();a.show();}}


40.

package newpack;public class new1 {int x,z;public new1() {x=3;z=0;}public new1(int x) {this.x=x;z=0;}}

package newpack;public class new2 extends new1 {int y,z;public new2(){y=6;z=1;}public new2(int x){super(x);y=6;z=1;}void show(){System.out.println(super.z);System.out.println(z);}public static void main(String[] args) {// TODO 自动生成的方法存根         new1 fa=new new2();         System.out.println(fa.x);        // System.out.println(fa.y);因为父类中没有y,y是子类的         //而fa是父类的一个对象,所以fa能访问的只是父类相关的部分,不能是子类相关的         new2 a=new new2();         a.show();}}


结果:
301

41.

public class new2 extends new1

public static void main(String[] args) {         new1 fa=new new2();         new2 a=new new2();         new1 b=new new1();        if(fa instanceof new1) System.out.println(1);        if(fa instanceof new2) System.out.println(2);        if(a instanceof new1) System.out.println(3);        if(a instanceof new2) System.out.println(4);        if(b instanceof new1) System.out.println(5);        if(b instanceof new2) System.out.println(6);         }

结果:

1//这种特殊情况父类 父=new 子类() ,父即是父类实例又是子类实例2//这种特殊情况父类 父=new 子类() ,父即是父类实例又是子类实例3//子类对象是父类的实例4//子类对象是子类的实例5//父类对象是父类的实例//父类对象不是子类的实例

42.

package newpack;public class new2 extends new1 {int y,z;//有两个z,super.z是父类的z,普通的z是子类覆盖掉的zpublic new2(){y=6;z=1;}public new2(int x){super(x);y=6;z=1;}void show(){System.out.println(super.z);System.out.println(z);}public static void main(String[] args) {// TODO 自动生成的方法存根         new1 fa=new new2();         new2 a=new new2();         new1 b=new new1();                //fa.show();//没有为类型 new1 定义方法 show(),show()是子类才有的功能         new2 t=(new2)fa;//证明fa可以强制转换赋值到子类对象上恢复  其原来的功能,证明fa有'内//力'啊         t.show();        // (new2)fa.show();这个写法是错误的}}
结果:
01


43.

package newpack;public class new1 {int x,z;public new1() {x=3;z=0;}public new1(int x) {this.x=x;z=0;}static void show(){System.out.println("this is show");}    void show2(){System.out.println("this is show2");}}

package newpack;public class new2 extends new1 {int y,z;//有两个z,super.z是父类的z,普通的z是子类覆盖掉的zpublic new2(){y=6;z=1;}public new2(int x){super(x);y=6;z=1;}static void show()//如果要覆盖父类的static函数,这里也需要static{System.out.println("this is show");}void go(){System.out.println("go");}static void go2(){System.out.println("go2");}static new2 c=new new2();public static void main(String[] args) {// TODO 自动生成的方法存根        new1.show();        //new1.show2();//不能对类型 new1 中的非静态方法 show2()进行静态引用        new1 a=new new1();        a.show2();        a.show();        //go();//不能对类型 new2 中的非静态方法 go()进行静态引用        go2();        new2 b=new new2();        b.go();//可以        c.go();//可以,所以静态关键是注意顺序问题        }}

44.
package newpack;public  abstract class new1 {int x,z;public new1() {x=3;z=0;}public new1(int x) {this.x=x;z=0;}static void show(){System.out.println("this is show");}//abstract void t();抽象类中方法全部实现是可以的abstract void t();//没有花括号,打分号    void show2(){System.out.println("this is show2");}    public static void main(String args[])    {    System.out.println("d");    }}


45.

package newpack;public abstract class new1 {int x,z;public new1() {x=3;z=0;}public new1(int x) {this.x=x;z=0;}static void show(){System.out.println("this is show");}//abstract void t();抽象类中方法全部实现是可以的abstract void t();//没有花括号,打分号    void show2(){System.out.println("this is show2");}   }

package newpack;public class new2 extends new1 {//类型 new2 必须实现继承的抽象方法 new1.t()public void t(){//这里只有加了public,才能在别的类中通过对象调用这个函数System.out.println("go");}

package test;import newpack.new1;import newpack.new2;import fuxi.fuxi1;//在不同包中,所以要导入public class test1 {public static void main(String args[]){//new1 a=new new1();// 不能实例化类 型 new1new2 b= new new2();b.t();}}


结果:

go

46.接口内不能有变量,接口类不能有构造函数

抽象类可以有构造函数,

两者都不能被实例化

package newpack;public abstract class new1 {int x,z;//抽象类中可以没有构造函数,接口里面必须没有//abstract void t();抽象类中方法全部实现是可以的abstract void t();//没有花括号,打分号    }

package newpack;public class new2 extends new1 {//类型 new2 必须实现继承的抽象方法 new1.t()public void t(){//这里只有加了public,才能在别的类中通过对象调用这个函数System.out.println("go");}int y,z;//有两个z,super.z是父类的z,普通的z是子类覆盖掉的zpublic new2(){y=6;z=1;}}



47.

继承 extends

实现  implements


48.

package newpack;public final class new1 {int x,z;    }

package newpack;public class new2 extends new1 {//类型 new2 不能成为终态类 new1 的子类}


49.

package newpack;public final class new1 {    int x,z;//谁说final类中的属性和方法都必须被final修饰符修饰    void showx(){System.out.println(x);}//<span style="font-family: Arial, Helvetica, sans-serif;">谁说final类中的属性和方法都必须被final修饰符  //修饰</span>    public static void main(String args[])    {       new1 a=new new1();           a.showx();    }    }

运行结果:

0


50.

package newpack;interface new1 {//    int x,z;接口中不能有变量static int x=3;//static int y;//空白终态字段 y 可能尚未初始化,虽然没写final,但是确实是final,//与一般的final不同,y必须初被显示初始化static  final int z=3;  final int w=3;//这里的w其实是静态变量,即static    void showx();//接口中的方法一个都不能实现    public static void main(String args[])    {      // new1 a=new new1();    //new1.x=45;//不能对终态字段 new1.x 赋值,    /*     接口中只能有全局静态常量     这里的new1.x不能赋值。     */    //new1.w=3;//不能对终态字段 new1.w 赋值    System.out.println(new1.w);    }    }

结果:

3



51.java只支持但继承,不支持多继承

package newpack;public class new2 implements new1{//类型 new2 必须实现继承的抽象方法 new1.showx()    public  void showx()//不能降低自 new1 继承的方法的可视性,所以要用public,不能默认    {//interface 里面的方法默认都是公有和抽象的。    System.out.println(x);    }    public static void main(String args[])    {        new2 a=new new2();    System.out.println(a.x);    a.showx();    }}

33

52.

多接口:

package newpack;interface new3 {    void show3();    }
package newpack;interface new1 {    void show();    }

package newpack;public class new2 implements new1,new3{    public  void show()    {    System.out.println("show");    }    public  void show3()    {    System.out.println("show3");    }    public static void main(String args[])    {        new2 a=new new2();    a.show();        a.show3();        }}

结果:

show
show3


53.内部类

package newpack;public class outer {     private int size=1;    public class inner    {    private int size=2;    public void doStuff(int size)    {    System.out.println(size);    System.out.println(this.size);    System.out.println(outer.this.size);    //System.out.println(outer.size);    //不能对非静态字段 outer.size 进行静态引用只有静态才能这么高        }    }    static class inner2//只有内部类才能用static    {        }    class name    {        }    static void g()    {    //this.size;//不能在静态上下文中使用 this    }        public static void main(String args[])    {    //this.size;//不能在静态上下文中使用 this    }       }



package newpack;public class ttt {     private int size=1;    public class inner    {    private int size=2;    public void doStuff(int size)    {    System.out.println(size);    System.out.println(this.size);    System.out.println(ttt.this.size);    //System.out.println(outer.size);    //不能对非静态字段 outer.size 进行静态引用只有静态才能这么高        }    }    static class inner2//只有内部类才能用static    {        }    class name    {        }    static void g()    {    //this.size;//不能在静态上下文中使用 this    }        public static void main(String args[])    {    //this.size;//不能在静态上下文中使用 this    }       }


编译后生成的class文件名为:

1.外部类名.class   (对于外部类 q)

2.外部类名$内部类名.class (对于所有内部类)



54.

try{ }  catch(异常类型 名){/*......*/}

package newpack;public class ttt {       public static void main(String args[])    {    String []s={"ACM","ICPC","dfs"};    for(int i=0;i<4;i++)    {    System.out.println(s[i]);        }    }       }




package newpack;public class ttt {       public static void main(String args[])    {    String []s={"ACM","ICPC","dfs"};    for(int i=0;i<4;i++)    {    try {System.out.println(s[i]);}    catch(Exception e)    //catch()括号里面要是什么都不写是错的    {        }    }    }       }



55.

package newpack;public class ttt {    public static void main(String args[])    {    String []s={"ACM","ICPC","dfs"};    for(int i=0;i<4;i++)    {        try {    System.out.println(s[i]);    System.out.println("in");         }    catch(Exception e)    {        }    finally     {    System.out.println("working");    //不管是否有异常发生,这里代码都要执行。    }    System.out.println("visit");    }    }       }


结果:try{}catch(){}finally{}

ACMinworkingvisitICPCinworkingvisitdfsinworkingvisit//这里少了个in,证明try里面异常抛出后后面的就不执行了working//然后try外面的还是会继续执行,特别是finally肯定会执行visit

56.try {} finally{}

package newpack;public class ttt {    public static void main(String args[])    {    String []s={"ACM","ICPC","dfs"};    for(int i=0;i<4;i++)    {        try {    System.out.println(s[i]);    System.out.println("in");    }    finally     {    System.out.println("working");    //不管是否有异常发生,这里代码都要执行。    }    System.out.println("visit");    }    }       }


在i=3时,try抛出异常没有处理,所以会报错。

但是finally语句还是会执行。但是最后那一句不会执行,因为出错了。



57.放错位置




package newpack;public class ttt {    public static void main(String args[])    {    String []s={"ACM","ICPC","dfs"};    for(int i=0;i<4;i++)    {        try {    System.out.println(s[i]);    System.out.println("in");    }    finally     {    System.out.println("working");        }    System.out.println("visit");    }    finally //finally放错了位置{System.out.println("working");}    }       }

package newpack;public class ttt {    public static void main(String args[])    {    String []s={"ACM","ICPC","dfs"};    for(int i=0;i<4;i++)    {        try {    System.out.println(s[i]);    System.out.println("in");    }    int x=3;//int隔断了try和catch    catch(Exception e)    {        }    finally     {    System.out.println("working");        }    System.out.println("visit");    }        }       }


package newpack;public class ttt {    public static void main(String args[])    {    String []s={"ACM","ICPC","dfs"};    for(int i=0;i<4;i++)    {        try {    System.out.println(s[i]);    System.out.println("in");    }    catch(Exception e)    {        }    int z=3;//z隔断了finally    finally     {    System.out.println("working");        }    System.out.println("visit");    }        }       }

58.

可以多个catch并存,但只会有最近一个处理异常。


59.

package newpack;public class ttt {/* extends implements throws try catch才是真正意义上的异常处理  这个写法是直接向上层抛出  当前方法不处理异常,向上层抛出。 */    public static void main(String args[])throws Exception//记住是throws不是throw    {    String []s={"ACM","ICPC","dfs"};    for(int i=0;i<4;i++)    {    System.out.println(s[i]);    System.out.println("in");     }        }       }



60.

package newpack;public class ttt {    public static void main(String args[])throws Exception//记住是throws不是throw    {    String []s={"ACM","ICPC","dfs"};    Exception e=new Exception("抛出异常");    for(int i=0;i<4;i++)    {//    if(i==3)  throw new Exception("抛出异常");这个写法是一样的,简便些        if(i==3)  throw e;    System.out.println(s[i]);    System.out.println("in");     }        }       }



61.

package newpack;public class ttt {       public static void main(String args[])     {    String []s={"ACM","ICPC","dfs"};    for(int i=0;i<4;i++)    {    try {System.out.println(s[i]);}    catch(Exception e)    {    try {throw new Exception("抛出异常");} catch (Exception e1) {// TODO 自动生成的 catch 块e1.printStackTrace();}     /*     *      这可能是所谓的throw要么和try catch finally 配套使用     ,要么和throws配套使用。          throws可以单独使用,但throw不能。     */    }    }    }       }



62.

package newpack;public class ttt {       public static void main(String args[])     {    String []s={"ACM","ICPC","dfs"};    for(int i=0;i<4;i++)    {    try {        if(i==4) throw new Exception("d");    }    catch(Exception e)    {        }    System.out.println(s[i]);    }    }       }

结果为啥是这样????:



63.

package newpack;public class ttt {       public static void main(String args[])     {    String a=new String("abc");    String b=new String("abc");    String c=a;    System.out.println(a==b);    System.out.println(c==a);        }       }



64.

package newpack;public class Line {public static class Point{}}class Triangle{//错 Point p=new Point();//Point 无法解析为类型  Line.Point p=new Line.Point();//错  Line.Point p=Line.new Point();//Line cannot be resolved to a variable/*错      Line l=new Line(); l.point p=new l.Point(); *//*错  Line l=new Line(); l.point p=l.new Point(); */}


65.

package fuxi2;public class fuxi2{String s;fuxi2(){}//如果不写这个,只有下面那个传参数String  cc的构造函数,那么默认构造函数就被屏蔽了    public fuxi2(String cc)    {}void fun(){System.out.println(s);}public static void main(String args[])//String args[]掉了这句,引发大毛病{String cc="33";fuxi2 a=new fuxi2();fuxi2 b=new fuxi2(){};fuxi2 c=new fuxi2(){ void fun(){System.out.println("6");  }  };fuxi2 d=new fuxi2(cc)//cc没有被用到,不写cc结果一样{//fuxi2(String s) {this.s=s;}  不能在这里创建构造函数void fun(){System.out.println("6");  }  //这个函数被覆盖了};a.fun();b.fun();c.fun();}}





66.

class Alpha{public void foo(){System.out.print("A");}}class Beta extends Alpha{public void foo(){System.out.print("B");}}public class test {public static void main(String args[]){Alpha a=new Beta();Beta b=(Beta) a;a.foo();//父类的方法被覆盖了,输出"B"b.foo();//恢复后的a赋值给b,恢复后的a的foo()方法还是子类的}}

BB

67.

package fuxi2;interface A {}class B implements A{}class C extends B{}class D{}public class test{public static void main(String args[]){A a=new C();//a其实instanceof C,所以也instanceof A and BC b=new C();D d=new D();if(a instanceof A)  System.out.print("1");if(a instanceof B)  System.out.print("1");if(a instanceof C)  System.out.print("1");if(b instanceof A)  System.out.print("2");if(b instanceof B)  System.out.print("2");if(b instanceof C)  System.out.print("2");if(d instanceof A)  System.out.print("3");//if(x instanceof X)只要x是X类的对象或者是X类子类的对象即可。}}

111222

68.

public class test {public static void main(String args[]){int s=6;//s-=3+++s;不打空格为错s-=3+ ++s;System.out.println(s);s=6;//++s-=3+ ++s;//标记“++”上有语法错误,删除此标记s+= ++s  + ++s;//6 +7  +8;System.out.println(s);s=6;//s++=s+ ++s;//赋值的左边必须是变量s=s + ++s;//s=6+7;System.out.println(s);s=6;s=s+++ ++s;//s= 6+7 +1;System.out.println(s);}}

-4211314

69.

class Super{static String greeting(){return "Good night";}    String greeting2(){return "Good night";}String name(){return "Tom";}}class Sub extends Super{static String greeting(){return "Hello";}//父类的静态方法,子类是覆盖不了的//static String greeting2(){return "Hello";}//此静态方法不能隐藏 Super 中的实例方法,//用静态盖住编译无法通过 String greeting2(){return "Hello";}//此静态方法不能隐藏 Super 中的实例方法String name(){return "Mary";}void show(){System.out.println(greeting());//这里是可以区分的System.out.println(super.greeting());//这里是可以区分的System.out.println(greeting2());//这里是可以区分的System.out.println(super.greeting2());//这里是可以区分的}}public class test {public static void main(String args[]){Super s=new Sub();System.out.println(s.greeting()+","+s.name());System.out.println(s.greeting2()+","+s.name());//s.show();//没有为类型 Super 定义方法 show(),s不能使用子类的方法Sub t=new Sub();t.show();}}

Good night,MaryHello,MaryHelloGood nightHelloGood night


70.

class StaticInitDemo{static int i=1;static{System.out.print(i++);}}public class test {public static void main(String args[]){System.out.println(StaticInitDemo.i);}}

12


71.

class StaticInitDemo{static int i=1;static{System.out.print(i++);}}public class test {    static{//l++;在定义字段之前不能引用该字段k=8;//k++;在定义字段之前不能引用该字段}static int k,l=3;static{l++;k=5;}static{l++;k=9;}//static 块按照出现的先后讲究次序public static void main(String args[]){System.out.println(StaticInitDemo.i);System.out.println(k);}}

129

72.

public class test {    //     z:public static void main(String args[]){for(int i=0;i<4;i++){if(i==2)  break;System.out.println(i);}System.out.println( );x:for(int i=0;i<4;i++){if(i==2)  break x;System.out.println(i);}System.out.println( );x:for(int i=0;i<4;i++)        {y:       for(int j=0;j<4;j++)       {       if(j==3)  break ;//跳出小循环       System.out.println(i+" "+j);       }         }System.out.println( );x:for(int i=0;i<4;i++)        {y:       for(int j=0;j<4;j++)       {       if(j==3)  break y;//跳出小循环       System.out.println(i+" "+j);       }         }System.out.println( );x:for(int i=0;i<4;i++)        {y:       for(int j=0;j<4;j++)       {       if(j==3)  break x;//break x,就是说大循环跳出了       System.out.println(i+" "+j);       }         }System.out.println( );x:for(int i=0;i<4;i++)        {y:       for(int j=0;j<4;j++)       {       if(j==2)  continue y;//对于小循环 continue;       System.out.println(i+" "+j);       }         }System.out.println( );x:for(int i=0;i<4;i++)        {y:       for(int j=0;j<4;j++)       {       if(j==2)  continue x;//对于大循环 continue;       System.out.println(i+" "+j);       }         }}   }

01010 00 10 21 01 11 22 02 12 23 03 13 20 00 10 21 01 11 22 02 12 23 03 13 20 00 10 20 00 10 31 01 11 32 02 12 33 03 13 30 00 11 01 12 02 13 03 1


73.

class Shape{}class Rectangle extends Shape{}class Square extends Rectangle{}class Triangle extends Shape{}public class test {    public static void main(String args[]){   Triangle tr=new Triangle();   Square sq=new Square();   Shape  sh=tr;   System.out.println(sh==tr);//sh和tr引用的内存空间是相同的,sh和tr引用同一个对象   String s="123";   //String a=(String)s;可以   String a=s;   System.out.println(a==s);   System.out.println(a.equals(s));   }   }

truetruetrue


74.

class Shape{}class Rectangle extends Shape{}class Square extends Rectangle{}class Triangle extends Shape{}public class test {    public static void main(String args[]){   Triangle tr=new Triangle();   Square sq=new Square();   Shape  sh=tr;      Triangle ntr=tr; //  Triangle mtr=sh;//类型不匹配:不能从 Shape 转换为 Triangle   //即使sh(父类对象)是从tr(子类对象)赋值来的,但是在这里仍然不能作为子类对象赋值给子类。   sh=sq;//sq是sh所属类的子类的子类对象,这样仍可。   }   }


75.

public class test {    static boolean isPerfect(int a){int s=0;for(int i=1;i*i<=a;i++)  if(a%i==0){s+=i;}return s==a;}public static void main(String args[]){   System.out.println(isPerfect(10));}   }

76.

public class test {    static boolean isInvert(int a){int s=0;int x=a;while(x>0)//注意java里面不能写while(x) ,while和if的()里必须写条件句{s=s*10+x%10;x/=10;}return s==a;}public static void main(String args[]){   System.out.println(isInvert(10));   System.out.println(isInvert(121));   System.out.println(isInvert(122));   System.out.println(isInvert(22122));   System.out.println(isInvert(32122));   System.out.println(isInvert(2122));}   }


falsetruefalsetruefalsefalse


77.

public class test {        static int getlastnum(String s)    {    //if(s==NULL||s.length()==0) return -1;这里大写的NULL是错的,虽然c/c++中可以    if(s==null||s.length()==0) return -1;    char x=s.charAt(s.length()-1);    return '0'<=x&&x<='9'?x-'0':getlastnum(s.substring(0, s.length()-1) );     }public static void main(String args[]){    String s1;    String s2="";   // System.out.println(s1);//局部变量 s1 可能尚未初始化,这里无法使用    System.out.println(s2);    System.out.println(s2.length());   // getlastnum(s1);//局部变量 s1 可能尚未初始化    System.out.println(getlastnum(s2));    System.out.println(getlastnum("abc24us43"));    System.out.println(getlastnum("82445adb"));    System.out.println(getlastnum("ab"));      }   }





78.

abstract class Animal{protected String food;Animal(String food){this.food=food;}public abstract void eat();    }class Dog extends Animal//(这句话当且仅当,出现Dog() 时且没用super()才会说)没有为缺省构造函数定义隐式超构造函数 Animal()。必须定义显式构造函数{Dog(String s){super(s);}Dog()//未定义隐式超构造函数 Animal()。必须显式调用另一个构造函数{super("?");}public void eat(){System.out.println("狗吃"+food);}}public class test {public static void main(String args[]){Animal a1=new Dog("骨头");Animal a2=new Dog();a1.eat();//要求输出“狗吃骨头”a2.eat();//要求输出“狗吃?”}}

狗吃骨头狗吃?


79.

import java.io.*;public class test {public static void main(String args[]){InputStreamReader isr= new InputStreamReader(System.in);BufferedReader  br=new BufferedReader(isr); //没有br.hasNext();String s;try {s = br.readLine();System.out.println(s);br.close();//文件有关的操作都得放到try里面    isr.close();//文件有关的操作都得放到try里面} catch (IOException e) {}//System.out.println(s);如果把try中的这句话放到这里会出现问题}}




80.

import java.io.*;public class test {public static void main(String args[]) throws Exception////IOException亦可{BufferedReader  br=new BufferedReader(new InputStreamReader(System.in)); //没有br.hasNext();String s;s= br.readLine();System.out.println(s);br.close();}}

81.

import java.io.*;public class test {public static void main(String args[]){//InputStreamReader isrr= new InputStreamReader(System.in);//这个不必用try//BufferedReader  brr=new BufferedReader(isrr); //这个不必用try//没有br.hasNext();String s;//s的声明放在try里面外面都行try {FileInputStream fis=new FileInputStream("E:\\ttt.txt");//字节流//这样也行 :FileInputStream fis=new FileInputStream("E:/ttt.txt");//字节流InputStreamReader isr= new InputStreamReader(fis);//字符流BufferedReader  br=new BufferedReader(isr);//缓存流//while(s=br.readLine()!=null) s = br.readLine();while((s=br.readLine())!=null) {System.out.println(s);}fis.close();br.close();//文件有关的操作都得放到try里面    isr.close();//文件有关的操作都得放到try里面} catch (IOException e) {}//s = br.readLine();//无法解析 br,因为br在{}里面,而且这句话一定要处理异常//System.out.println(s);如果把try中的这句话放到这里会出现问题}}


82.

import java.io.*;public class test {public static void main(String args[]){String s;try{//记住是BufferedWriter和BufferedReader不是BufferBufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("E:/tta.txt")));//没有会自动创建BufferedReader br=new BufferedReader(new InputStreamReader (new FileInputStream("E:\\ttt.txt") )  );//因为有'\n'所以'\'要打两次,表示路径,或者用'/'//注意区分Reader和Writerwhile((s=br.readLine())!=null){bw.write(s);//这样写不会换行。//bw.write('\n');//加了这个没用}//Resource leak: 'bw' is never closed//Resource leak: 'br' is never closedbw.close();br.close();}  catch(Exception e){}}}

83.

import java.io.*;public class test {public static void main(String args[]){String s;try{PrintWriter pw=new PrintWriter(new BufferedWriter(new FileWriter("E:/tta.txt")));BufferedReader br=new BufferedReader(new InputStreamReader (new FileInputStream("E:\\ttt.txt") )  );while((s=br.readLine())!=null){//pw.write(s);//这样写不会换行。pw亦可用writepw.println(s);//但是BufferedWriter不可用print和println//pw.print(s);//这个换不了行//pw.printf(s);//pw.printf('\n');类型 PrintWriter 中的方法 printf(String, Object...)对于参数(char)不适用//pw.print('\n');//这个还是没用}pw.close();br.close();}  catch(Exception e){}}}

84.

import java.io.*;public class test {public static void main(String args[]){String s;try{File f1=new File("E:/tta.txt");File f2=new File("E:\\ttt.txt");//f1,f2类似于String,替换""中的内容PrintWriter pw=new PrintWriter(new BufferedWriter(new FileWriter(f1)));InputStreamRea isr=new InputStreamReader (new FileInputStream(f2) )  ;int c;while((c=isr.read())!=-1)//!=null错 ,readLine()错{pw.print(c);//这样用会成数字pw.print((char)c);//正确}pw.close();isr.close();}  catch(Exception e){}}}


85.

import java.io.*;public class test {public static void main(String args[]){String s;try{File f1=new File("E:/tta.txt");File f2=new File("E:\\ttt.txt");//f1,f2类似于String,替换""中的内容FileOutputStream fos =new FileOutputStream (f1 );FileInputStream fis=new FileInputStream(f2)   ;int c;while((c=fis.read())!=-1)//!=null错 ,readLine()错{fos.write((char)c);//正确,fos没有print,bw也没有//fos.write(c);//正确,均正确,fos没有print,bw也没有}fis.close();fos.close();}  catch(Exception e){}}}


86.

import java.io.*;public class test {public static void main(String args[]){String s;try{File f1=new File("E:/tta.txt");File f2=new File("E:\\ttt.txt");//f1,f2类似于String,替换""中的内容OutputStreamWriter  osw =new OutputStreamWriter( new FileOutputStream (f1 )  );InputStreamReader isr=new InputStreamReader(new FileInputStream (f2))   ;int c;while((c=isr.read())!=-1)//字符流等级,仍然没有readLine(),至少要到buffered等级才有{//osw.write((char)c);//字符流等级,正确,fos没有print,bw也没有osw.write(c);//字符流等级,正确,均正确,fos没有print,bw也没有}osw.close();isr.close();}  catch(Exception e){}}}

87.

import java.io.*;class Node{Object m_Data;Node m_Next;public Node(Object data){m_Data=data;m_Next=null;}public void setObject(Object data){m_Data=data;}public Object getObject(){return m_Data;}public void setNext(Node next) {m_Next=next;}public Node getNext() {return m_Next;}}class EmptyQueueException extends Exception//记住是extends Exception{public EmptyQueueException(){System.out.println("队列已空!");}}class Queue{Node m_FirstNode;public Queue(){m_FirstNode=null;}public boolean isEmpty(){if(m_FirstNode==null)  return true;else return false;}public void enqueue(Object node){Node next=m_FirstNode;if(next==null)  m_FirstNode=new Node(node);// Node(node);没有为类型 Queue 定义方法 Node(Object)//所以一定要写newelse{while(next.getNext()!=null)  next=next.getNext();next.setNext(new Node(node));//next.m_Next=new Node(node);一样的}}public Object dequeue()throws EmptyQueueException//这里若写Exception 底下调用这个函数时的try catch也要写Exception e{Object node;if(isEmpty()){//throw new EmptyQueueException;//语法错误,将“( )”插入到完整 Expression 中//要将异常当作一个新的对象throw new EmptyQueueException();//throw new EmptyQueueException();不可达代码,不可用两个throw}else {node=m_FirstNode.getObject();m_FirstNode=m_FirstNode.getNext();return node;}}}public class test {public static void main(String args[]){Queue q=new Queue();q.enqueue("first!");q.enqueue("second!");q.enqueue("third!");try{   while(true )  System.out.println(q.dequeue()) ;}catch(EmptyQueueException e)//EmptyQueueException() e 这里不能多打括号{}}}



88.

interface shape{double area();double zc();}class rectangle implements shape{double l,w;rectangle(double l,double w){this.l=l;this.w=w;//必须加this}public  double area(){return l*w;}public double zc(){return 2*(w+l);}}class circle implements shape{double r;final double PI=3.14;circle(double r){this.r=r;}publicdouble area(){return PI*r*r;}public double zc(){return 2*PI*r;}}class shape2{}class rectangle2 extends shape2{}public class test {public static void main(String []args){shape r=new rectangle(10,10);//shape rr=new shape(10,10);//不能有new shape,因为shape是interface接口rectangle rec=new rectangle(10,10);shape c=new circle(2);System.out.println(r instanceof shape);//trueSystem.out.println(r instanceof rectangle);//true 在这里可以看作 r其实就是rectangle类的对象System.out.println(rec instanceof shape);//trueSystem.out.println(rec instanceof rectangle);//trueshape2 s2=new shape2();rectangle2 r2=new rectangle2();System.out.println(s2 instanceof shape);//false//System.out.println(s2 instanceof rectangle);//条件操作数类型 shape2 和 rectangle 不兼容System.out.println(r2 instanceof shape);//false//System.out.println(r2 instanceof rectangle);//条件操作数类型 rectangle2 和 rectangle 不兼容System.out.println(s2 instanceof shape2);//trueSystem.out.println(s2 instanceof rectangle2);//falseSystem.out.println(r2 instanceof shape2);//trueSystem.out.println(r2 instanceof rectangle2);//true//shape2 o=new shape2();shape2 o=null;System.out.println(o instanceof shape2);//falseSystem.out.println(o instanceof rectangle2);//false/*shape2 o2;System.out.println(o2 instanceof shape2);//局部变量 o2 可能尚未初始化shape o3;System.out.println(o3 instanceof shape);//局部变量 o3 可能尚未初始化System.out.println(o3 instanceof rectangle);//局部变量 o3 可能尚未初始化*/}}

truetruetruetruefalsefalsetruefalsetruetruefalsefalse



89.

</pre><img src="http://img.blog.csdn.net/20160111151000181" alt="" /><p></p><p><img src="http://img.blog.csdn.net/20160111151034319" alt="" /></p><p><span style="font-size:24px">这样是不行的:</span></p><p><span style="font-size:24px">变量x具有二义性,修改方法是改名,或者C不要同时继承B、实现A。</span></p><p><span style="font-size:24px"></span></p><p><span style="font-size:24px">90.</span></p><p><span style="font-size:24px"><img src="http://img.blog.csdn.net/20160111152246975" alt="" /></span></p><p><span style="font-size:24px">修改方法:</span></p><p><span style="font-size:24px">删去ball=new Ball("Football");</span></p><p><span style="font-size:24px">另:接口中的常量必须立马初始化。</span></p><p><span style="font-size:24px"></span></p><p><span style="font-size:24px">91.</span><pre name="code" class="java">public class test5 {      public static void main (String args[])throws EmptyQueueException      {      int x=3;    // if(x>2) throw new EmptyQueueException();     if(x>2) throw new Exception();//未处理的异常类型 Exception    }        }  

public class test5 {      public static void main (String args[])throws Exception      {      int x=3;     if(x>2) throw new EmptyQueueException();     //if(x>2) throw new Exception();//两者均可    }        } 


0 0