20190814 On Java8 Chapter III Everything Is an Object

Chapter III Everything Is an Object

Object Creation

data storage

There are five different places to store data:

  1. Register (Registers) the fastest storage area, located inside the CPU. No direct control.

  2. Memory stack (Stack) is present in the conventional memory RAM (Random Access Memory, Random Access Memory) area, the processor may be directly supported by the stack pointer. Allocate memory stack pointer down, move the release of memory, which is a rapid and efficient method for allocating memory, only to speed register. When you create a program, Java system must know exactly saved in the stack of the life cycle of all items. This constraint limits the flexibility of the program. Thus, although there are some data in Java on the stack memory, in particular, object references, but Java object is stored in the heap memory.

  3. Heap memory (Heap) which is a common pool of memory (also in the RAM area), all Java objects are present therein. With different memory stack, the compiler does not need to know how long the object must stay on the heap. Therefore, the heap memory to save data with more flexibility. When you create an object, just use the new command can instantiate objects, when executing code automatically in the heap memory allocation. This flexibility comes at a price: heap memory allocation and cleanup than the stack memory needs more time. Over time, Java heap memory allocation mechanism is now very fast, so this is not a problem worthy of concern.

  4. Constants memory (Constant storage) is generally constant values ​​directly in the program code, because they will never change.

  5. Non-storage RAM (Non-RAM storage) is present in the data is completely outside the program, still exists and the program is not running out of program control. Two main examples: (1) serialized object: the object is converted into a byte stream, generally sent to another machine; (2) persistent object: the object is placed on a disk, even if the program is terminated, data is still presence. These objects are stored in a manner to dump another medium, into conventional recovery and if necessary, based on the object RAM. Java provides support for lightweight persistence. The JDBC and Hibernate libraries such as these provide a more complex support for the databases to store and retrieve objects information.

The basic types of storage

The method for creating basic types, Java and uses the C / C ++ the same strategy. In other words, instead of using a new variable is created, but the use of a "automatic" variables. This variable is stored directly, "value", and placed in the stack memory, and therefore more efficient.

Numerical precision

In Java there are two types of data can be used in high precision calculations. They are BigInteger and BigDecimal. Although they may be classified generally as "package type", but they do not correspond to the basic types.
BigInteger supports arbitrary-precision integers. It can be used to accurately represent integral values of any size, at the same time without loss of accuracy during operation. BigDecimal supports arbitrary-precision fixed-point numbers. For example, it can be used for accurate currency.

Create a class

Basic Type Default

If the member variable (attribute) type is a primitive type, then in the case of initialization, these types will be given an initial value.

basic type The initial value
boolean false
char \ U0000 (zero)
byte (byte) 0
short (short) 0
int 0
long 0L
float 0.0f
double 0.0d

This default values is not given suitable for topical variables - that are not part of the attribute class. Therefore, if the basic data types defined in the method, as follows:
int x;
Here the variable x is not automatically initialized to 0
the Java will prompt us to "compile-time error, this variable might not be initialized."

Return Type

== == method name and parameter list == == == collectively referred to as the method signature == (signature of the method). == == signature as a method of unique identification.

Call the method's behavior is sometimes referred to send a message to the object. Object-oriented programming can be summarized as: send a message to the object.

Programming

Naming Visibility

Generate a clear name for a class library, Java creators want us to use your own web domain the reverse, because the domain name is usually unique.

Now the entire package name is lowercase.

Small scale chopper

java.lang implicitly included in every Java code file, these classes are automatically available.

Guess you like

Origin www.cnblogs.com/huangwenjie/p/11350156.html