`

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

    博客分类:
  • Java
阅读更多
    Package
    Like most object-oriented languages, Java allows the programmer to declare who can access what parts (if any) of an object. As befits its C++ heritage, Java allows fields and methods to be accessed by any part of a class in which those fields and methods are declared. But outside of an object, access is defined by the access control modifier associated with the field or method. Fields or methods that are labeled private can be accessed only from within the defining class. Those that are labeled protected can be accessed either by other parts of the class or by any class that extends the defining class. Finally, those that are marked public may be accessed by anyone. This much is familiar to those who come to Java from C++.
    This gives a hierarchy of access possibilities for the programmer. At the most restricted are those fields and methods marked private, which can be accessed only from within the class in which they occur. Next most restrictive are those with no declared access specification. These have package access, which makes them available to anything that is in the same package, but keeps them from the prying eyes of anything in any other package. The next level of access, protected, loosens the restrictions on package pro-tection to include any classes that are extensions of the class in which the method or field are defined, no matter where in the set of packages those extensions are defined.Finally, there are those methods and fields that are marked as public, which can be accessed from anywhere
    Classes and interfaces are also subject to access specifications. However, with the ex-ception of inner classes, the possible access specifications for these parts of the language are limited to either package access (in which case the class or interface are not labeled) or public access. A little thought convinces one that these are the only access specifiersthat make sense. A class or interface that can be called only by itself is not very inter-esting. Neither is one that can only be called by subclasses. Although from a purely linguistic point of view, this lack of symmetry may be troubling, the fact that the lan-guage keeps you from doing something useless more than makes up for it.
    You can also put some access specifications on the methods defined in an interface,but what you do here doesn’t really matter. Only two access specifiers are legal for interface methods. You can mark an interface method as abstract, but doing so has no effect, since all interface methods are unimplemented at the level of the interface and are instantiated only in classes that implement the interface. Likewise, you can mark an interface method as public, but this is documentation at best; an interface method is accessible to any code that can access the interface. If the interface is marked as public, then all of the methods of that interface are public, even if that access specifier does not preface the method declaration. If the interface has only package visibility, then the methods in that interface will also have only package visibility.

   关于异常应该放在接口的包还是实现的包
    这可以根据自己风格决定,作者采用放在接口实现的包中,虽然在接口中暴露了实现细节。


垃圾回收和引用
    对程序员来说,垃圾回收尽管是不可见的,但垃圾回收的诸多效果却在语言中处处留下了印记,其中最明显的就是Java中没有指针,而处处使用引用。使用引用而不是指针,不允许对引用做算术运算,这使Java环境避免了C和C++语言中因允许这类操作而导致的大量bug和安全弱点。
    虽然垃圾回收将程序员从内存管理的任务中解脱出来,但程序中还会用到其他资源,而它们的确需要显示地管理。比如文件句柄和网络套接字(socket),这些都是程序中会用到不再需要时又要释放的稀有资源(其稀缺性取决于底层操作系统)。与内存不同的是,Java环境并未对这种资源的管理提供帮助。
finalizer在垃圾回收前被jvm调用,但更具jvm特性不定时调用,不要依靠finalizer将稀缺资源的管理和内存管理绑定,交由垃圾回收处理。因为往往稀缺资源比内存资源更加稀少,和内存同步不是一个好方法。更重要的是,finalizer的调用是不稳定的。

Java虚拟机
    人们关注的大多数虚拟机都是在很低的层次上进行虚拟化。这些虚拟机在裸机硬件和操作系统(OS)之间提供了一个软件层,这个然间层向OS提供了一组调用,使系统可被引导,并通过单一的软件接口与硬件交互。比如VMVare(包括服务器和桌面)、Xen、VirtualBox等。
    Java虚拟机是一种与之类似的抽象机制,只不过他在更高的层次上实现。JVM并不向操作系统提供一套表示硬件的接口和抽象,而是向程序员提供了硬件和操作系统的整体抽象。也就是说,虚拟机提供的不只是一堆硬件的抽象视图。计算机的所有功能,包括从文件系统、网络到线程,都作为编程环境的一部分提出来。

   即时编译(just-in-time)在运行时即时地将原本解释执行的字节码编译成本地代码。这类编译器最有趣的地方在于:某段代码使用得越频繁,编译出的代码优化程度越高。
    “一次编译,四处运行”这里能够四处运行的目标代码,而不是源码。以前,编写可移植性代码一位这要在代码中添加大量的#define和#ifdef,使用可以确定的环境并产生实际的makefile。
     可移植性并不意味着编程时的肆无忌惮,注意File.separator等写法。

Javadoc
    Java有第三种注释方法,这种发放可以被Javadoc提取。可以放在包,类,方法和属性前。其中,放在包前需要使用类的全路径。
    {@link Batter}
    {@inheritDoc }
    @return
    @param

    javadoc details:
    http://www.oracle.com/technetwork/java/javase/documentation/index-137868.html
   
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics