`

《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
   
分享到:
评论

相关推荐

    java语言精粹

    ### Java语言精粹之异常处理 #### 引言 在《Java语言精粹》这本书中,作者Jim深入探讨了Java语言及其环境中的诸多亮点。其中一个关键主题是异常处理。异常处理是Java编程的一个核心概念,它对于确保程序的健壮性...

    疯狂java讲义精粹(第2版)

    《疯狂Java讲义精粹(第2版)》是一本深度剖析Java编程语言的专业书籍,其源代码包含在提供的压缩包文件中。这本书的核心目标是帮助读者深入理解Java语言的本质,提升编程技能,并掌握实际开发中的关键技术。以下是该...

    JavaScript语言精粹完整版

    《JavaScript语言精粹》作为一本深入浅出讲解JavaScript编程语言的经典之作,对于想要提升自己JavaScript技能的开发者来说,无疑是一份宝贵的资源。本书由Douglas Crockford所著,他不仅是JSON格式的创始人,也是...

    疯狂Java面试题(疯狂Java讲义精粹附赠).pdf

    "疯狂Java面试题(疯狂Java讲义精粹附赠).pdf" 本资源是关于Java面试题的集合,涵盖了Java核心技术部分的面试题,包括Java基本语法、面向对象、Java常用API、Java集合框架、注解、泛型、输入/输出、多线程、网络...

    JAVA案例精粹150例

    JAVA案例精粹150例.pdf JAVA案例精粹150例.pdf

    javascript语言精粹 pdf

    《JavaScript语言精粹》是JavaScript开发者必读的经典之作,它深入浅出地讲解了JavaScript语言的核心概念和实用技巧。这本书不仅适合初学者作为入门指南,也适合经验丰富的开发者用来巩固和提升自己的技能。 首先,...

    疯狂Java讲义精粹(第二版)

    本资源主要聚焦于Java语言的实践应用,特别是通过代码示例来阐述关键的设计模式和项目实践。 在压缩包中,"Java设计模式(疯狂Java联盟版).chm"是一个帮助文件,它详细介绍了Java设计模式。设计模式是软件开发中的...

    JavaScript 精粹读书笔记(1,2)

    在《JavaScript 精粹》一书中,作者深入探讨了JavaScript的特性和最佳实践,旨在帮助开发者区分和利用其精华部分,同时避免那些可能会引起问题的鸡肋特性。 首先,JavaScript的某些特性带来了麻烦,因为其规范的不...

    JavaScript语言精粹_修订版.pdf

    JavaScript语言精粹_修订版.pdf

    疯狂Java讲义精粹随书光盘

    光盘中的"疯狂Java讲义精粹.iso"文件很可能是该书的完整电子版,以ISO镜像格式提供,方便用户在计算机上阅读或刻录到光盘上。 在Java编程领域,以下几个核心知识点是《疯狂Java讲义》可能涉及的: 1. **Java基础**...

    uml精粹笔记

    **UML精粹笔记概述** UML(统一建模语言)是软件开发中的一种标准化建模工具,用于可视化、理解和构建复杂系统。本笔记主要涵盖了UML的核心概念和图形表示,包括类图、时序图、对象图、包图、部署图、用例、状态机...

    JavaScript 语言精粹

    《JavaScript语言精粹》这本书深入浅出地探讨了这一语言的核心概念和技术,旨在帮助开发者更好地理解和掌握JavaScript的精髓。 首先,JavaScript的核心特性包括弱类型、动态类型、基于原型的对象模型以及函数作为...

    Java案例精粹150例 高清完整版

    该压缩包包含了一份PDF文档,名为“Java案例精粹150例.pdf”,很可能包含了150个精心挑选的Java编程实例,涵盖了Java语言的核心特性、标准库的使用、面向对象编程原则以及常见的设计模式等众多知识点。以下是可能...

    《JavaScript语言精粹》.txt

    JavaScript语言精粹

    《JavaScript语言精粹》.pdf

    通过阅读《JavaScript语言精粹》这本书,无论是初学者还是有经验的开发者,都可以深入理解这些概念,提升自己的JavaScript技能。书中的实例和解释将帮助读者更好地掌握JavaScript的实际应用,并在实际项目中运用这些...

    Java案例精粹150例(源代码)_java_

    2. **面向对象编程**:Java是一种面向对象的语言,案例中会涉及类的继承、多态、封装和接口。理解这些概念有助于设计出更高效、可维护的代码。 3. **异常处理**:Java异常处理机制是程序健壮性的重要组成部分。案例...

Global site tag (gtag.js) - Google Analytics