Home > Java > javaTutorial > Everything in Java is an object

Everything in Java is an object

巴扎黑
Release: 2017-06-26 11:42:37
Original
1306 people have browsed it

1. Use references to operate objects

Everything in Java is regarded as an object, and the operation object is actually a "reference" of the operation object. The relationship between the two is equivalent to using the remote control (reference) to operate the TV (object). Having a reference does not necessarily require an object to be associated with it, such as Sting a; just declaring a reference is not associated with an object.

2. You must create all objects

Once you create a reference, you want to associate it with an object. You usually use new to create a new object (strings in Java can be created with quoted text initialization).

 2.1. Storage location

  (1) Register. The fastest storage area (located inside the processor) cannot be directly controlled by humans in Java (c++/c allows you to suggest register allocation methods to the compiler).

 (2) Stack. Located in RAM, the stack pointer moves downward to allocate new memory, and moves upward to release memory. It is very fast, second only to registers. But when creating the system, the java system must know the exact declaration period of all items stored in the stack in order to move the pointer up and down. This constraint limits the flexibility of the program. Some basic types of variables and object references (Java objects are not among them) are stored on the stack.

  (3) Heap. A general memory pool (located in RAM) used to store all java objects. The advantage of the heap unlike the stack is that the compiler does not need to know how long the data stored in the heap survives. When an object is new, memory is automatically allocated in the heap for storage. The compiler automatically recycles the object when it is no longer needed without human control. Of course, this flexibility comes at a price: creating objects on the stack takes more time than in C/C++.

  (4) Constant storage. Constant values ​​are usually stored directly within program code because constant values ​​never change.

  (5) Non-RAM storage. Data lives outside the program and is not subject to any control by the program. Such as stream objects and persistent objects.

2.2. Special case: basic types

A series of types often used in programming, such as integers, decimals, characters... These types are called basic types and are Special treatment.

Why should we be treated specially? new stores objects in the heap. As mentioned before, data stored in the heap takes longer, so for these frequently used, particularly small, simple data, the cost of storing it in the heap is too high. Therefore, Java does not use new to create variables, but creates an "automatic" variable that is not a reference. This variable stores the value directly and places it on the stack. The storage space occupied by basic types is fixed.

In order to adapt to the fact that everything is an object, basic types have a wrapper class, so that a non-basic object can be created in the heap to represent the corresponding basic type.

For example:

char c='a';
Character ch=new Character(c);
或者
Character ch=new Character('a');
Copy after login

The automatic packaging function in java se5 can automatically convert between the two.

 2.3. Scope

Variable scope: Variables include basic types and class references that directly store values. Only survives within the code segment.

Object scope:

{
    String s=new String("string");      
}//end of scope
Copy after login

References are stored on the stack and end at the end of the scope. However, the String object pointed to by s continues to live in the heap. It is up to Java's garbage collector to determine when to recycle the object. Doing this eliminates most "memory leak" problems.

 2.4. Method parameters

Java is different from c/c++. Many people are confused about whether Java method parameters are passed by value or by reference. For basic data types, values ​​are passed, because basic data stores values ​​directly in the stack area. The object is passed by reference (the object reference in the stack area is the address of the object in the heap area). Take String and StringBuilder to illustrate.

Let’s look at two examples.

public class Test
{public static void test(String str)
        {
            str = "world"+str;
        }public static void main(String[] args)
        {
            String  str1 = new String("hello");
            test(str1);
            System.out.println(str1);
        }
}
Copy after login

The output result of the above example is still hello, why? Because the modification to str in the test is not a modification to the original object, this involves the characteristics of the String object. The String object cannot be modified after it is created. The above modification is actually: create an empty StringBuilder and then call the append method to insert it. "world", str, and then call the toString method to create a new String object. So the reference object of str is already brand new at this time, so str1 in the main function has not changed. Let's look at another example of StringBuilder.

public class test1 {    static void test(StringBuilder str){
        str.append("1234");
    }    public static void main(String[] args) {
        StringBuilder a=new StringBuilder("abc");
        test(a);
        System.out.println(a.toString());
    }
}
Copy after login

The above output result is abc1234. The modification to str in test also takes effect on a, because the references of str and a point to the same StringBuilder object, and StringBuilder is modifiable. . It is enough to show that what is passed is a reference.

The above is the detailed content of Everything in Java is an object. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template