Thinking in Java - Study Notes - (2) Everything is an object

Java Programming Ideas - Chapter 2 - Everything is an Object

Although Java is based on C++, Java is a more "pure" object-oriented programming language by comparison.

Manipulate objects with references

Although everything is seen as an object, the manipulated identifier is actually a "reference" to the object.

Think of this situation as manipulating a TV (object) with a remote control (reference).

A reference does not necessarily need to have an object associated with it.

Even without a TV, the remote control can stand alone.

All objects must be created by you

where to store

When the program is running, how are the objects arranged? In particular, how is the memory allocated?

  1. register . Fastest memory area because it is located inside the processor. But the number of registers is extremely limited, so registers are allocated on demand. There is no direct control, nor any indication of the existence of the register being felt in the program.
  2. stack . Located in general purpose RAM (random access memory), but with direct support from the processor via the stack pointer . If the stack pointer moves down, new memory is allocated; if it moves up, that memory is freed. When creating a program, the Java system must know the exact lifetime of all items stored on the stack in order to move the stack pointer up and down. Some Java data is stored on the stack - especially object references, but Java pairs are not stored there.
  3. heap . A general-purpose memory pool (also in RAM) that holds all Java objects. The compiler does not need to know how long the stored data will live on the heap.

    new stores objects in the "heap".

  4. Constant storage . Constant values ​​are usually stored directly inside the program code.

  5. Non-RAM storage . Two basic examples: stream objects and persistent objects

Special Case: Basic Types

basic type size minimum maximum value wrapper type
boolean Boolean
char 16-bit Unicode o Unicode 2 16 1 Character
byte 8 bits -128 +127 Byte
short 16 bits 2 15 + 2 15 1 Short
int 32 bits 2 31 + 2 31 1 Integer
float 32 bits IEEE754 IEEE754 Float
double 64 bits IEEE754 IEEE754 Double
void Void
  • All numeric types have a sign.
  • The size of the storage space occupied by the boolean type is not explicitly specified, only defined as being able to take the literal value true or false .
  • BigInteger supports arbitrary precision integers.
  • BigDecimal supports fixed-point numbers of any precision.

Arrays in Java

  • When you create an array of objects, you actually create an array of references, each of which is automatically initialized to null .
  • When creating an array to store basic data types, set all the memory occupied by the array to zero.

Objects never need to be destroyed

scope

Variables defined in a scope are only available until the end of the scope.

object scope

    {
        String s = "a string";
    } //End of scope

The reference s disappears at the end of the scope. However, the String object pointed to by s continues to occupy memory space.

Objects created by new persist as long as they are needed.
Java has a garbage collector that frees memory space for objects that will no longer be referenced.

static keyword

Generally speaking, when you create a class, you are describing the appearance and behavior of objects of that class. Unless you create an object of that class with new, you don't actually get any object. When new is executed to create an object, the data storage space is allocated and its methods are called by the outside world.

There are two situations that cannot be solved by the above method:

  1. Just want to allocate a single storage space for a specific domain, regardless of how many objects the space will create, or even no objects at all.
  2. It is desired that a method is not associated with any object of the containing class. That is, this method can be called even if the object is not created.

This need can be met through the static keyword.

class StaticTest {
    static int i = 47;
}
    StaticTest st1 = new StaticTest();
    StaticTest st2 = new StaticTest();

Here, st1.i and st2.i point to the same storage space.

Using the class name is the preferred way to refer to static variables, and similar logic applies to static methods.

    StaticTest.i++;

Although when static acts on a field, it definitely changes the way data is created (because a static field has only one storage space for each class, while a non-static field has one storage space for each object), But if static acts on a method, the difference is not so great. An important usage of static method is to call it without creating any object.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325199740&siteId=291194637