Javase Basics (5)-Object-oriented (class 5.1 and object concepts, creation, and memory allocation)

Source: Internet
Author: User

I. Related concepts of classes and objects 1, object-oriented and process-oriented understanding

Object-oriented and process-oriented are the thinking ways to solve the problem.
process oriented : Think in terms of the performer , focus on " How to do ", more suitable to solve small projects
Object-oriented : Thinking in the direction of the conductor, focusing on " who to do ", more suitable for solving large-scale projects
Object-oriented and process-oriented can not be separated, the two complement each other, indispensable!
Macroscopic through the object-oriented thinking, micro-oriented through the process of thinking!

2. Concepts, relationships, distinctions between classes and objects

(1) Concept

Object: All things that can be described in the objective world are called objects (objects of all things).
Class: A collection of groups of objects with the same properties and methods is called a class

(2) Relationship

Object: A concrete instance created by a class
Class: The type to which the object belongs

(3) Difference

Object: Concrete, actual existence
Class: abstract, template-Nature

3, how to separate each object?

Through the characteristics of an object (properties and methods)

Property:
Appearance characteristics of an object
such as: name, age, color, price, etc.
Method:
The behavior characteristics or functions of an object
For example: Can fill water, eat, drink, can calculate

Ii. creation and use of classes and Objects ★ 1, class creation

Steps:
① defined by the class keyword
② writing attributes (appearance features)
③ Writing method (behavioral function)
Example:
[Public] class name {
String name;//Property
int age;//Property
public void Show () {//method
}
}

2. Creation of objects

Steps:
① creating objects
Type Object name = new Type ();
② using objects
Assigning a value to an object's properties
The name of the object . attribute = value;
System.out.println (object name. Attribute);
Methods for calling Objects
The name of the object . Method ();

Attention:
① different objects are independent , their property values do not affect each other, and one change does not affect the other object
The properties of the ② object can be used without assigning a value , with a default value

int--0
double--0.0
char--\u0000
Boolean--false
Reference type--null

3. Comparison of basic types and reference types
Use steps Call Storage location Access mode
Basic type int i = 100; Variable name and variable value Stack Direct access to I can
Reference type

Student s = new Student ();

S.name= "Xiaoming"

Variable name, object name, or reference name

Object with variable Value

Object name exists on stack

Value exists for

Properties of general access s, such as S.name

Iii. memory allocations for classes and objects ★ 1, class load only once ★

each time you create an object , you need to determine whether the class has already been loaded , and if it has already been loaded, you do not need to load it again, and if it has not, you need to load the bytecode file into the method area through the ClassLoader;
Then create the object in the heap

2. Store ★

The object's Reference (object name) and local variables are stored in the stack
The properties of objects and objects are stored in the heap
Method Area is the structure information of the class

3, different objects are independent, do not affect the "supplementary" memory allocation diagram

Javase Basics (5)-Object-oriented (class 5.1 and object concepts, creation, and memory allocation)

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.