Introduce the method-call stack and activation records in java.
3.1 Method-Call Stack and Activation Records
3.1.1 Stack data structure
Pushing: Placing a dish on the pile at the top
Popping: Removing a dish from the pile from the top
LIFO data structures: The last item pushed(inserted) on the stack is the first item popped(removed) from the stack. (last-in, first-out)
3.1.2 Activation Records
When a program calls a method, the called method must know how to return to its caller
The return address of the calling method is pushed onto the method-call stack
If a series of method calls occurs, the successive return addresses are pushed onto the stack in last-on, first-out order
The method-call stack also contains the memory for the local variables used in each invocation of a method during a program’s execution
Stored as a portion of the program-execution stack known as the activation record or stack frame of the method call
When a method call is made, the stack frame for that method call is pushed onto the method-call stack
When the method returns to its caller, the method’s activation record is popped off the stack and those local variables are no longer known to the program
If more method calls occurs than can have their stack frames stored on the method-call stack, an error known as a stack overflow occurs
Roles of the constructors:Initializing objects,instand of requesting memory.
Java requies a constructor that initializes an object of a class when the object is created.
Keyword newrequests memory from the system to store an object, then calls the corresponding class’s constructor to initialize the object.
A constructor must have the same name as the class.
By default, the compiler provides a default constructor with no paramaters in any class that does not explicitly include a constructor. ( 默认构造函数的访问范围与类的访问范围相同 )
Instance variables are initialized to their default values.
A constructor’s parameter list specifies the data it requires to perform its task.
Constructors can’t return values, so they cannot specify a return type.
If you declare any constructors for a class, the Java compiler will not create a default constructor for that class.