Java type

zhaozj2021-02-11  157

Java type

Java is simple and beautiful for programmers. Programmers can practice well, achieve OO's ideology, design. The first time when we learn language is the type. I have been in the Java language should have been a long time. I have only started learning, careful research. Learning should always sum up, I feel that I can write something to make you understand more, but also more comparable. Publish, more opportunities to communicate with everyone. Please Guao, my email is: justin@ipedo.com. Interested friends please send Email communication with me.

The Java type is always two, the primitive type and objects.

These types include int, long, byte, char, short, double, float, boolean. These types are placed in Stack. Learning to use C / C can use them like C / C . But and C / C is not the same, you can't get their address, or index. So a basic type will be released outside the valid range. Look at the following code:

Class testprimitive {PUB

li

C static void showprimitive () {INT i = 1; system.out.println ("The Integer IS:" i);} PUB

li

c static void main (string [] args) {showprimitive ();}}

I is allocated in the STACK, and I will clear it from the Stack after ShowPrimitive (can be seen as a scope) code. You can't assign a basic type from HEAP, unless he is a The member variable of the object. And C / C is different, as long as you use New, you can allocate any type in HEAP.

In the .NET, you can allocate a structure in the STACK.

Also, if you do not assign a variable for intimate type of int C / C , which variable may be any value, while Java's variable of a basic type will assign a initialized value. Int, long is 0, float Double is 0.0, Boolean is False.

Some people say that there is no object in Java, all of which have a pointer. This point has a certain reason. In contrast, you cannot assign an object in a Stack, you can only pass new (or other implicit method call constructive) A object is assigned in HEAP. And each of us defines an object, as follows:

Object ob = New Object ();

We created an object object through new object (), and the OB we act is an object reference. This object references to object entities in HEAP. We can also treat Object Reference as a basic type, it In STACK, the same as other basic types, will be cleared from Stack after execution of code execution in the scope of code. But it actually points to the objects in the HEAP. If there is no other Object Reference point When this object, when the Java virtual machine calls the garbage collection code, it will be cleared from the heap.

Similarly, Java also pies the initial value of each defined object to null. From this point we can also see that we are different from the objects and C , which can all correspond to the pointer, without the object variables corresponding to the pointer. exist.

Each basic type corresponds to an object-outsourcing class. All basic types of Java have corresponding outsourcing classes, ie object type INT, FLOAT, etc. The specific correspondence table is as follows:

Int Integer Long Long Short Byte Byte Char Character Float Float Double Double Boolean Boolean

Basic types and objects are different from those of the memory area, which can use point operators, and basic types should not. Let's take a look at the code: int i = 10; integer = new integer (10); System.out.println (i.tostring ()); // error code system.out.println (integ.tostring ()); // correct code

The object has its own method (MEMBER), and the basic type is not, so calls the basic type variable I.Tostring is an error, although both the semantics i and INTEG represent an integer of 10 values.

The biggest advantage of basic type is to allocate memory in Stack, which is certain than in HEAP, and saves memory. So it is recommended to use basic types instead of object when using a large number of numeric types. Let us look at a piece of code so that there is Sensory understanding:

pub

li

C class freemem {

pub

li

C static void main (string [] args) {long first = runtime.getime (). freeMemory (); int [] ar = new int [1000]; for (int i = 0; i <1000; i ) ar [ I] = i; long second = runtime.getruntime (). freeMemory (); system.out.println (first - second); integer [] ar2 = new integer [1000]; for (int i = 0; i <1000 ; i ) Ar2 [i] = new integer (i);

Long third = runtime.getruntime (). freeMemory (); system.out.println (second - third);}}

Here is the result I get:

4016 20848

It can be clearly seen that the use of basic types is much less than the memory consumed by the object. So you can use the basic type to save the basic type to save memory, increase the code execution speed.

Just like we are all Yellow descendants, all the objects of Java have the same ancestors - Object. This is a single structure of Java. Many C class libraries (or other object-oriented class libraries) use single structure, For example, MFC's COBJECT, VCL's TOBJECT, but Java guarantees this from the language itself. Use the MFC you can define a class is not inherited from cobject, but in Java, if you define a class does not inherit, then It is inherited from the Object class by default. Single structure provides a good feature for some of the development of tool classes. For example, some clusters: Vector, HashMap, etc., which cannot know what kind of data they want to accommodate at the time of design. C uses template to complete this feature, while Java does not implement Template at the language level, but because all Java objects are inherited from Object, the cluster class can be accommodated.

转载请注明原文地址:https://www.9cbs.com/read-5791.html

New Post(0)