Java 09 (object oriented java core)

Object oriented and process oriented

Process oriented: focus on causality without independence. Small individuals are formed by causality, and small individuals are formed as a whole by causality. Focus on the process.

Object oriented: focus on objects. It doesn't care about how the underlying object is implemented, but only what the object can do.

eg: human brain is used to object-oriented.
One person smokes ---- > one cigarette lighter
Lighter brand change
People change
The scene can be changed
Can be replaced.
The relationship between these things is weak, but each independent body cooperates with each other and focuses on what the individual can do.

The relationship between enterprises and employees is weak.
The focus of low coupling and high extension force is object-oriented.

Process oriented: focus on the specific implementation process and causality.
Advantages: for the program development with relatively simple business logic, it can achieve rapid development and low initial cost.
Disadvantages: it is difficult to solve very complex business logic. Process oriented leads to very high coupling between software. As long as there is a problem in one link, the whole will be affected.

Object oriented: focus on what functions the object (independent solid) can accomplish.
Advantages: low coupling and strong expansion ability. It is easier to solve the complex business logic in the real world.
Disadvantages: the initial investment cost is high, which requires independent body extraction and a lot of system analysis and design.

C language, pure object-oriented process, C + + semi object-oriented process,
Java is purely object-oriented

Three characteristics of object-oriented


All object-oriented programming languages have these three characteristics.

Adopt object-oriented software development: (OO is used throughout the whole software development process)
Object oriented analysis: OOA
Object oriented design: OOD
Object oriented programming; OOP

In the development process, programmers play the role of transformation, or bridge, to connect the real world and the virtual world.

What is a class

A result of a highly abstract summary of the human brain.
Class does not exist in the real world. It is a template, a concept, and the result of human brain thinking abstractly.
Class represents a class of things.
In the real world, object A and object B have common characteristics. Abstract and summarize A template, which is called class.

What is an object

Object: the actual individual, which actually exists in the real world.

How to use objects and classes
There are classes before objects.

Software development process:
Programmers observe the real world and find objects from the world
After finding N objects, the common features of all objects are extracted
Form a class (template) in the brain

Definition of JAVA program class
Creating objects through classes
Objects cooperate with each other to form a system

Class - > objects are called instantiations
Objects - > classes are called abstractions
a key:
Class describes the common characteristics of objects: for example, height characteristics
When you need to access the height feature, you need to access the object.
Different objects may have different height characteristics.

	A class mainly describes:    state + Action.
	Status information: name, height, gender, age.
	Action information: eat    drink      sing       dance     study.
	state------------> Class properties
	action------------> Class method

Property: describes the state information of the object.
Method: describe the action information of the object.

###Class definition

Syntax structure:

