Thinking in Java Chapter II study notes

Source: Internet
Author: User

Java is based on C + +, but in contrast, Java is a more purely object-oriented programming language.

In the Java world, almost everything is an object, and all of Java's work is defining classes, producing objects of those classes, and sending messages to those objects.

Although everything is called the object, but we really manipulate the object is actually a reference, like the TV for the object, the remote control is a reference, if you want to walk around the room to control the TV, just take the remote control this reference, do not have to bring the object of television. In addition, a reference can exist separately, and there is no need for an object to be associated with it. If you create a string s, a string reference is created, but if you send a message to the reference, the error is returned because S is not associated with any object. Therefore, it is more secure to create a reference and initialize it at the same time. When we create a reference, we really want it to be associated with a new object, that is, the instantiation of the object.

  This chapter focuses on:

  1, the program runs, the specific memory allocation of the following five kinds of

1) Register: The fastest storage area, located inside the processor, a limited number, can only be automatically allocated according to demand, cannot be controlled, the program does not see any signs of it. (c, C + + can provide the compiler with a way to allocate registers)

2) Stack: In general RAM (random access memory), through the stack pointer move up and down to achieve the release and allocation of memory. The Java system must know all the life cycles of the data stored in the stack so that the pointer moves up and down. The flexibility of the program is very limited. The object reference exists on the stack, and the object exists in the heap.

3) Heap: Also located in RAM, a common memory pool. For storing all Java objects, the benefit of heaps different from stacks is that the heap does not need to know the life cycle of stored data, greatly increasing flexibility. Cost: The cleanup and storage allocation takes more time.

4) Constant Storage: Constant value is usually stored inside the program, but in the embedded system, the constant itself is separated from other parts of the turn, so it is selected to be stored in read-only memory.

5) Non-RAM storage: If the data exists entirely outside of the program, it will not be controlled by any program, and can exist if the program is not running. Two examples: Stream objects convert objects into byte streams, send to another machine, and persist objects on disk in a persisted object.

  2, the size of the basic type of storage space

Fixed size for each base type. Unlike most languages, this fixation is an important reason for Java portability because of changes in the hardware architecture of the machine.

  3. Never Destroy objects

The garbage collector in Java always monitors all objects created with new and identifies objects that are no longer referenced, and then frees up space for new objects to use.

  4. Variable initialization

When a variable is a data member of a class, Java gives its default value, ensuring that it is initialized to prevent program errors. For other variables that are not members of a class, they are not automatically initialized and must be assigned a value.

  5. Static

Generally speaking, when creating a class, the situation describes the appearance and behavior of the object of that class. Unless you create the object of that class with new, you do not actually get any objects. The data storage space is allocated only when you execute new to create the object, and the method is called by the outside world. There are two other situations where the above approach cannot be solved by simply allocating a single storage space for a particular domain without considering how many objects to create, or even creating any objects at all. Another scenario is that you want a method not to be associated with any object of the class that contains it. These two needs are addressed through the static keyword. When declaring a thing to be static, it means that the field or method is not associated with all the objects of the class that contains it. That is, you can call this static or static domain even if you do not create any objects of this class.

  6. The difference between heap and Stack in Java

Heap: Mainly used for storing objects, the access speed is slow, can be run dynamically allocated memory, the life cycle does not need to determine in advance.

Stacks: Variables that are primarily used to hold basic data types, and reference variables for objects, are automatically freed when the variables are scoped. The memory management of the stack is realized by the "LIFO" mode of the stack. Fast access, size and life cycle must be determined and inflexible.

  

Thinking in Java Chapter II study notes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.