First
Section
member Variables
1.1
member variables and local variables
Member variables: declared under the class, outside of the method, acting on the entire class;
Local variables: Declarations are under methods and are used under methods.
1.
2 member variable default value
The default value of the member variable is the same as the default value for the array:
Integer type: 0
Floating-point type: 0.0
Character Type: \u0000
Boolean type: False
String type: null
1.
3 The difference between a member variable and a local variable
1. From the location of the declaration: local variable declaration in a method, the member variable is declared below the class.
2. From the default values: Local variables have no default values, and member variables have default values.
3. From the life cycle: Local variables open up storage space as the method is called, and the method call ends.
Frees storage space, and member variables disappear as the object disappears.
4. From the storage location aspect: Local variables are stored in stack memory, and member variables are stored in heap memory.
Second Section JVM Memory Management in object-oriented
Class 2.1 file name
A class file is a code file that is suffixed with. Java, allowing only one public class to appear in each class file.
When there is a public class, the name of the class file must be the same as the name of the public class, and if no public exists,
The name of the class file can be any name (names that begin with a number are not allowed).
2.
2
stack memory, heap memory, and method area
Java's JVM's memory can be divided into 3 extents: heap (heap), stack (stack), and method area.
2
.
2
. 1
Heap Area:
1. objects and member variables are stored, and each object contains a class-corresponding information. (The purpose of class is to get operation instructions)
2.JVM only one heap area (heap) is shared by all threads, and the heap does not hold basic types and object references, only the object itself
2.2.2 Stack area:
1. Each thread contains a stack where only objects of the underlying data type are saved and references to custom objects (not objects), objects are stored in the heap area
2. The data in each stack (original type and object reference) is private and other stacks are inaccessible.
3. The stack is divided into 3 parts: the basic type variable area, the execution environment context, the operation instruction area (holds the operation instruction).
2.2.3 Method Area:
1. Also called the static zone, like the heap, is shared by all threads. The method area contains all class and static variables.
2. The method area contains elements that are always unique throughout the program, such as the class,static variable.
2.
3 Execution of various types of data in memory during object creation
Run class procedure: Method area Find Method--instantiation object in heap--call stack (point to instance in heap) (Error??????)
Previous notes are questionable, confirmed and updated to be added .......... ...
Summarize:
Stack memory: Basic data type, object reference;
Heap Memory: Everything new out of the object, member variables;
Method area: Class file, static variable, method;
Third
Section
constructor Function
3.1
constructor Function
1, meaning: Initialize the member variable (class);
2. Classification: parameter-free constructor function
3.
3 this keyword
The role in Java:
1. Represent itself (this class) this. Member variable
2. You can call the related constructors of this class
No parameter: This (parameter list);
There are no parameters: this ();
Note:
1. The This keyword must be placed in the first row of the constructor when calling its own constructor, otherwise error;
2, there is no parameter adjustment, there is no parameter can only save one, can not exist at the same time;
Routine: Dog.java
3.3 Characteristics of the constructor function
1. No return value (no write void).
2. The method name must match the class name.
No parameter constructor:
Grammar:
public class name () {
Method body
}
There are parametric constructors:
Grammar:
public class name (argument list) {
Method body
}
Routine: Dog.java
1 PackageGouzao_this;2 3 Public classDog {4 5 String kind;6 String name;7 intAge ;8 9 //no parameter constructorTen PublicDog () { One //without parameters, the constructor of this call itself must be placed on the first line or an error will be A //This ("Rhubarb", "Earth Dog", 9); - - //there can be no mutual calls between participating without a call only one cannot exist at the same time theKind = "Erha"; -Name = "Shagou"; -Age = 3; -SYSTEM.OUT.PRINTLN ("No parameter Call"); + } - + //Parametric Constructors A PublicDog (String name,string Kind,intAge ) { at //This (); //There is no reference to the argument - - //The This keyword invokes the class itself - This. Name =name; - This. Kind =kind; - This. Age =Age ; inSystem.out.println ("called With reference"); - } to + Public voidShow () { -System.out.println ("This is called" +name+ "of" +kind+ "this year" +age+ "years old"); the } * $}
View Code
Routine: Testing the Dog class:
1 PackageGouzao_this;2 3 Public classTextdog {4 5 Public Static voidMain (string[] args) {6 7Dog Dog1 =NewDog ();//call the parameterless constructor8Dog dog2 =NewDog ("Rhubarb", "Earth Dog", 4);9 dog1.show ();Ten dog2.show (); One } A -}
View Code
Thinking:
1. Can I have retrun keywords in the constructor?
Yes, just write return; As a terminator;
2. Can the parameter list control the number of parameters in the argument constructor (can the quantity be inconsistent with the member variable)?
Yes, to initialize several variables and initialize a few variables
3. The parameter constructor and the parameterless constructor can exist independently?
Yes, but it's best to write it in the class.
Note:
1, the constructor executes when the object function is created, that is, the creation of the object is a call to the constructor of the class;
2, if you do not create a constructor, the system will default to add a parameterless constructor, the new object will automatically call the parameterless constructor;
3, when the parameter constructor is added, the system will not add an parameterless constructor at this time, you need to add the parameterless constructor manually, if you do not add the parameterless constructor, you can not create the non -parameter object;
Practice:
using object-oriented thought to implement the color ball random array, the maximum value of generating random number can be changed arbitrarily.
Reference code:
1 PackageGouzao_this;2 3 Importjava.util.Arrays;4 ImportJava.util.Random;5 6 Public classDoublecolerball {7 8 Public voidGetnum (intRedballnum,intblueballnum) {9 Ten int[] RedBall =New int[6]; One intIndex =0; A inttemp = 0; - intBLUEBN = 0; - theRandom r =NewRandom (); - //generate a random number of red balls -redball[index++] = R.nextint (redballnum) +1; - +Loop while(true) { - +temp = R.nextint (redballnum) +1; A for(intt=0; t<redball.length;t++) { at - if(redball[t]==temp) { - - ContinueLoop; - - } in}//For Loop - toredball[index++] =temp; + if(index==6) { - the Break; * } $ Panax Notoginseng } - the + //Blue ball Random number ABlueballnum = R.nextint (blueballnum) +1; the + int[] Ball = arrays.copyof (RedBall, redball.length+1); - $ball[ball.length-1]=Blueballnum; $ -SYSTEM.OUT.PRINTLN ("The resulting array is:" +arrays.tostring (Ball)); - the } - Wuyi}
View Code
1 PackageGouzao_this;2 /**3 * 4 * @ project name: oopday02m5 * @ MODULE function: Realize color ball with object-oriented thinking method,6 * The maximum value of the generated random number can be arbitrarily changed. 7 * @ Module version:8 * @JDKVersions: JDK 8.09 * @author: KanekiyiTen */ One Public classTEXTDOUBLECB { A - Public Static voidMain (string[] args) { - theDoublecolerball DCB1 =NewDoublecolerball (); - //Enter the maximum number of digits generated by the red ball Blue ball -Dcb1.getnum (37,18); - } + -}
View Code
Java Object-oriented (ii): Memory management in member variable-oop-constructors