Java 4 Android: all objects (1)

Source: Internet
Author: User

Java 4 Android: all objects (1)

We always like to use C ++ and Java for comparison. Although I have used both of them, I forget them. My first job gave me the oldest language (relative to the two) C. Of course, the conversion from the downloaded library to Android forces the user to return to the object-oriented world...

1. Use a reference operation object.

In C/C ++, you can directly manipulate your data elements, or you can use pointers to introduce the operations. Of course, there are references in C ++, A reference is the alias of a data element. It points to the same place (hopefully wrong ).

In Java, everything is an object, but the object is manipulated through "Reference". The relationship between reference and object is like the relationship between home remote control and TV. Without TV, remote control still exists. To control TV, you need to use remote control, when the TV is not controlled by the remote control, you press it in disorder. Hey hey, there will be no accident, right, because it is your home, but in Java, if a reference you are not associated with the corresponding object, when you operate on it and send messages to it, there will be errors and runtime errors (I just want to remind you that, for example, my cainiao must remember compilation errors and runtime errors ).

String s; // This is the reference, and there is no associated object. String s2 = "abc"; // The created object is String s3 = new String ("efg"); // Like s2, the object is created and associated with the Reference


2. You must create all objects

1) Where to store

Storage Type
Name Speed Description
Register) Fastest Inside the CPU, the fastest speed and the smallest capacity
Stack) It is slower than the register and faster than the heap Located in RAM, the stack pointer can be obtained from the processor
Directly. Pointer down, allocate memory, recycle up
Memory, fast
Heap) Slower than Stack Located in RAM, a general memory pool that stores all Java
Object. The advantage is flexibility. The disadvantage is that it is slower than Stack.
Constant storage Zone
(Const Value Storage)
Fast In RAM, you can also put it in ROM.
The stack is fast, because it is always dead. There is a direct address, saving allocation,
Reclaim
Non-RAM Storage Slow Some stream objects and persistence objects. It may be a network or a disk,
So the speed is slow.


2) Special Case: Basic Type


Java retains some basic non-object types. Such as boolean, char, byte, short, int, long, float, double, void. Why keep this built-in type? Because the Heap memory allocation needs to be managed. If an int is used to create an object, the memory fragmentation will be very serious. Therefore, Java chose to compromise on efficiency. That is, keep them.

Note: (1) Java determines the number of bytes of the basic type and will not change with the platform, which ensures excellent portability; (2) char is 16 bits, two bytes, that is, Unicode encoding. (3) All numeric types are signed and there is no unsigned xxx. (4) Each basic type has its corresponding package. They are classes and objects. (5) for high-precision computing, Java provides BigInteger and BigDecimal.


3) array

Unlike the arrays of C ++/C, Java arrays perform boundary checks, sacrificing efficiency for security. Also, creating a Java array actually creates a reference array. Each element in the array is a reference, and sooner or later it will point to a real object. If it is not specified, it is null. Of course, you can also create an array that stores the basic data type. If this type is not initialized, it will be initialized to 0.


3. Never destroy objects

In C/C ++ and Java, Scope is determined by the position of curly braces. However! In C/C ++, hiding a variable with a large scope is not allowed in Java. Because Java designers think this will lead to program confusion.

{    int x = 12;    {         int x=96;    }}
The above code is valid in C/C ++ and invalid in Java.

In Java, after the referenced scope is complete, the objects to which it points do not necessarily be immediately recycled. You don't have to worry about Object Memory leakage. All this work is done by the garbage collector.










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.