Java foundation 06 - the difference between process oriented and object-oriented, the concept of class and object, the definition of class, the creation and use of object

Posted by scorphus on Thu, 18 Nov 2021 03:54:21 +0100

The difference between process oriented and object-oriented

  • Process oriented: the main concerns are: the specific process of implementation, causality [development idea of integrated graphics card] [fried rice with eggs]
    • Advantages: for programs with simple business logic, rapid development can be achieved, and the early investment cost is low.
    • Disadvantages: it is difficult to solve very complex business logic by using process oriented development. In addition, process oriented development leads to very high "coupling" between software elements. As long as one of the links goes wrong, the whole system will be affected, resulting in poor "expansion" of the final software. In addition, because there is no concept of independence, component reuse cannot be achieved.
  • Object oriented: the main focus is: what functions can be completed by the object [independent solid]. [development idea of independent graphics card] [GaiFan]
    • Advantages: low coupling, strong expansion, easier to solve more complex business logic in the real world. Strong reusability of components.
    • Disadvantages: the investment cost in the early stage is high, which requires independent body extraction and a lot of system analysis and design.
  • c language is purely process oriented, c + + semi object-oriented and Java purely object-oriented
  • Most of the new programming languages are object-oriented. People understand the real world in an object-oriented way.
  • Object oriented is more in line with people's way of thinking.
  • Three characteristics of object oriented
    • encapsulation
    • inherit
    • polymorphic
    • All object-oriented programming languages have these three characteristics
  • Develop a software in an object-oriented way. In the life cycle: [OO object-oriented method is used throughout the whole life cycle]
    • Object oriented analysis: OOA
    • Object oriented design: OOD
    • Object oriented programming: OOP

Concepts of classes and objects

  • What is a class?
    • Class does not exist in the real world. It is a template, a concept and the abstract result of human brain thinking.
    • A class represents a class of things.
    • In the real world, object A and object B have common characteristics. They abstract and summarize A template, which is called class.
  • What is an object?
    • An object is an actual individual. In the real world.
  • Describe the whole software development process:
    • Programmers first observe the real world and look for objects from the real world
    • After searching for N objects, it is found that all objects have common characteristics
    • The programmer forms a template [class] in his brain
    • Java programmers can express a class through Java code
    • There are class definitions in Java programs
    • Then you can create objects through classes
    • After having objects, you can make objects cooperate directly to form a system
  • Class -- [instantiation] - > object
  • Objects are also called instances / instances
  • Object -- [Abstract] - > class
  • a key
    • Classes describe the common characteristics of objects
    • Common features such as height
    • When accessing this height feature, you must first create an object to access this feature through the object. Because the value of this feature is different after a specific object. Some objects are 1.80 in height and others are 2.80 in height.
  • What information does a class mainly describe?
    • A class mainly describes: state + action
      Status information: name, height, gender, age
      Action information: eating, singing, dancing, learning
    • Status – > Properties of a class
    • Actions – > methods of a class
  • Class{
    Attribute: / / describes the status information of the object
    Method: / / describes the action information of the object
    }
    Note: when the state and action are specific to an object, the final results may be different.
    Objects and objects have common characteristics, but there are data differences between them.

Class definition

  • Syntax structure:
 [Modifier list]class Class name{
	Properties:
	method:
}
  • Student class, which describes the common characteristics of all student objects:
    • What status information does the student object have
      • Student number [int]
      • Name [string]
      • Gender [boolean]
      • Age [int] [age is an attribute, age is a data, and there should be a data type if it is data]
      • Address [string]
      • ...
    • What action information does the student object have
      • having dinner
      • sleep
      • study
      • play
      • sing
      • dance
      • ...
    • Key: attributes are usually defined in the form of a variable [because the status information needs data to be represented, and the data needs to be stored in the variable]
      • int no;
      • int age;
      • ...
public class Student
{
	//Attribute [describes the status information of the object]
	//Attributes are usually defined as variables
	//In the class body, variables defined outside the method body are called "member variables"
	//The member variable is not assigned. The system assigns the value by default, and everything is in line with 0.
	
	//Student number
	int no;
	//full name
	String name;
	//Gender
	boolean sex;
	//Age
	int age;
	//address
	String address;
    //method
    //Method describes the action information of the object
    //The example here does not involve methods first
}
  • java includes two data types
    • Basic data type
      • byte,int,long,float,double,boolean,char
    • Reference data type
      • String. Class / / provided by sun
      • Student.class / / provided by sun
      • System.class / / customized by the programmer
      • User.class / / customized by the programmer
      • Product.class / / customized by the programmer
      • Customer.class / / customized by the programmer
      • ...
      • String username = "zhangsan" ;
      • Student s = ???;
    • All class es in the java language belong to reference data types

    Object creation and use

    Part I

