Java Basics Review 2-6

Source: Internet
Author: User

If statement

If else struct shorthand: variable = (conditional expression)? Expression 1: Expression 2;

Ternary operators:

Benefit: You can simplify if else code.

Cons: Because it's an operator, you have to have a result after the operation.

Switch statement

The IF and switch statements are similar.

What is the specific scenario, and which statement to apply?

If the exact value of the judgment is not many, it conforms to the four types of byte short int char.

Although both statements are available, it is recommended that you use a switch statement. Because the efficiency is slightly higher.

Other cases: Judging the interval, the result is a Boolean type, and the use of if,if is more extensive.

While and do while differences

While: The condition is judged first, and the loop body is executed only if the condition satisfies.

Do While: The loop body is executed first, then the condition is satisfied, and then the loop body is resumed.

A simple sentence: Do while: the loop body executes at least once, regardless of whether the condition is satisfied.

for (initialize expression; loop condition expression; post-loop action expression) {EXECUTE statement}

Example: for (int x = 0;x<3;x++) {System.out.println ("x=" +x);}

1. The variable has its own scope. For the for: if the increment that is used to control the loop is defined in the For statement, then the variable is valid only within the FOR statement. The For statement is executed and the variable is freed in memory.

2.for and White can be interchanged, and if you need to define a loop increment, use for is more appropriate.

Summarize:

When to use the loop structure:

The loop structure is used when you want to execute a number of statements many times.

For the print rectangle, the outer loop controls the number of rows, and the inner loop controls the number of columns per row.

Function

1. Since the function is a separate function, then the result of the operation of the function is what is defined first, because this is the return value type in the explicit function.

2. In the process of defining the function, it is necessary to determine whether unknown content is involved in the operation, because it is the parameter list (the type of parameter and the number of arguments) in the specified function.

Array

A collection of data of the same type, an array is a container that automatically numbers the elements in the array from 0 to facilitate operation.

Use the For loop to iterate through the array, manipulate the group,

Select sort

Bubble sort

Two ways to check binary

Binary conversion

PS: In fact, these to the later stage will have a good package, direct reference on the good, Java is demand-oriented, what needs to find what is good, copy, I am a small move.

Array

One-dimensional array int [] x; int x [];

Two-dimensional array int [] [] y; int y []; int [] y [];

Object-oriented : Three features: encapsulation, inheritance, polymorphism;

Later development: In fact, is to find objects to use, no object on their own to build a slightly. Find objects, build objects, use objects, and maintain the relationships of objects.

class and object relationships: A class is a description of things in real life, objects that are real entities of such things. Mapped to Java, the description is class-defined classes that correspond to Java in heap memory using new to establish entities.

member variables and local variables:

Member variable: acts on the entire class. In heap memory, it exists in memory because the object exists.

Local variable: Acting in a function, or in a statement. exists in the stack memory.

Anonymous objects

Encapsulation: Hides the properties and implementation details of an object, providing public access to the outside. Set and get

When an object is established, the corresponding constructor is called.

constructor function: can be used to initialize an object.

Small details of the constructor: When a constructor is not defined in a class, the system defaults to a constructor that adds an empty argument to the class.

When a constructor is customized in a class, the default constructor is gone.

Constructors and general functions differ in their notation.

are also different on the run.

Constructors are run when an object is established. Initializes the object.

The general method is that object invocation is performed, giving the object the ability to add objects.

When an object is established, the constructor runs only once. The generic method can be called multiple times by the object.

When do you define a constructor?

When analyzing things, the function exists with some attributes or behaviors, then the content is defined in the constructor.

To construct a code block:

Function: initializes the object.

The object runs as soon as it is established, and takes precedence over the constructor execution.

To construct the difference between a block of code and a constructor:

Constructing code blocks is a unified initialization of all objects, and constructors are initialized to the corresponding objects.

The initialization content of different object commonalities is defined in the Construction code block.

This : is used to differentiate between local variables and member variables with the same name.

This represents the object of this class, in the end, which one does it represent? Represents the reference to the object to which the function belongs. Simply put, which object is the one that is calling this function, this represents which object.

This applies: when defining a function in a class, the function is used internally to invoke the object of the function, and this is used to represent the object, and this is used for the purpose of using this class of objects internally.

This statement: used to call each other between constructors. You can only define the first row in the constructor, because initialization is performed first.

Static: Statics

Usage: is a modifier used to modify members (member variables, member functions), when the member is statically decorated, there is a more than one invocation, in addition to being called by the object, can also be directly called by the class name. Class name, static member.

Static Features:

1. Load as the class loads. That is, the static disappears as the class disappears, which means that his life cycle is the longest.

2. Precedence over the existence of the object, make it clear that the static exists first and the object is behind.

3. Shared by all objects.

4. Can be called directly by the class name.

The difference between an instance variable and a class variable:

1. Storage location.

    class variables exist in the method area as the class is loaded.

    instance variables exist in heap memory as the object is established.

2. Life cycle:

    The class variable has the longest life cycle and disappears as the class disappears.

    The instance variable life cycle disappears as the object disappears.

Static Use precautions:

1. Static methods can only access static members.

Non-static methods can access both static and non-static.

2. The This,super keyword cannot be defined in a static method.

This is not available in a static method because static overrides the existence of the object.

3. The main function is static.

main function: is a special function. As the entry for the program, it can be called by the JVM.

The definition of the main function:

Public: Represents the maximum access permission for the function.

Static: Represents the main function that already exists as the class is loaded.

void: The main function does not have a specific return value.

Main: Not a keyword, but a special one, can be recognized by the JVM,

(string "arr): The parameter of the function, the parameter type is an array, and the element in the array is a string." An array of string types.

The main function is in fixed format: JVM recognition.

Why use static?

In both respects, because the statically decorated content has member variables and functions .

When do you define static variables (class variables)?

When shared data is present in an object, the data is decorated statically.

The unique data in the object to be defined as non-static exists in heap memory.

When do you define static functions?

This function can be defined as static when there is no access to non-static data (unique data for the object) within the function.

PS: each application has common features that can be extracted, individually encapsulated, for reuse.

Although these tool methods can be used by building Arraytool objects, the array is manipulated to find the problem:

1, the object is used to encapsulate the data, but the Arraytool object does not encapsulate the unique data.

2, each method of manipulating an array does not use the unique data in the Arraytool object.

At this time to consider, let the program more rigorous, do not need the object, can be Arraytool in the method is defined as static, directly through the class name can be called.

After the method is static, it is easy to use, but the class can also be used by other programs to establish objects, in order to be more rigorous, forcing the class cannot establish objects, you can do this by privatizing the constructor .

Person p = new person ("Zhangsan", 20);

What has been done in this sentence?

1. Because new uses Person.class, the Person.class file is first found and loaded into memory.

2. Execute the static block of code in the class, and if so, initialize the Person.class class.

3. Open up space in heap memory and allocate memory address.

4. Establish the unique properties of the object in heap memory and initialize it by default.

5. Display initialization of the properties.

6. Construct the code block initialization for the object.

7. Initialize the corresponding constructor for the object.

8. Pay the memory address to the P variable in the stack memory.

(Rain, written on July 13, Guangzhou, reprint please indicate the source, thank you)

    

Java Basics Review 2-6

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.