Java4android Everything is the object (1)

Source: Internet
Author: User

We always like to use C + + and Java as a comparison, although, both of which I have used but also forget in the lake. And my first job, let me take one mouthful of the oldest language (relative to it) C. Of course, from the download library to the Android conversion, the perpetrators themselves back to the object-oriented world ...

1. Manipulate the object with a reference.

In C + +, you can manipulate your data elements directly, or you can manipulate them by pointers, of course, there are references in C + +, which is an alias for a data element that points to the same place (hopefully not mistaken).

In Java, everything is an object, and objects are manipulated by reference (Reference). The relationship between reference and object is like the relationship between the remote control and the TV, no TV, remote control is still there, and the TV should be controlled by remote control, when the TV is not controlled by the remote, and it is associated with it, you go to disorderly press, hey, no accident, right, because that is your home, but in Java, if a reference , you go to manipulate it, send a message to him, it's going to make a mistake, run-time error (I just want to remind my rookie that you have to keep in mind compile-time errors and run-time errors).

String s;//is a reference, and there is no object associated with it. String s2= "abc";//This object has been created with a string S3=new string ("EFG");//And S2, the object is created and associated reference


2, all objects must be created by you

1) Where to store

Storage type
name Speed Description
Register (Register) The fastest Fastest, smallest capacity inside the CPU
Stacks (Stack) Slower than registers, faster than heaps Located in RAM, through the stack pointer can be obtained from the processor
to direct support. Pointer down, allocate memory, recycle up
Memory, Fast
Heaps (heap) Slower than stack Located in RAM, a common memory pool that holds all Java
Object. The advantage is flexibility, the disadvantage is slower than stack
Constant Storage Area
(Const Value Storage)
Fast Located in RAM can also be put into ROM can, personally think will be than
Stack fast, because all are dead, directly an address, omitting the allocation,
Recovery
Non-RAM Storage Slow Some stream objects, persisted objects. Could be a network, possibly a disk,
So the speed is slow.


2) Exceptions: Basic types


Some basic types of non-objects are still preserved in Java. such as Boolean, Char, Byte, short, int, long, float, double, void. Why do you want to keep this built-in type? Because allocating memory in the heap requires management, this time if an int also has an object, then memory fragmentation can be very serious. So, for efficiency, Java chooses a compromise. That is, keep them.

It is important to note that: (1) Java determines the number of bytes of the base type, does not change with the platform, and thus maintains a very good portability; (2) Char is 16 bits, two bytes, which is Unicode encoding. (3) All numeric types are signed, there is nothing unsigned xxx, no. (4) Each base type has its corresponding wrapper, which is the class is the object. (5) High-precision Computing, Java provides BigInteger and BigDecimal.


3) array

Unlike the C++/C array, the Java array is checked for bounds, sacrificing efficiency for security. Also, creating a Java array is actually creating a reference array in which each element of the array is a reference, and sooner or later it is going to point to the real object. And when it is not specified, it is null. Of course, you can also create an array that holds the base data type, which, if not initialized, is initialized to 0.


3, never need to destroy objects

In C + + and Java, scope (scope) is determined by the position of the curly braces. But! It is not allowed in Java to "hide" a variable of a larger scope in C/s + +. Because Java designers feel that this can lead to confusing programs.

{    int x = n;    {         int x=96;    }}
The above code is legal in C/s + + and is illegal in Java.

In Java, after the scope of the reference is complete, the object it points to does not necessarily have to be reclaimed immediately. You don't have to worry about object memory leaks. These jobs are given to the garbage collector.










Java4android Everything is the object (1)

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.