`

《Java语言精粹》读书笔记(1)

    博客分类:
  • Java
阅读更多
   这是一本只讲Java优点的书。
    Jim Waldo先生是原Sun微系统公司实验室的杰出工程师,他亲历并参与了Java从技术萌芽、发展到崛起的整个过程。


   类型系统
    类、抽象类和接口。
    用接口定义一种类型,再用一个类实现此接口,这样,要调用执行调用的性能损失是程序的设计者们不愿付出的。在早期的Java解释器中,可接受的性能是人们真正关心的,但即使在那时不通过接口的直接调用也只是一个微乎其微的优化,没有实际意义。考虑到如今的即时编译(just-in-time)编译器的性能,这种反对意见已经完全没有意义了。
    关于效率,另一种反对意见在于接口导致系统中的信息重复,需要程序要录入更多的代码。集成开发环境(Integrated Development Environment,IDE)大大缓解。
   方法参数和返回值的声明中,接口的概念第一次显得重要起来。编辑器要求传入方法或从方法中返回的对象必须符合声明的类型。这些对象的精确类型不必与声明的类型完全一致;编译器只要求它们“至少是”那种类型。多态。
    接口的定义可以使实现任务交给可以信任的其他团队。
    对象的接口实际上就是用户接口,只不过用户是程序员。它理清了整个系统设计的方法。
   Java的接口不能区别同类方法;还有一个关于类加载器(classloader)的问题。但这两种情况都很少遇到。


   异常
   The Why
    The reasoning behind the exception mechanism in Java is pretty simple. Bad things can happen, even to good programs. When such a bad thing happens, there is a need to react to it, either by figuring out some other way to accomplish what was trying to be done when the bad thing happened, by cleaning up and gracefully exiting, or by abort-ing the operation in which the bad thing happened and trying something else. But the code that deals with the problem shouldn't be mixed in with the code that is used when bad things don't happen, or the program can rapidly become unreadable. In addition, programmers are probably better off if they have to deal with problems as close as possible to the time and place that they occur.
    The design also ties into the Java type system. Since an exception is an alternative return value, it forms part of the signature of the method. Not declaring an exception would be like not having to declare the type of the return value. It would be very difficult to write predictable code in such a situation, and it would be difficult for the compiler to help you find mistakes.
    Finally, the exception mechanism allows a program to separate the mainline code and the error-handling code.
   Use and Abuse
 
   
      try{ 
        ... 
          method1(); 
        ... 
          method2(); 
      ... 
      } catch (Type2Exception e){ 
          //do something to recover 
      } catch (Type1Exception e) { 
          //do something else to recover 
      } 

    Take care
    Exception next type
    We can use the RuntimeException,but you should do everything in your power to warn users of your code that this ex-ception might be thrown, the circumstances under which it will be thrown, and what the caller might to do avoid the exception or deal with it if it is thrown. It isn’t the perfect solution, but it is the best that is open to you.
    Example
 
    try {
        ...
    } finally {
        if (resultSet != null) try { resultSet.close(); } catch (SQLException logOrIgnore) {}
        // will go on to do after catch
        // tack care the difference with the former example
        if (statement != null) try { statement.close(); } catch (SQLException logOrIgnore) {}
        if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {}
    }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics