For just contact with the object oriented true little white shoes, class object, object instance, object reference, reference variable problem as well as no object ~ although the blogger did not get the object ~, this article will be explained one by one!

1. What is an object?

There is a popular phrase in Java called “Everything is an object”, which is one of the concepts from the design of the Java language. To understand what an object is, you need to understand it in conjunction with a class. Here’s a quote from Ideas for Java Programming:

“In common parlance, every object is an instance of a class, where ‘class’ is a synonym for ‘type.'”

From this sentence, we can understand the essence of the object. In short, it is an instance of a class. For example, all people are collectively called “human beings”, where “human beings” is a class (a type of species).

2. What is object reference?

Let’s start with a quote, also from Java Programming Ideas:

“Every programming language has its own way of handling data. Sometimes programmers have to be careful about what type of data they are dealing with. Do you manipulate elements directly, or do you manipulate objects with some kind of indirect representation based on special syntax (such as Pointers in C/C++)? All of this is simplified in Java, where everything is treated as an object. Therefore, we can adopt a uniform grammar. Although everything is “treated” as an object, the manipulated identifier is really a “reference” to an object.

Object and object reference are two very different things. For example, we would normally create an object with this line of code:

  Person per =new Person("Zhang");
Copy the code

Some white boy would say that per here is an object, an instance of the Person class. The per is not really an object, but a reference to the object being created. Which one is right? Before we worry about which is true, let’s look at two lines of code:

  Person person; person =new Person("Zhang");
Copy the code

These two lines of code do exactly the same thing as the one above. In Java, new is used to create objects on the heap. If per is an object, then why should the second act create objects through new?

Thus, per is not the object created. What is it? Per is a reference to an object that can point to the Person class. The statement that actually creates the object is the new Person(” Zhang SAN “) on the right; So per here is a reference to an object that can point to the Person class.

Simply put, a reference to an object means “define a variable that refers to an object.”

Object  obj=new Object(a);/ / Object: class
//obj: object reference
/ / the new Object () : the Object
Copy the code

3. What is an object instance?

First of all, there is no such thing as an instance of an object, only an instance of a class, and an instance of a class, by definition, is an object of a class. But a lot of people call it that, so how do you understand object instances? For example, there is a programmer named Yichun in human beings. Yichun is an example of human beings

String s = new String("YiChun");
Copy the code

S is an instance of the String class

There are many concepts that are not officially given to object instances, such as the parent class declaration pointing to subclass objects, method signatures, etc., but people often use such concepts so that they get used to it. So instance objects can be roughly understood as object references…

4. What are reference variables?

A reference variable is a variable declared with a reference type, called a reference type variable. Such as:

People people;  // Where people is a reference variable, people is a class belonging to the reference type
Copy the code

5. Object and object reference collision sparks

From the object reference:

An object reference can refer to zero or one object

Start with the object:

An object can be referenced by one or more objects

How to understand pinch? ~ The little white boy’s shoe skull is getting bigger… ~

5, 1. Start with object references: an object reference can refer to zero or one object

Start with object references: an object reference can refer to zero or one object

public static void main(String[] args) {
      Two t; // An object reference can refer to zero objects
      t=new Two();// An object reference can refer to an object
      t=new String(a);// An object reference cannot point to >1 object
    }
Copy the code

Imagine what would happen to such code? Yes, it will compile and fail

 public static void main(String[] args) {
        String str;
        str=new String("string1");
        str=new String("string2");
    }
Copy the code

If a new object is created, it creates an object. If a new object is created, it creates an object. If a new object is created, it creates an object. Little white boy shoes said feel the blogger pressed on the toilet seat…

Don’t worry, let’s see what this code says

  public static void main(String[] args) {
        String str; 
        str=new String("string1");
        str=new String("string2"); System.out.println(str); } run result: string2Copy the code

What does that mean? Indicates that STR is now a reference to the next object. The previous object is garbage collected because the previous object (i.e., string1) cannot be used again. So the above program can be understood as follows:

 public static void main(String[] args) {
   String str;// An object references STR to zero objects
   str=new String("string1");// An object refers to STR to an object string1
   str=new String("string2");// Note that the object reference STR does not refer to the second object string2, but instead redirects the previous reference to the first object string1 to another object string2
}
Copy the code

Instead of pointing to the second object, string2, the object reference STR repoints a previous reference to the first object, string1, to another object, string2. So start with object references: an object reference can point to zero or one object!

At this time, the little white boy shoes gradually released the blogger pressed on the toilet seat of the hands…

5, 2. Start with objects: An object can be referenced by one or more objects

From an object, an object can be referenced by one or more objects

White boy shoes also don’t understand, so he pressed the blogger on the toilet seat with both hands…

Take a look at this program:

   Demo demo1,demo2,demo3;
   demo1=new Demo();
   demo2=demo1;
   demo3=demo2;
Copy the code

How to understand? A little dizzy? Object reference = object reference…

To figure this out, you first need to understand Java virtual machine memory, and how variables (object references) and objects are stored. Object references are stored in stack memory, while objects are stored in heap memory. Analysis is as follows:

Demo demo1,demo2,demo3;// Create multiple object references, all stored on the stack
demo1=new Demo();// Create a Demo object, store it in the heap, and point demo1 to it, which adds a chain
demo2=demo1;// Demo2, like demo1, points to the Demo object in the heap
demo3=demo2;// Demo3, like demo2, points to the Demo object in the heap
Copy the code

First, each object has only one address value. New Demo () creates an object, demo1 is the address value of the object, and is used to find the object. Demo2 =demo1 changes the address value of new Demo () from demo1 to demo2. Demo3 =demo2; demo3=demo2; the size of the object itself does not change.

At this time, the little white boy shoes gradually released the blogger pressed on the toilet seat of the hands…

Finally, let’s look at a popular program and analysis of objects and object references on the Web. The code is as follows:

UserType ut = new UserType();  // ut is a reference, the actual object is in memory.
ut = new UserType(); /* now ut is a reference to another object, and the previous object is garbage collected (because the previous object cannot be used again). * /UserType ut2;// defines a reference to ut2, which does not refer to any objects and cannot use...
ut2 = new UserType(); // Then UT2 becomes a reference to an object.
UserType ut3 = new UserType();
UserType ut4 = new UserType();
ut3 = ut4;   // Now ut3 refers to ut4 objects, this is not assignment...
int a = 5;
int b = 4;
a = b;  // This is an assignment. A and B still refer to different objects
Copy the code

Finally to believe in the truth

For object-oriented languages like Java, first of all, believe in one thing: everything is an object. Then believe the other word: a variable is just a zero-hour storage place. A variable of a reference type is just a normal variable that stores the address of the reference type. Object creation allocates a memory directly in memory.

One last bit of grooming

Referencing type variables is like a shortcut on your computer; An object is a game installed on your disk that actually takes up space; Variables are just shortcuts

In plain English, primitive datatypes are like things that sit directly in the closet, and reference datatypes are the keys to the corresponding code in the closet. The key number corresponds to the cabinet.

To the end of this article oh, small white shoes, you know what baa ~

Finally, if there is insufficient or improper place, welcome to criticize, grateful!

Welcome to pay attention to my public number, discuss technology together, yearning for technology, the pursuit of technology…