Java2 Practical Tutorials (5th edition) important points and missing points (ii)

Source: Internet
Author: User
Tags garbage collection

3.2 Statement Overview

A good point is also a statement, called an empty statement.

3.3.1 If statement

If (An expression)

The value of an expression within a heap of parentheses () after an if must be a Boolean type

3.3.2 If-else Statements

If-else (expression)

The value of an expression within a heap of parentheses () after an if must be a Boolean type

3.4 Switch Switch statement

Switch (expression)

{

Case Constant Value 1:

Statement

Break

Case Constant Value 2:

Statement

Break

......

Default

Statement

}

The value of an expression, a constant value can be a byte,short, int, char type

3.7 For statements and arrays

Format: for (declaring loop variable: The name of the array) {

......

}

The loop variable takes the value of each element of the array sequentially

4.2.1 class declaration

1. First letter of class name using uppercase letters

2, multiple words compound into the class name, the first letter of each word should be capitalized

4.2.3

The first letter of the variable name is lowercase, and if the variable name consists of multiple words, the first letter of the other word starting with the 2nd word is capitalized

4.2.4 method

Member variables, local variables

1. The parameters of variables and methods declared in the method body are called local variables.

2. Unlike member variables of a class, a local variable is only valid within a method and is related to its declared location.

3, if the name of the local variable in the method is the same as the name of the member variable, then the method hides the member variable , and if you want to use the hidden member variable in the method, you must use the keyword this.

4. The member variable has a default value, and the local variable has no default value, so it must be initialized.

5, the operation of the member variables can only be placed in the method

UML diagrams for the 4.2.6 class

UML diagrams belong to a structure diagram

The 1th layer is the name layer , if the class name is a regular glyph , indicating that the class is a concrete class

If the name of the class is italic , it indicates that the class is an abstract class

The 2nd layer is the variable layer , also called the attribute layer , lists the class's member variables and types, the format " variable name: Type "

The 3rd layer is the method layer , also called the operation layer , lists the methods in the class, the format " method name ( parameter list): Type "

4.3.2 Creating objects

Create an object that includes the declaration of an object and assigns a variable to an object two steps

1. Declaration of objects

the name of the class object name;

2. Assigning variables to Objects

The object is created by assigning a variable to the declared object using the new operator and the constructor of the class .

The memory model of the object

1 . Memory model when declaring objects

Xiyoujirenwu Zhubajie;

After declaring the object variable Zhubajie with the class Xiyoujirenwu, Zhubajie has no data in memory, the Zhubajie is an empty object , the empty object cannot be used , the object must be assigned a variable, The object is assigned an entity.

2. Memory model after assigning a variable to an object

New Xiyoujirenwu ();

The new operator and the construction method do two things:

1. Allocate memory space for member variables of the Xiyoujirenwu class, and then execute the statements in the constructor method.

If the member variable does not specify an initial value at the time of Declaration, and the constructor used does not initialize the member variable, the default initial value for the member variable of the integral type is 0, and for the floating-point Default initial value is 0.0, for the default initial value of Boolean is false, which is null for reference-type default initial values .

2. After allocating memory space for a member variable of a class, the new operator calculates a hexadecimal number called the referenced value (which contains important information that represents the memory location of these member variables and related), which is the expression New Xiyoujirenwu () is a value.

3. The new operator assigns a variable to the object

Zhubajie = new Xiyoujirenwu ();

1. Assigning this reference value to the object Zhubajie, the member variables of these classes that allocate memory space are managed by the Zhubajie operation.

2. The member variables of these classes that allocate memory space are entities that belong to the object Zhubajie.

3. Assigning references to Zhubajie,zhubajie objects can be manipulated to manage these entities.

4. After allocating the memory space and calculating the reference, assign the reference to the object, and the object has a member variable that is allocated memory by the new operator, that is, the new operator assigns a variable to the object.

Note: The object's reference exists in the stack, and the object's entity (the variable assigned to the object) exists in the heap.

The advantage of the stack is that the access speed is faster than the heap. The disadvantage is that the size of the data in the stack and the lifetime must be deterministic and inflexible.

The advantage of the heap is that the memory size can be dynamically allocated, and the life cycle is no better than telling the compiler beforehand, but the access speed is slow.

3. Create multiple different objects

A class can create several different objects by using the new operator, and the variables of those objects will be allocated different storage spaces.

4.3.4

1, avoid the use of empty objects , that is, you can not let an empty object to invoke the behavior of the method, or the program will run with exception nullpointerexception.

2. Garbage collection mechanism

Java has a so-called garbage collection mechanism, which periodically detects whether an entity is no longer owned by any object, and if such an entity is found, frees up the memory that the entity occupies.

Class 4.4 and the basic structure of the program

Save the Java source files involved in the application in the same directory, although a Java source file can have more than one class, it is still recommended to write only one class in a Java source file.

4.5.2 the value of the basic data type parameter

For parameters of the base data class, the level of the value passed to the parameter is not higher than the level of the parameter

4.5.3

"Pass value" passes the "reference" stored in the variable, not the entity referenced by the variable.

4.5.4 variable Parameters

public void f (int ... x)//x is called "parameter representation" of a variable parameter in the parameter list of method F

{

}

In the parameter list of a method, you can declare multiple parameters of the same type , without giving the name of the last parameter, starting with an item.

A parameter represents a specific parameter in a parameter list that can be represented by subscript operations. i.e. x[0], x[1]......x[m-1]

X.length equals the number of arguments represented by X

For mutable parameters, Java provides an enhanced for statement that allows you to use the For statement to traverse the parameters represented by the parameter list as follows:

For (declares loop variable: parameter represents)

{

......

}

for (int param:x)

{

Sum=sum+param;

}

4.6 Combination of objects

A member variable of a class can be any data type allowed by Java

A Class A can take an object of a different Class B as its own member variable, then class A produces an object that contains the object of Class B, and the object generated by Class A will take the object produced by Class B as its own component, called Has-a.

4.6.1 Combination and Reuse

There can be no inheritance or interface between the two classes, only one object in Class B is written to Class A, which is a member variable in Class A. Write a method in Class A at the same time, the parameter of the method is class B object

void Setbottom (Circle c) so that after declaring an object of Class B, a reference to the object is passed to the Class B object declared in Class A by invoking a reference passing method in Class A. This allows the method of Class B to be used by object manipulation of Class A, which is called object A to reuse The method of object B in a composite way . an object that declares an instantiated class B can be re-instantiated, but does not affect the Class B object in Class A.

public class sim{

Long number;

SIM (long number) {

This.number = number;

}

Long GetNumber () {

Return number;

}

}

public class mobiletelephone{

Sim Sim;

Void Setsim (SIM card) {

Sim = Card;

}

Long Looknumber () {

Return Sim.getnumber ();

}

}

public class example4_9{

public static void Main (String args[])

}

Java2 Practical Tutorials (5th edition) important points and missing points (ii)

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.