Approaching java object

What is the object

How to create objects

Assignment object

What is the object
object is an individual class is concrete, real existence of things, it has its own life cycle, you can create a class object, the object will die.
Speaking of objects, classes, and we have to have real objects opened distinction. You can create a class object, the object is an individual, namely concrete, but the abstract class is a collection of things that have the same properties and functions, it is an abstract concept. Of course, the object here is real life and we are talking about a completely different subject, this we all know.
How to create objects
in the format: name = new class name of the object class name ();
Where:
class name of the object name 1. left: a name defined objects stored in the stack.
2.new: create objects on the heap.
3. The class name (): Constructor, to initialize the object attribute.
Reference Type Default: null
4. The address of the object name assigned to an object.
E.g:

......
Data d = new Data();.....
.............
public class User {
	public String name;             //属性
	int age;
	
	public User() {                    //构造方法
		
	}

There are three ways to create objects:
1, class name object name 1 = new class name ();

2、类名  对象名2 = 对象名1;

3、public 类名 方法名(){  return 对象名2 ;} //get方法,方法名 get+属性名首字母大写
   对象名3 = 方法名();

For the definition of basic types: For example: int a = 10;
1.int a: is the definition of a name of a variable of type int which is stored in the stack;
2. 10 and 10 being assigned to the i.
Talked about this, I have to add that the concept of the stack and heap:
Stack:

Stack (Stack), also known as the stack, which is a linear form of operation is limited. With the restriction that only permits insertion and deletion operation at one end of the table. This input is called the stack, relatively, and the other end is called the bottom of the stack. To insert a new element, also known as the stack into the stack, push or push, it is a new element into the top element of the above, making the new top of the stack; remove elements from one stack to stack or also known as unstack, it is the top element removed, so that the adjacent element becomes the new top of the stack.
As a stack data structure, only a specific insertion and deletion of linear form at one end. It stores data in accordance with the principle of last-out, first enter the data is pushed onto the stack at the end, the final data in the top of the stack, when you need to read data starts pop-up data (the last data is first read out) from the top of the stack. Stack having a memory effect, insertion and deletion of stack operation, it is unnecessary to change the bottom of the stack pointer.
Stack is allowed to insert special linear form and delete operations at the same end. It allows for insertion and deletion of one end called stack (Top), and the other end of the stack bottom (bottom); fixed bottom of the stack, the stack while floating; called an empty stack if the number of elements in the stack is zero. Commonly referred to insert into the stack (PUSH), delete is called the stack back (POP). Stack also known as LIFO table.
Stack can be used to store the breakpoint when the function call, use the stack to do recursively!
Stack (OS): released automatically assigned by the operating system, the stored function parameters, local variables, and the like. Operate similarly to a stack data structure.

Here Insert Picture Description
stack:

It is a basic stack data structure. Here I use an array to describe, in a binary heap array, each element must ensure that not less than two other elements in a specific location. Meanwhile Accordingly, these elements have to be greater than the other two elements equal to the corresponding position, so the entire data structure. If we draw the entire data structure into a tree structure, we can clearly see the look of the entire structure.
Here Insert Picture Description

Assignment object
if assign "from one object to another," is actually a handle copied from one place to another. This means that if the use of "C = D" for the object, then C and D will eventually point to the object that was initially only D pointed.

Number n1 = new Number();
    Number n2 = new Number();
    n1.i = 9;
    n2.i = 47;
    n1=n2;    //相同类的对象之间赋值
    System.out.println(n1.i+" "+n2.i);              //输出结果为n1.i和n2.i都为47
    n1.i=27;                                                    //之后再输出n1.i和n2.i都为27

N1 behind the change but also changed n2! This is because both n1 and n2 contain the same handle, it points to the same object (initially handle in n1, point to accommodate a target value of 9 in the assignment process, the handle has actually been lost; it objects will be cleared by the "garbage collector" automatic).

Published 22 original articles · won praise 27 · views 2857

Guess you like

Origin blog.csdn.net/weixin_44346470/article/details/88920193