Notes Of Thinking In Java (1)

来源:互联网 发布:linux下搭建ftp服务器 编辑:程序博客网 时间:2024/04/28 03:29

As everyone knows, English is very important for a programmer, because most outstanding program books are written in English. Thus, when I read a program book, I select the English edition. Though I am not so skilled at English that read the English edition smoothly, it is significant to do that. Well, these days I am reading the Thinking In Java(Fourth Edition) and here are some notes of the previous six chapters.

Where storage lives

There are five different places to store data:

  • Registers;

  • The stack;

  • The heap;

  • Constant storage;

  • Non-RAM storage;

Special case: primitive types

the difference between using new and not using new:

using new:the variable created by new is placed on the heap

not using new:the variable created not using new holds the value directly, and it’s placed on the stack

Default values for primitive members

When a primitive data type is a member of a class, it is guaranteed to get a default value if you do not initialize it:

  • boolean false

  • char ‘/u0000’ (null)

  • byte (byte)0

  • short (short)0

  • int 0

  • long 0L

  • float 0.0f

  • double 0.0d

aliasing:

when you assign primitives, you copy the contents from one place to another;

Whenever you manipulate an object, what you’re manipulating is the reference, so when you assign “from one object to another,” you’re actually copying a reference from one place to another.

Cleanup: finalization and garbage collection

  • The garbage collector only knows how to release memory allocated with new.

  • Your objects might not get garbage collected.

  • Garbage collection is not destruction.

  • Garbage collection is only about memory.

  • When the garbage collector is ready to release the storage used for your object, it will first call finalize(), which Java provides, and only on the next garbage-collection pass will it reclaim the object’s memory.

Enumerated types

  • Although enums appear to be a new data type, the keyword only produces some compiler behavior while generating a class for the enum, so in many ways you can treat an enum as if it were any other class. In fact, enums are classes and have their own methods.

  • enums can be used inside switch statements.

package: the library unit

  • Each compilation unit must have a name ending in .java, and inside the compilation unit there can be a public class that must have the same name as the file (including capitalization, but excluding the .java file name extension). There can be only one public class in each compilation unit; otherwise, the compiler will complain. If there are additional classes in that compilation unit, they are hidden from the world outside that package because they’re not public, and they comprise “support” classes for the main public class.

conditional compilation

  • Since Java is intended to be automatically cross-platform,C’s conditional compilation is missing from java.

Java access specifiers

  • private

    no one can access that member except the class that

    contains that member, inside methods of that class .

  • Package access

    all the other classes in the current package have access to that member, but to all the classes outside of this package, the member appears to be private.

  • protected

    The derived classes and the classes in the current package have access to that member.

  • public

    the member declaration that immediately follows public is available to everyone, in particular to the client programmer who uses the library.

The final keyword

  • final data

    final makes the value a constant or makes the reference a constant. The confusable thing is once the reference is initialized to an object, it can never be changed to point to another object. However, the object itself can be modified; Java does not provide a way to make any arbitrary object a constant .

  • final methods

    The first reason for final methods is to put a “lock” on the method to prevent any inheriting class from changing its meaning. The second is efficiency.

  • final classes

    You cannot inherit from a final class.