`
taimukang
  • 浏览: 180926 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

java基础知识备忘拾遗

阅读更多
一、接口

1.接口中的方法默认都是public和abstract的,不管是否显式声明。
2.接口可以继承接口。
3.接口中的变量都是 public,static和final的
4.接口中不能声明静态方法

二、枚举

1.枚举类型默认继承了java.lang.Enum类,因此不能在继承其他类或enum。
2.不要实例化一个enum,因为枚举类型中的内容都是public,static和final的,没有理由去创建一个枚举类型的实例。
3.枚举类型的values()方法返回一个包含其所有内容的数组,对于枚举类型中的每个元素,ordinal()方法可以获得该元素的序号(从零开始)
4.enum中可以定义方法和数据结构,但是必须在enumeration list之后。

三、嵌套类

1.java中有四种类型的嵌套类:成员内部类(member inner class)、本地内部类(local inner class,在方法内部定义的)、匿名内部类(在方法内部定义的没有名字的local inner class)、静态嵌套类(Top - level inner classes are static inner classes that are nested at the member level of a class.)

2.内部类与其外部类是联系在一起的,不能在不依赖其外部类的情况下实例化内部类

3.成员内部类:A member inner class is defi ned at the member level of a class
(the same level as fields,methods and constructors).
  (1).成员内部类可以是public或protect或private或默认的访问权限
  (2).成员内部类可以继承任何类或实现任何借口
  (3).成员内部类可以是abstric或final的
  (4).成员内部类中不能定义静态变量或方法
  (5).最重要的是,成员内部类可以访问其外部类的所有成员,包括私有成员

4.Local inner classes
  (1)Local inner classes do not have an access specifier.
  (2)Local inner classes cannot be declared static , nor can they declare static fields or methods.
  (3)Local inner classes have access to all the fields and methods of its enclosing class(包括private的).
  (4)A local inner class does not have access to the local variables of method unless those variables are final .

5.匿名内部类
  (1)An anonymous inner class must either extend an existing class or implement an existing interface.
  (2)The difference with anonymous inner classes is that you can create multiple instances of a local inner class within the method, but an anonymous inner class can only be instantiated one time.

6.静态嵌套类
  (1).静态嵌套类不是内部类,它不能访问包含它的类的字段(field)和方法,可以不依赖其外部类(这样表达可能不够准确,因为既然不是内部类,也就无所谓外部类)的实例而被实例化。
  (2).静态嵌套类与一般的类没有太多区别,除了:
   --The nesting creates a type of namespace. To denote a nested class from outside its enclosing class, the nested class is prefixed with the name of the enclosing class (similar to how static fields and methods are accessed).
   --Access to the nested class can be controlled by an access specifier. For example, a nested class declared as private can only be used within its enclosing class, in effect hiding it from any other classes.
   --The enclosing class has access to the fields and methods of the nested class, even the private ones.


四、异常

1.java.lang.Throwable 是所有异常类的父类,其直接子类包括java.lang.Exception和java.lang.Error。

2.可以用多个catch语句块捕获异常,如果异常被一个catch块捕获,则后面的catch语句块将被忽略;如果一个catch块中的异常类型是前面catch语句块中异常类型的子类,程序将出现编译错误。

3.异常类型
    Runtime exceptions:异常类型为java.lang.RuntimeException或其子类。java.lang.RuntimeException是java.lang.Exception的直接子类。
    Checked exceptions:异常类型为java.lang.Exception,但不是java.lang.RuntimeException的子类。这类异常必须被处理(通过try...catch捕获或者通过throws抛出)。
    Errors:类型为java.lang.Error。通常是由程序外部原因导致的错误。
    理论上,所有类型的异常都可以被捕获,包括Error,但是通常捕获Error是非常困难或不可能的;而捕获runtime exception对程序设计是非常不利的,runtime exception可以通过代码优化而避免,如果捕获并处理,将无法发现程序中存在的问题,是一种隐患。

4.A try statement can contain a finally block without any catch clauses

5.也有finally语句不被执行的可能:如果try抛出了异常被catch捕获,并且在catch块中调用了System.exit终止了JVM运行,则finally语句块不被执行。

五、String

1.JVM将String对象实例保存在一个特殊的内存区域(String pool)中,可以共享。如:
    String one = “today”;
    String two = “today”;
    if(one == two)
        System.out.println(“true”);
    else
        System.out.println(“false”);
    输出是true。
2.String对象比较时,应使用equals方法,因为并不是所有的String object都在String pool中。如:
    String one = “today”;
    String three = new String(“today”);
    if(one == three)
        System.out.println(”true”);
    else
        System.out.println(“false”);
    The one reference points to a String object in the string pool while the three reference points to a dynamically created String object on the heap, so one and three do not point to the same instance and the output of the previous code is ”false”.

3.StringBuffer与StringBuilder二者方法完全相同,区别在于StringBuffer的方法是线程安全的,StringBuilder的方法是非线程安全的。

4.当需要对字符串进行频繁的连接操作时,使用StringBuffer或StringBuilder效率更高。因为在对String对象进行连接操作时,JVM会创建对应的临时StringBuffer对象来进行操作。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics