考试的宗旨仍然是掌握基础知识,不过鉴于Oracle的这个认证考试还有很多的你们来找茬的成份在,因此必定必定要细心为上。 java
关于抽象类的坑点集合: express
- 抽象类不是必须得有抽象方法,但有抽象方法的类必须是抽象类必须加上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")
关于循环的坑:
- 这段代码是能够编译经过且正常运行的
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)
- 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 (这个好高深喔!)文章一
- arr1.toString() 获得的字符串使用到的方法:Integer.toHexString(hashCode())