Java Learning notes [5th-7th Chapter]

Source: Internet
Author: User
Tags class manager

Behavior of the object

Method Call Stack

All methods are maintained in a structure called the call stack, and the currently executing method is located in the call stack.

For a basic type of variable: the Java Virtual machine puts it on the stack.

For a variable of reference type: The reference variable itself is placed in the stack, and the object pointed to by the reference is placed in the Java heap. The reference does not contain the actual data of the object it points to, but instead points to the location of the object in memory.

If a variable is a reference type, the value of the variable is a memory address, which is the location of the object to which the reference refers.

Call by value

Refers to when a method call passes a parameter that is passed by a copy of the value.

Examples are as follows:

public class Temptest {

private void test1 (int a) {

Do something.

}

public static void Main (string[] args) {

Temptest t = new temptest ();

int a = 3;

T.test1 (a);//The parameter a passed here is passed by value

}

}

Passing important characteristics by value: Passing a copy of a value, that is, passing it off is unrelated.

Examples are as follows:

public class Temptest {

private void test1(int a) {

A = 5;

System.out.println ("a=== in the Test1 method" +a);

}

public static void Main (string[] args) {

Temptest t = new temptest ();

int a = 3;

T.test1(a);//After passing, the Test1 method changes the value of the variable without affecting the A

System.out.println ("a=== in the Main method" +a);

}

}

The result of the operation is: A===5 in the Test1 method

A===3 in the Main method


3.2: By reference what is referred to is when a method call, the passed parameter is passed by reference, in fact, the address of the reference to pass, that is, the variable corresponding to the memory space address.

3.3: An important feature passed by reference is a reference to a value, meaning that both before and after delivery point to the same reference (that is, the same memory space).

The example is as follows: line 1th public class Temptest {

private void Test1(a a) {

A.age = 20;

System.out.println ("age= in the Test1 method" +a.age);

}

public static void Main (string[] args) {

Temptest t = new temptest ();

A = new A ();

A.age = 10;

T.test1(a);

System.out.println ("age= in the Main method" +a.age);

}

}

Class a{

public int age = 0;

}

The results of the operation are as follows: Age=20 in Test1 method

Age=20 in the Main method

(1): "In Java parameter passing is passed by value" This sentence means: Pass by value is a copy of the value passed, by reference is actually passed the value of the referenced address, so collectively by value passed.

(2): There are only basic types in Java and strings that are defined in the following way are passed by value, others are passed by reference. is to define the string directly using double quotation marks: string str = "Java private";

Overloading of methods

When a class has two or more methods with the same name but a different argument list.

Examples of overloading are shown in the following example:

void Getarea (int w,int h);

void Getarea (float w,float h);

In the second case, the member Method Getarea () accepts two floating-point variables as its arguments, and the compiler determines which member method to call based on the different parameters that are called, and if you give two integers to the member method, the first member method is called; if you give two floating-point numbers to the member method, A second member method is called. When writing code to invoke one of these methods, it chooses the appropriate method based on the type of argument provided.
Note: As with the member method, the construction method can also be overloaded.
2.2: Overloaded rules for methods
(1): Method names must be the same

(2): The parameter list must be different (the number is different, or the type is different, or the parameter is arranged in different order).

(3): The return type of the method can be the same or different. Only the return type is not sufficient to be an overload of the method.

Array

An array is a collection of data that consists of several items of the same type. That is, the array is used to assemble objects of the same type and to refer to the collection by a name, and the array is a reference type.

Declaration method:

Declaration one: int x[];

Statement two: int [] x=new int[3];

Declaration three: Int[]x=new int[]{3,4,5};

The method of array copying is to use the Arraycopy () method provided by the system class with the following syntax:

System.arraycopy (object src, int srcpos, object dest, int destpos, int length);

System.arraycopy (source array, starting position in source array, target array, starting position in target data, number of array elements to copy);

public class T {

public static void Main (String args[]) {

int arr1[] = {1,2,3,4,5};

int arr2[] = new INT[5];

System.arraycopy (arr1, 0, arr2, 0, 5);

for (int i = 0; i < arr2.length; i++) {

System.out.println (Arr2[i]);

}

}

}

Arrays class

Some basic operations of an array, such as sorting, searching, and comparing, are common. The array is provided in Java to assist you with these operations, array is a class located in the Java.util package, and he provides several methods that can be used directly.

Sort () helps you sort the specified array by using the Quick Sort method

BinarySearch () allows you to search a sorted array for $ two, and returns the index of the value if the specified value is found, otherwise a negative number is returned

Fill () When you configure an array, the default value is given according to the data type. For example, an array of integers has an initial value of 0, and you can use the Arrays.fill () method to set all elements to the specified value

Equals () compares the values of the elements in the two array if they are all equal, and returns true if they are, otherwise false

Inherited

public class Employee {

String name;

Date HireDate;

Date dateOfBirth;

String JobTitle; int grade;

... }

public class Manager extends Employee {

String Department;

Employee[] Subordinates;

... }

In such a definition, the Manager class is defined with all the variables and methods owned by the Employee. All of these variables and methods are inherited from the definition of the parent class.

The key word is extends.

In the Java programming language, the initialization of objects is very structured, and this is done to ensure security. In the previous module, you saw what happened when a particular object was created. Because of inheritance, objects are completed, and the following behaviors occur sequentially:

(1) The storage space is allocated and initialized to a value of 0

(2) Perform an explicit initialization

(3) Call construction method

Keyword Super

The keyword super can be used to refer to the parent class of the class, which is used to refer to a member variable or method of the parent class. The behavior of the parent class is called as if the behavior is the behavior of this class, and the invocation behavior does not have to occur in the parent class, it can automatically trace back to the upper class.
Features of the Super keyword:

(1): Point to a data member that is hidden by the quilt class in the parent class

(2): Take a method that is already covered

(3): A method name represents the parent class construction method

For example:

public class Employee {

private String name;

private int salary;

Public String getdetails () {

Return "Name:" + name + "\nsalary:" + salary;}

}

public class Manager extends Employee {

Private String Department;
Public String getdetails () {

return super.getdetails () +//Call the method of the parent class "\ndepartment:" + department;

}

}

construction method cannot be inherited                                                          

Although a subclass inherits all methods and variables from the parent class, it does not inherit the constructor method, which is important to know. A class can be constructed with only two methods. or write a constructor method, or do not write the constructor method at all, the class has a default constructor method.

calling the Parent class construction method

In many cases, the default constructor method is used to initialize the parent class object. Of course, you can also use super to display the constructor method that calls the parent class.
public class Employee {

String name;

Public Employee (String N) {

name = N;

}

}
public class Manager extends Employee {

String Department;

Public Manager (string s, string d) {

Super (s);

department = D;

}

}

Note: Both super and this must be placed on the first line of the construction method.

Single Inheritance                          

Single inheritance: When a class inherits from a unique class, it is called single inheritance. Single inheritance makes your code more reliable. Interfaces provide the benefits of multiple inheritance, and there are no (multiple inherited) drawbacks. The Java programming language allows a class to inherit only one other class, that is, a class can have only one parent class. This restriction is called single inheritance. The advantages of single inheritance and multiple inheritance are topics that are widely discussed among object-oriented programmers. The Java programming language strengthens the single inheritance limit and makes the code more reliable, although this sometimes increases the programmer's work. Later, a language feature called Interface (interface) is learned that allows most of the benefits of multiple inheritance without being affected by its drawbacks.

Java Learning notes [5th-7th Chapter]

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.