JAVA SE 803 考试前突击

考试的宗旨仍然是掌握基础知识,不过鉴于Oracle的这个认证考试还有不少的大家来找茬的成份在,所以一定一定要细心为上。

 

关于抽象类的坑点集合:

  • 抽象类不是必须得有抽象方法,但有抽象方法的类必须是抽象类必须加上abstract
  • 抽象类里面可以有非抽象方法(有body),或抽象方法定义,即abstract void doit();
  • 接口是extends接口,别被坑了
  • 抽象类不可以被直接实例化,只能实例化它的具体继承类
  • 如果写了@Override,却在base class里面找不到这个方法,系统就会出syntax error
  • abstract方法不能有body,非abstract必须有body
  • Abstract_A a = new Extend_A(); 记住这儿Extend_A被称为reference type,而Abstract_A被称为object type,即a中的方法是object type的(使用getClass时,总是得到的右边实例化后的对象的类型,而非定义类型,但内存地址又是reference type的。
  • 只要基类有定义构造函数的,new子类就会自动先去执行基类的构造函数
  • 继承的方法的访问scope必须大于或等于,即cannot reduce visibility of super class,这是JAVA的一个重要思想,就是不能让基类的东西被挡住了
  • 接口可以定义抽象方法,接口里面还可以有具体的变量赋值
  • 有抽象方法的类必须是抽象类,但抽象类不一定要求所有方法为抽象方法 
  • 在JDK6中,抽象类里面可以实例化当前抽象类,但JDK7开始就不行抛异常 Cannot instantiate the type AbstractTry1
  • final和abstract不能同时使用,因为abstract一定要new,地址肯定会变
  • getClass拿到的是=右侧即initializer的类型,而非定义时的类型,即如果 myX = myY; 的话,getClass得到的是myY的类型
  • With overriding, the signatures of two methods are identical but they reside in different classes. 方法的签名是一样的,但链向不同的类
  • 继承类的异常不能比基类抛的异常小 String theString = "Hello World"; System.out.println(theString.charAt(11));
    会抛异常:java.lang.StringIndexOutOfBoundsException
关于数组的坑点集合:
  • 定义二维:int[] array2D[]; int [] [] array2D;
  • 可以这么定义:int[][] array2D = new int[2][];// 因为columns的长度不一定非要一样长
  • {{xx},{yyy}} 这种数组的赋值只能用于初始阶段 (initializer),
    正确:int[] arr = new int[]{1,2}; 
    正确:int[] arr = {1,2};
    错误:int[] arr = new int[2]; arr = {1,2}; // 报编译错:Array constants can only be used in initializers
    错误:int[] arr = new int[2]{1,2};  // int[2] => int[] 报编译错:Cannot define dimension expressions when an array initializer is provided
  • int grades[][] = new int[ROWS][COLS]; // 注意列表
关于数组的其它:
  • Arrays provide constant time random access which is an efficient way of accessing data
  • Arrays are more difficult to insert or remove elements than other data structures
  • An index to an invalid element is possible
关于数组的API:
  • System.arraycopy(s,start1,t,start2,length)(注意:s是原数组,t是目标数组,start1&start2是开始复制下标,length一般是s的长度,由于arraycopy方法不给目标数组分配内存空间,所以必需要先为t分配内存空间!)
  • equals, 是用一维的 deepEquals,是用在多维的
  • toString() 用于一维的,deepToString() 用在多维的,deepToString(object[] o),但对String[]的一堆也等同于toString()
  • 注意Array及ArrayList的API 如arrlist.Remove(xx) 只移除第一个
  • 这个会create a new array based on an existing array arr2 = Arrays.copyOf(arr1, 3); //3是指长度
  • Using the Arrays.copyOfRange method 这个也会create a new array based on a sub-range of elements in an existing array
    arr2 = Arrays.copyOfRange(arr1, 3, 5); // 3 4
  • Arrays.fill(arr1,1,3,5); // [0,5,5,0,0] 而不是[0,5,5,5,0] 这儿注意fill的第二个数值参数是exclusive的
  • 可以用set来改值:lst2.set(0,"Birch")
关于循环的坑:
  • 这段代码是可以编译通过且正常运行的
    55164449
    Java允许无限循环的存在,见这一篇Java无限循环问题, java识别到这是一个无限循环,就不会再让下面有代码了
  • 循环中不可修改循环变量,否则会出compile错,ConcurrentModificationException。如果一定要在过程中变更的话,换成iterator
其它大小不一的坑:
  • java的switch支持32位及以下的,包括Integer(包含byte, char, short, int,但double, long不行), enum, String
  • 重载中不一定需要完全匹配类型,但不能ambiguous,或完全找不到,比如m("00") 匹配m(Object), m(10)如果没有m(int)的话,也匹配m(long),如果m(0.5d)但找不到m(double)而只有m(float)什么的话,就会跑去找m(Object)
    85391920
  • java的数据类型就分为四种,long是整数,所以上一条会匹配long ,以下为32位机,会受机器位数影响
    实数:double (8字节)、float (4字节)
    整数:byte、short(2字节)、int (4字节)、long (8字节)
    字符:char (2字节)
    布尔值:boolean

    还有一些自定义的类型:
    java.math.BigDecimal extends Number
    java.math.BigInteger extends Number
  • 静态变量不管是什么情况下,都是指向的是一个heap地址的啊...实例内的也是,和C#长得不一样,但内存表面在这点上没区别,亲
  • field 是不同于local variable的
  • 这一题Q94的这一句求解释 for ( expr1 ; expr2 ; expr3 ) { statement; } When expr2 evaluates to false, the loop terminates. It is evaluated only after each iteration through the loop。
  • 这样可以的:ArrayList<Integer> t3 = new ArrayList<>(1);
  • int a,b,c=10; 这种情况只有c被赋值了,但仍能编译通过
  • 同一变量名可以被定义为不同类型,如String str = null; int str = 1;
关于异常的坑:
  • 除非遇到System.exit(0), 否则无论有没有try或有没有进入到catch,都会执行finally
  • 类似c#的using的 try(xxx) {}中的xxx所涉及的类必须实现了autoclosable接口
  • catch (java.io.IOException | java.io.FileNotFoundException e)
  • 这样会出错,提示后者已经在前者是存在,反之一样,所以|分隔的exception不可以有继承/实现关系
  • catch block不是必须得有的,try只要有catch或finally都可以
  • 实现它的类的Exception不能比基础定义中的大,这个很容易想,就是不能超出定义的范围
  • The catch clause argument is always of type Throwable 
关于API的坑:
  • Java有哪些exception? 以下是RuntimeException
    ArithmeticException, ArrayStoreException, BufferOverflowException, BufferUnderflowException, CannotRedoException, CannotUndoException, ClassCastException, CMMException, ConcurrentModificationException, DOMException, EmptyStackException, IllegalArgumentException, IllegalMonitorStateException, IllegalPathStateException, IllegalStateException, ImagingOpException, IndexOutOfBoundsException, MissingResourceException, NegativeArraySizeException, NoSuchElementException, NullPointerException, ProfileDataException, ProviderException, RasterFormatException, SecurityException, SystemException, UndeclaredThrowableException, UnmodifiableSetException, UnsupportedOperationException
  • StringBuilder的delete的end可以是超过length()的数值,而且不会出错 StringBuilder sb = new StringBuilder(); sb.append("test"); sb.delete(0, sb.length()+1222);
  • Arrays.sort(arr,null); 表示comparator为null则用默认的自然排序
  • Arrays.asList(T...); 这个在学Arrays时可没有留意到呢!
  • ArrayDeque extends Deque (这个好高深喔!)文章一
    14610507
  • arr1.toString() 得到的字符串使用到的方法:Integer.toHexString(hashCode())
posted @ 2013-07-14 23:34  Elaine Shi  阅读(981)  评论(0编辑  收藏  举报