菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
370
0

读Java 804 - Quick refresher

原创
05/13 14:22
阅读数 26724
  • Upcast永远是成功的,但Downcast不是,记得做instanceof判断
  • 仅抛不同异常,而返回值相同的重载是不可以的
  • static import只会import静态类
  • static method内不能使用this和super,不能用super!
  • 只有static nested class能有静态成员,其它nested不能有,且静态嵌入类可以访问外围类的私有成员但仅限于静态成员
  • 不可以在enums前使用new(因为其构造函数是私有的)
  • 枚举默认且只能为public static final,所以不能继承
  • 接口里面的变量必须被初始化,而且是final的
  • 接口里面不能有BODY,全部为public abstract
  • 接口可以有nested interface,这时的内嵌的接口可以为private/protected or public
  • is-a (实现/继承) is-like-a(接口),has-a(组合)
  • 泛型不支持协变,again, List<SuperT> lst = new List<T>() 会出错
  • <? extends T> 指必须在T的scope内,相当于c#的(?), <? super T> 指的是T必须为继承了T,比如<? super Path>
  • 没有泛型异常这回事,即没有这种东西 -> GenericExeption<T> extends Throwable
  • 泛型类型不可以为primitive
  • 使用HashSet/HashMap时要重写hashCode()和equals()
  • 关于泛型呢,因为是type erasure所以没有预先定义这回事,所以静态泛型(static T xxx) 是不存在的,
  • %o: octual 八进制  %x: hexadecimal 16进制
  • Watch service不会递归watch子目录,需要自己resursively register watch key
  • If the JDBC API is not able to locate the JDBC driver, it will throw a SQLException. If there are jars for the drivers available, they need to be included in the classpath to enable the JDBC API to locate the driver

  • ResultSet一开始游标指向的是第一条前面,所以必须运行.next()拿到第一条

  • RowSet是种特殊的支持JavaBean组件的ResultSet
        JdbcRowSet: 
        CachedRowSet: disconnected ResultSet
            WebRowSet: CachedRowset + XML (没有说这个是JSON格式喔)
                JoinRowSet: WebRowset + SQL join
                FilteredRowSet: WebRowset + filtering

  • JDBC 4.1 introduces the capability to use try-with-resources statement to close resources (Connection, ResultSet,and Statement) automatically.

  • try-catch的异常如果类似而且处理方式也类似的话,可以考虑使用multi-catch块

  • 即使在catch中return了,也还是会执行到finally的,除非执行的是system.exit(n), 因为这个是停掉所有包括其它的

  • static initialization block cannot throw any checked exceptions. now-static initialization blocks can. however, all the constructors should declare that exception in their throws clause

  • 重写的方法中throw的异常只能比父类的异常更具体,而不能更宽泛,或者不throws也是可以的

  • 如果方法在实现的多个接口中都存在且抛出的异常不同,则实现的方法应该同时抛出这些异常

  • 自定义exception最后继承于Exception或RuntimeException,而不要直接继承Throwable,这个是JVM预留的

  • bundlename_language_country_#script.properties

  • Format (抽象类)
        NumberFormat
        DateFormat - 大写W/D/F(day of week)的是in year, 小写w/d/f的是in month,大写S是毫秒,大写K是am/pm小时数,小写k是hour (1-24), H是(0-23)
            SimpleDateFormat

  • sleep(): 不会release the lock,会hold on to the lock

  • join(): wait for another thread to terminate

  • 仍然不懂的:


  • static method 不可以被override

  • 接口里的方法只能为public & abstract,否则会出编译错“Illegal modifier for the interface method say; only public & abstract are permitted”

    接口里面的field只能为public, final & static,否则会出编译错“Illegal modifier for the interface field Foo.iVisual; only public, static & final are permitted”

  • 前期绑定和后期绑定

  • static block: A constructor will be invoked when an instance of the class is created, while the static block will be invoked when the program initializes

  • 抽象类可以有静态类
  • static的目标不可以使用this,因为它不属于任何一个实例
  • 静态方法里面不可以使用super.StaticMethod(),既然它不可以被继承,也不能使用super了
  • nested class:
    如果内嵌方法是static的,则使用new A.B()
    如果外围方法是static的,则使用new A().B()
    new A().B().C();
    抽象类中的内嵌方法,因为没有实例,所以可以直接A.new B()或A.this.method();
    记住实例方法一定要有实例,否则要new()
    没有static外围类这回事内部类自动拥有对其外围类的所有成员的访问权, 如果内部类和外部类成员的名字完全相同,则在内部类方法中要访问外部类成员(即使是private的),
    内部类必须声明为protected, default, public才对外围类可见(private则会编译错显示为is not visible),
  • a. 内部类可以访问private
    b. 内部类成员可以与外围类同名
  • 内部类不可以有static变量但可以有常量即static final
  • 内部方法类的参数只能为final
  • 匿名类一定是定义在实例化后的对象内的,即 Foo f = new Foo(){ public void say(){ .. } }; 或Foo f {return new Foo(){ public void say(){};}}
  • 匿名类没有显式构造函数,也不能有构造函数
  • Enum类型的constructor只能被定义为private
  • Enum类型的enum常量的定义必须是在第一行...
  • Enums被隐式定义为public, static, final,不可被继承
  • 所有enum都会被转换为类:所以TryEnums.SomeEnum.valueOf("TEST1").getClass()
  • 所有enum类型都继承于java.lang.Enum,每个enum元素都会被转换为enum类的常量,见enum定义 TryEnums.SomeEnum et = TryEnums.SomeEnum.TEST1; 
  • 接口内部类自动都是public static的,相当于为接口定义了一种变量类型

  • TryEnums.SomeEnum et = TryEnums.SomeEnum.TEST1;

    System.out.println(et.TEST2); // et定义为一个静态类的常量后,仍然可以用et.TEST2来取到TEST2的常量

  • 因为enum是final static public的,所以不能定义在local inter class中

  • FileWriter fw = FileWriter(path, boolean isAppend)
    fw.append("test001");
    fw.close();
    如果没有第二个boolean参数,默认为覆盖写。

  • A setAutoCommit (False) method invocation starts a transaction context.

  • 看一下正则表达式
    \\s* matches 0 or more occurrences of whitespaces.

  • ScheduledExecutorService

  • The method used to obtain the Executor determines how many Threads are used to execute tasks.

  • 看集合类图,象Q144

  • Class Hashtable extends Dictionary implements Serializable, Cloneable, Map。

    Interface SortedMap extends Map

  • Change FileReader to BufferReader.

    public class BufferedReader extends Reader

    Read text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.

    The buffer size may be specified, or the default size may be used. The default is large enough for most purposes.

  • ThreadLocalRandom.current().nextInt(1, 101);   

      发表评论

      0/200
      370 点赞
      0 评论
      收藏