Modifier list class name{

Define an exposed class---Students
public class student {
//  attribute
  int no;
  String name;
  boolean sex;
  int age;
  String address;
//  method
//  Describe the action information of the object

java contains two data types:
Basic data type
Reference data type
String. Provided by class sun
System. Provided by class sun
Student. The class programmer wrote it himself

User. The class programmer wrote it himself
Product. The class programmer wrote it himself
Customer. The class programmer wrote it himself

Class is a reference data type
String username = "za";
Student s = ???

Object creation and use

Student class

Students: common features:

Student id int
Name String
Gender boolean
Age int
Address String

having dinner

// Method area memory: when the class is loaded, the class byte code fragment will be loaded into the memory.
// Stack memory (local variable): when a method code fragment is executed, it will allocate memory space to the method and press the stack in the stack memory.
// Heap memory: new objects are stored in heap memory.

// Object: the memory space created by the new operator and opened up in the heap memory is called an object.
// Reference: a reference is a variable that holds the memory address of another java object.
// In java language, programmers cannot directly manipulate heap memory, and there is no pointer in java. Instance variables inside objects in heap memory can only be accessed by reference.

Object creation and use

public class Test {
  public static void main(String[] args) {
    // Instantiate multiple objects through a class
    // Method of instantiating object: new class name ();
    // The new operator is used to create objects and open up new memory space in the JVM heap memory

    int i = 10; //Int is the basic data type and 10 is the literal value of int type
    // s is a local variable (stored in stack memory)

    Student s = new Student();// s is a variable name and new Student() is a student object
    // Syntax format for accessing instance variables:
    // Reading data: references Variable name
    // Modifying data: references Variable name = value;
    int stuNo =;
    String stuName =;
    int stuAge = s.age;
    boolean stuSex =;
    String stuAddr = s.address;

    System.out.println("Student number = " +; //Student number = 0
    System.out.println("full name = " +;//Name = null
    System.out.println("Age = " + s.age);//Age = 0
    System.out.println("Gender = " +;//Gender = false
    System.out.println("address = " + s.address);//Address = null = 10; = "Jack";
    s.age = 20; = true;
    s.address = "Beijing";

    System.out.println("Student number = " +; //Student number = 10
    System.out.println("full name = " +;//Name = Jack
    System.out.println("Age = " + s.age);//Age = 20
    System.out.println("Gender = " +;//Gender = true
    System.out.println("address = " + s.address);//Address = Beijing

    // s2 is both a reference and a local variable
    // Student is a variable data type
    Student s2 = new Student();

    System.out.println("Student number = " +; //Student number = 0
    System.out.println("full name = " +;//Name = null
    System.out.println("Age = " + s2.age);//Age = 0
    System.out.println("Gender = " +;//Gender = false
    System.out.println("address = " + s2.address);//Address = null

//    System.out.println(;     //  Compilation error: no this instance object cannot be accessed directly through the class name



Local variables are stored in stack memory
 The instance variable of the member variable is stored in the heap memory java Object internal storage

Instance variables are one copy for one object and 100 copies for 100 objects.

public class User {

  // Attribute: the following members are instance variables of member variables

  // User number
  int no;
  // int is the basic data type: integer
  // no is an instance variable

  // user name
  String name;
  // String is a reference data type: represents a string
  // name is an instance variable

  // Home address
  Address adder;
  // Address reference data type: represents the home address
  // addr is an instance variable

public class Address {
    String city;

    String street;

    String zipcode;


public class Test {
  public static void main(String[] args) {
    // Create user object
    User u = new User(); // u is a local variable that holds the memory address

    // Output the value of the instance variable inside the user object

    //Modify the value of User internal instance variable = 110; = "zs"; // A string is a special object, not new
    u.adder = new Address(); = "Beijing";
    System.out.println( + " Live in " +;

Examples of mutual calls between objects:

Couple example

public class Husband {

  String name;

  // Include wife references
  Wife w;

public class Wife {

    String name;

    Husband h;

public class Test {
  public static void main(String[] args) {
    // Create a husband object
    Husband hxm = new Husband(); = "Huang Xiaoming";
    // Create a wife object
    Wife bb = new Wife(); = "baby";
    // Marriage "finds a wife through a husband, finds a husband through a wife"
    hxm.w = bb;
    bb.h = hxm;

    //Get the name of Huang Xiaoming's wife above
    System.out.println( + "The wife's name is "+;



JVM virtual machine memory is mainly divided into three parts: stack memory heap memory method area

There is one heap memory and one method area memory.
One thread, one stack memory.
When a method is called, the memory space required by the method is allocated in the stack memory, which is called pressing the stack.
After the method is executed, the memory space to which the method belongs is released, which is called elastic stack.

The code fragment of the method and the code fragment of the whole class are stored in the method area memory. When the class method is loaded, these methods will be loaded.

During program execution, java objects are created through new and stored in heap memory. There are instance variables inside the object, so the instance variables are stored in heap memory.

Variables are divided into:
Local variable "declared in method body"
Member variable "method declaration"
Instance variable "no static before modifier"
Static variable "static in front modifier"

Static variables are stored in the method area memory.

Among the three pieces of memory, the stack memory changes the most frequently;
The first to have data is the method area memory;
Garbage collector is mainly used for heap memory.

When will the garbage collector [automatic garbage collection mechanism, GC mechanism] consider recycling the memory of a java object?
When the java object in the heap memory becomes garbage data, it will be recycled by the garbage collector.

When will java objects in heap memory become garbage?
If there is no reference to the object, it becomes garbage.

Because this object cannot be accessed, the reference of the object is recycled by reference.

Null pointer exception - creation and use of java objects

public class Customer {

  int id;


public class Test {
  public static void main(String[] args) {
    Customer c = new Customer();
    System.out.println(; // 0
    // The following program can be passed because it conforms to the syntax
    // Null pointer exception during operation: Java lang.NullPointerException
    // Null pointer exceptions must occur when null references access instance related data
    //Instance related data: this data must be accessed with the participation of objects. This data is instance related data.
    c = null;
    System.out.println(; //

Memory analysis

Each class can write a main method, but generally, a system only writes one entry.

public class Product {

  int productNo;

  double price;

public class Test {
  public static void main(String[] args) {

    //Create object, commodity object
    //iphone7 local variables
    //IPhone 7 reference
    //The memory address saved in the iPhone 7 variable points to the commodity object in the heap memory.
    Product iphone7 = new Product();

    // Syntax for accessing instance variables:
    // Reading: references Variable name
    System.out.println("Item number: " + iphone7.productNo);
    System.out.println("Unit price of goods: " + iphone7.price);

    // Modifying: references Variable name = value
    iphone7.productNo = 1;
    iphone7.price = 6800.0;

    System.out.println("Item number: " + iphone7.productNo);
    System.out.println("Unit price of goods: " + iphone7.price);

    //error: the instance variable must first create an object and access it by reference instead of using the class name directly Access by
//    System.out.println(Product.productNo);
//    System.out.println(Product.price);

Object oriented development
First, there are classes.
Secondly, after having classes, create objects.

eg: Master house

public class Ren {
  // ID number
  String id;

  // name
  String name;

  // Gender
  boolean sex;

  // Age
  int age;


public class Bieshu {

    double size;

    Ren zhuRen;

public class Test {
  public static void main(String[] args) {
    // thinking
    // First create human objects, and then create villa objects
    Ren zs = new Ren();
    // Do not use the system default value, and assign values manually = "456123"; = "zs"; = true;
    zs.age = 100;

    // Create a villa
    Bieshu fangzi = new Bieshu();
    // Do not use the system default value, and assign values manually
    fangzi.size = 500.0;

    // Let the villa have a master
    fangzi.zhuRen = zs;

    // Know the name of the owner of the house
    System.out.println("The name of the owner of the house: "+;

    // House for owner
    Ren ls = new Ren(); = "ls";
    fangzi.zhuRen =ls;
    System.out.println("The name of the owner after the replacement of the house: "+;

Notebooks and people

public class Student {

  int no;

  // name
  String name;

  Computer notepad;


public class Computer {
    String brand;
    String color;
    String style;

public class Test {
  public static void main(String[] args) {

    Student zs = new Student();
    Computer computer = new Computer();

    computer.brand = "ASUS"; = "c00";
    computer.color = "black"; = 123; = "zs";
    zs.notepad = computer;

    // Print three notebook brands
    System.out.println("Zhang San's notebook brand: " + zs.notepad.brand);

    // Modify brand
    zs.notepad.brand = "Mac";
    System.out.println("Zhang San's notebook brand: " + zs.notepad.brand);

    // Direct new
    // Zhang San bought a new computer. The computer was lost before
    zs.notepad = new Computer();
    System.out.println("Zhang San's notebook brand: " + zs.notepad.brand);



Three main memory of java:
Stack memory
Heap memory
Method area memory

The method area memory mainly stores code fragments and static variables

Stack memory activity is the most frequent, because methods are constantly calling and releasing, calling and releasing, that is, constantly pressing stack, bouncing stack, pressing stack, bouncing stack

Method has local variables, which are stored in stack memory.
If new is used, an object will be created, mainly to open up a space in heap memory.
The variable that holds the address of the object is called a reference.
If an object has no reference to it, the object will become garbage data and wait for the garbage collector to collect it.

The garbage collector is mainly for the heap memory among the three main memories.

Keywords: Java

Added by devassocx on Mon, 21 Feb 2022 03:36:39 +0200