//Student class
//The student class is a template
//Describe the common characteristics of all students [state + behavior]
//The current class only describes the student's status information [attribute]
//Student is a class and belongs to the reference data type. The type name is student
public class Student{
	//Class body - attribute + method
	
	//Attribute [stored data is in the form of variable]
	//Since variables are defined in the class body and outside the method body, such variables are called member variables
	//All students have student number information
	//But each student's student number is different
	//Therefore, to access this student number, you must first create an object to access the student number information through the object
	//Student number information cannot be accessed directly through "class", so this member variable is also called instance variable
	//Objects are also called instances, and instance variables are also called object variables. [object level variables]
	//If the object is not created, the memory space of the no variable does not exist. The memory space of the no variable will not be created until the object is created.
	int no;
}
//If the member variable is not assigned manually, the system defaults
//Default value
data typeDefault value
byte,short,int,long0
float,double0.0
booleanfalse
char\u0000
Reference data typeNull (null)
//Object creation and use
public class OOTest01
{
	public static void main(String[] args){
		//int is the basic data type
		//i is a variable name
		//10 is a literal of type int
		int i = 10;
		
		//N objects can be instantiated through a class
		//Syntax of instantiated object: new class name ()
		//new is an operator in the java language
		//The new operator is used to create objects and open up new memory space in the JVM heap memory
		//Method area memory: when the class is loaded, the class bytecode code fragment is loaded into the memory space
		//Stack memory (local variable): when a method code fragment is executed, it will allocate memory space for the method and press the stack in the stack memory
		//Heap memory: new objects are stored in heap memory
		
		//Student is a reference data type
		//s is a variable name
		//new Student() is a student object
		//s is a local variable [stored in stack memory]
		//What is an object? The memory space opened up by the new operator in heap memory is called an object
		//What is a reference? Reference is a variable, but this variable holds the memory address of another java object
		//In java language, programmers cannot directly operate heap memory. java has no pointers, unlike C language
		//In java language, programmers can only access instance variables inside objects in heap memory through "reference"
		Student s = new Student();

		//Syntax format for accessing instance variables:
		// Read data: reference. Variable name
		// Modify data: reference. Variable name = value
		
		int stuNo = s.no;
		String stuName = s.name;
		int stuAge = s.age;
		boolean stuSex = s.sex;
		String stuAddr = s.addr;

		System.out.println("Student number=" + stuNo);
		System.out.println("full name=" + stuName);
		System.out.println("Age=" + stuAge);
		System.out.println("Gender=" + stuSex);
		System.out.println("address=" + stuAddr);
		//Put it another way
		System.out.println("---------------------------");
		System.out.println("Student number=" + s.no);
		System.out.println("full name=" + s.name);
		System.out.println("Age=" + s.age);
		System.out.println("Gender=" + s.sex);
		System.out.println("address=" + s.addr);		

		s.no = 10;
		s.name = "jack";
		s.age = 20;
		s.sex = true;
		s.addr = "Beijing";
 		
	}
}
  • Java memory structure diagram of the above code:
public class OOTest01
{
	public static void main(String[] args){
		...//Above code
		//Then instantiate a new object through the class
 		//stu is a reference
 		//stu is also a local variable
 		//Student is the reference data type of the variable
 		Student stu = new Student();
 		System.out.pritnln(stu.no);//0
 		System.out.println(stu.age);//0
 		Sysetm.out.println(stu.sex);//false
 		System.out.println(stu.name);//null
 		System.out.println(stu.addr);//null

		//An error is reported during compilation. The instance variable no cannot be accessed directly by "class name"
		//Because no is an instance variable, an object level variable, and the variable is stored inside a java object, there must be an object first
		//The instance variable no can only be accessed through the object, not directly through the "class name"
		//System.out.println(Student.no);
	}
}
/*
be careful:
	Local variables are stored in stack memory
	The instance variable in the member variable is stored inside the java object in the heap memory (the member variable is a large concept and the instance variable is a small concept. When the member variable is such a concept that can only be accessed through the object (instance), it is called the instance variable)
	Instance variables are one copy for one object and 100 copies for 100 objects
*/
  • Java memory structure diagram of the above code:

    Part II

//Home address class
public class Address
{
	//Attribute [instance variable of member variable]

	//
	//city
	String city;

	//street
	String street;

	//Zip code
	String zipcode;
}
//User class
public class User
{
	//Attribute [the following are instance variables of member variables]

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

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

	//Home address
	//Address is a reference data type: represents a home address
	//addr is an instance variable
	//addr is a reference
	Address addr;
}
public class OOTest02
{
	public static void main(String[] args){
		
		//create object
		//u is a local variable
		//u is a reference
		//u save memory address User object pointing to heap memory
		User u = new User();

		//Outputs the value of the internal instance variable of the User object
		System.out.println(u.no);
		System.out.println(u.name);
		System.out.println(u.addr);
	}
}

