Preliminary study of Java object-oriented preliminary understanding and object-oriented memory Analysis diagram example description

Source: Internet
Author: User
Tags lenovo

1. When it comes to object-oriented, a topic that is not open, is the process oriented.

Process-oriented for simple, non-collaborative transactions. Process-oriented = decomposition problem + logic first = First Detail, then whole.

Compared to the process oriented, object-oriented is modular, when we think of more complex issues, such as "How to build a car?" will find that listing 1234 Such steps, is not possible. That's because making a car is too complicated and requires a lot of collaboration to do it. At this point object-oriented thought came into being. Object-oriented = polymorphic + inheritance = First abstract, then concrete.

Refer to the references for comparison:

Process-oriented (Procedure oriented) and object-oriented (Oriented,oo) are an idea for software analysis, design, and development, which guides people to analyze, design, and develop software in different ways. In the early stage of the process-oriented thinking, with the expansion of software scale, the problem of the complexity of the improvement of the process-oriented drawbacks more and more obvious display, the emergence of object-oriented thinking and become the mainstream of the current way. Both are used throughout the software analysis, design, and development phases, and corresponding object-oriented approaches are referred to as Object-oriented analysis (OOA), Object-oriented design (OOD), and object-oriented programming (OOP).

Object-oriented has three main characteristics: encapsulation, inheritance and polymorphism, while process-oriented encapsulation is just encapsulation function, and object-oriented can encapsulate data and function. So the object-oriented advantage is more obvious.

2. Object-oriented is modular, i.e. class.

A class is a template, an abstract collection of things in a cluster.

A car class, the abstract combination of all vehicles, specific what class to see how to differentiate. Car class entities According to the size level, there are cars, vans, trains, cars and so on. According to the brand of Mercedes Benz, Audi, BMW, Volvo and so on.

In Java, common abstractions are called classes, class. The entity that produces the specific class is called an object. Object.

Summarize: Class and object relationships: A class is an abstract concept used to describe an object of the same type that defines the common attributes, methods, and so on that such an object should have.

Java classes and object examples:

 package   Cn.sxt.oop;  public  class   test_s { int      ID;    String name  = "Pikaqiu" ;         double   score;  public  static  void   main (string[] args) {System.out.println (" Object-oriented learning .... "
    
    // 
     for classes, get object  test_s stu = 
    new  
     test_s ();    System.out.println (Stu.name); }}
   

2. Properties and methods of the class

The properties of a class are used to define the class or the data or static characteristics that the class object contains. The scope of the property is the entire class.

When a member variable is defined, it can be initialized, and if not initialized, Java uses the default value to initialize it.

Data type Default value
Integral type 0
Floating point Type 0.0
Character type ' \u0000 '
Boolean type False
All reference types Null

method is used to define the behavior characteristics and functionality of the class or instance of the class. Methods are class and object behavior characteristics pair abstract.

The method is similar to the process-oriented function.  In the process, the function is the most basic unit, and the whole program consists of one or more function calls. In object-oriented, the entire program is a class to the base unit, and the method is subordinate to class and object pairs.

3. Object-oriented memory analysis;

Java Virtual machine memory can be divided into three areas: stack stack, heap, method area.

Stack pairs feature the following:

(1). The stack description pair is the method that executes to the memory model. Each method is called to create a stack frame (store local variables, operands, method exits, etc.)

(2). The JVM creates a stack for each thread that holds information about the thread's execution methods (actual parameters, local variables, etc.)

(3). Stacks are thread-private and cannot be shared between threads

(4). stack All storage features are "advanced out, last in first out"

(5). stacks are automatically assigned by the system and are fast. The stack is a contiguous memory space.

The heap is characterized by the following:

(1). Heap is used to store created object arrays (arrays are objects as well)

(2). JVM has only one heap, shared by all threads

(3). Heap is a discontinuous memory space, the allocation is flexible, the speed is slow.

The method area (also called static Zone) features the following:

(1). JVM has only one method area, shared by all threads

(2). The method area is actually a heap, just for storing classes, constant-related information

(3). used to store the program is always the same or the only content. (Classes information [Class object], static variable statics, string, method, etc.)

Example one: Student class description Stack, heap, method area

 PackageCd.bjtxt.array; Public classStudent {String name; intID; intAge ;    String gender; intWeig?ht; Static intNumber = 100;//static constants are stored in the method area         Public voidStudy () { This. Name = "High-forward"; SYSTEM.OUT.PRINTLN (Name+ "You are learning, do not Disturb ..."); }         Public voidSayHello (String sname) {System.out.println (name+ "Say hello to" +sname+ "); }         Public Static voidMain (string[] args) {/** S1,S2 is a class construct, such as an instance object, which is a pointer to a pair of one by one corresponding objects * name, ID, age is the property of the Instance Object * Study () and SayHello () is the real Example object's method*/Student S1=NewStudent ();        S1.study (); S1.sayhello ("Tom"); System.out.println ("####################"); Student S2=NewStudent (); S2.name= "Pikachu"; S2.sayhello (Children); }}

Console display example of a printed result

Gao Jin is learning, do not disturb ... High feed say hello to Tom ####################3 Pikachu say hello to children

Corresponding object-oriented memory analysis diagram with reference to an example

 

Example two, set example a slightly improved, Student and Compuent class description stack, heap, method area.

 Public classStudent {String name; intID; intAge ;    String gender; intweight; Computer comp; //One computer per person, one class can be used as a property         Public voidStudy () {SYSTEM.OUT.PRINTLN ("Our students learn all with the" +comp.brand+ "CPU of the computer is" +comp.cpu); }         Public voidSayHello (String sname) {System.out.println (name+ "Say hello to" +sname+ "); }         Public Static voidMain (string[] args) {computer C=Newcomputer (); C.brand= "Lenovo"; C.cpu= 920; Student S1=NewStudent (); //student has computer this propertyS1.comp =C;        S1.study (); S1.sayhello ("Tom"); System.out.println ("####################"); Student S2=NewStudent (); S2.name= "Pikachu"; S2.sayhello (Children); }}classcomputer{String brand; intCPU;}

Console display example of a printed result

Our students learn to use Lenovo Computer CPU is 920null say hello to Tom ################### #皮卡丘向小儿问好

The corresponding object-oriented memory analysis diagram of the reference two-picture

   

Preliminary study of Java object-oriented preliminary understanding and object-oriented memory Analysis diagram example description

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.