Memory structure of OOTest02 code Figure 1

public class OOTest02
{
	public static void main(String[] args){
		//...... Above code
		//Modify the value of the instance variable inside the User object
		u.no = 110;
		u.name = "jack";//"jack" is a java object, belonging to a String object
		u.addr = new Address();

		//At present, only one reference "u" can be seen in the main method
		//Everything can only be accessed through u
		System.out.println(u.name + "Which city do you live in:" + u.addr.city);
		System.out.println(u.name + "Which street do you live on:" + u.addr.street);
		System.out.println(u.name + "Zip code:" + u.addr.zipcode);

		u.addr.city = "Beijing";
		u.addr.street = "Sunrise";
		u.addr.zipcode = "11111111";
		System.out.println(u.name + "Which city do you live in:" + u.addr.city);
		System.out.println(u.name + "Which street do you live on:" + u.addr.street);
		System.out.println(u.name + "Zip code:" + u.addr.zipcode);
	}
}

OOTest02 code memory structure figure 2

Part III

public class OOTest03
{
	public static void main(String[] args){
		//u is a reference
		//u is a local variable
		User u = new User();

		//Written in the previous version
		//u.addr = new Address();

		//A is a reference
		//A is a local variable
		Address a = new Address();
		u.addr = a;

		System.out.pritnln(u.addr.city);//null
	}
}

public class OOTest03
{
	public static void main(String[] args){
		//u is a reference
		//u is a local variable
		User u = new User();

		//Written in the previous version
		//u.addr = new Address();

		//A is a reference
		//A is a local variable
		Address a = new Address();
		u.addr = a;

		System.out.pritnln(u.addr.city);//null

		a.city = "Tianjin";
		System.out.pritnln(u.addr.city);//Tianjin
	}
}

Part IV

//Husband class
public class Husband
{
	//full name
	String name;
	
	//The husband object contains the wife reference
	Wife w;
}
//Wife class
public class Wife
{
	//full name
	String name;

	//Wife object contains husband reference
	Husband h;
}
//Test class
public class OOTest04
{
	public static void main(String[] args){
		//Create a husband object
		Husband huangXiaoMing = new Husband();
		huangXiaoMing.name = "Huang Xiaoming";

		//Create a wife object
		Wife baby = new Wife();
		baby.name = "baby";
		
		//Marriage [find a wife through a husband, and you can find a husband through a wife]
		huangXiaoMing.w = baby;
		baby.h = huangXiaoMing;

		//Get the name of Huang Xiaoming's wife above
		System.out.println(huangXiaoMing.name + "My wife's name:" + baby.name);
		System.out.println(huangXiaoMing.name + "My wife's name:" + huangXiaoMing.w.name);
	}
}

Memory structure diagram

Part V

Description of JVM memory management

  • 1.JVM(Java virtual machine) mainly includes three memory spaces: stack memory, heap memory and method area memory
  • 2. There is one heap memory and one method area memory, one thread and one stack memory
  • 3. 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.
  • 4. The stack mainly stores local variables in the method body
  • 5. The code fragments of the method and the whole class are stored in the memory of the method area. These code fragments will be loaded when the class is loaded.
  • 6. The java object created by using the new operator during program execution is stored in heap memory. There are instance variables inside the object, so the instance variables are stored in heap memory.
  • 7. Variable classification:
    • Local variable [declared in method body]
    • Member variable [method declaration]
      • Instance variable [the front modifier has no static]
      • Static variable [the front modifier has static]
  • 8. Static variables are stored in the method area memory.
  • 9. Among the three pieces of memory, the stack memory changes most frequently, the method area memory is the first to have data, and the garbage collector is mainly aimed at heap memory
  • 10. When will the garbage collector [automatic garbage collection mechanism, GC mechanism] consider recycling the memory of a java object?
    • When the java objects in the heap memory become garbage collected data, they will be collected by the garbage collector.
    • When do java objects in heap memory become garbage?
      • When there are no more references to it.
      • This object cannot be accessed because the access object can only be accessed by reference.

Part VI

//Customer class
public class Customer
{
	//id
	int id;
}
//Test class
public class OOTest05
{
	public static void main(String[] args){
		
		Customer c = new Customer();
		System.out.println(c.id);//0
		
		c = null;
		//The following program can be compiled because it conforms to the syntax
		//Null pointer exception during operation
		//Null pointer exception must occur when null reference accesses data related to "instance"
		//java.lang.NullPointerException
		System.out.println(c.id);
	}
}
//Instance related data means that objects must participate in the data access. This data is instance related data

Memory structure diagram

Topics: Java