Javase Second Study essay (II.)

Source: Internet
Author: User
Tags array length modifier

Multilayer nesting jumps in a looping structure

targeta:for (int i = 0; i <; i++) {for (int j = 0; J < + j + +) {if (i + j = =) {//break targeta;continue target A;}}}

  

Array

* A one-dimensional array in which the value (direct data) is stored in a two-dimensional array with the address of a one-dimensional array
* Two-dimensional and one-dimensional arrays are two-dimensional arrays of data types: data type + [] + [] Create format type [] name = new
* Type[count][count_1]; Count is the number of one-dimensional arrays count_1
* is a two-dimensional array of the length of a one-dimensional array of recommended values (can not write it)!!!! If there is a one-dimensional array length that is not as long as it is, it will not take effect

Int[] A = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};int[] B = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4};int[][] c = new int[2][] ; c[0] = a;c[1] = b;for (int i = 0; i < c.length; i++) {int[] js = c[i];for (int j = 0; J < Js.length; J + +) {int num = Js[j]; System.out.print (num);} System.out.println ();}

  

Enhanced for Loop: foreach starts the loop and takes the first element out of the array: the variable in the front is used to iterate over the array behind it:

For (int[] is:c) {for (int i:is) {System.out.print (i);} System.out.println ();}

  

Learned the cycle can learn the sort of ~ after sorting can be used to find the two points

        Bubble sort//Select sort public static int[] Bubblesort (int[] arr) {int sum = 0;for (int i = 0; i < arr.length-1; i++) fo R (Int j = 0; J < arr.length-i-1; j + +) if (Arr[i] > Arr[j]) {arr[i] ^= arr[j];arr[j] ^= arr[i];arr[i] ^= arr[j];s um++;} SYSTEM.OUT.PRINTLN (sum); return arr;} public static int[] Selectsort (int[] arr) {int sum = 0;for (int i = 0; i < arr.length-1; i++) for (int j = Arr.length -1; J > i; j--) if (Arr[i] > Arr[j]) {arr[i] ^= arr[j];arr[j] ^= arr[i];arr[i] ^= arr[j];sum++;} SYSTEM.OUT.PRINTLN (sum); return arr;} public static int[] Binaryfind (int[] arr, int num) {int startIndex = 0;int EndIndex = Arr.length;int Middleindex = 0;int I  Ndex = -1;while (endindex-startindex >= 0) {Middleindex = (EndIndex + startIndex) >> 1;int sbound = Middleindex -1 > 0? MIDDLEINDEX-1:0;if (num >= arr[sbound] && num <= arr[middleindex]) {index = Sbound;break;} else if (num > Arr[middleindex]) {startIndex = Middleindex + 1;} else if (num < aRr[middleindex]) {endIndex = middleIndex-1;}} int arr1[] = new Int[arr.length + 1];int j = 0;for (int i = 0; i < arr.length; i++) {arr1[j++] = arr[i];if (i = = index) arr1[j++] = num;} Arrays.tostring (arr1); return arr1;}

  

The next is to start the object-oriented, (grumbling: I have objects, but she is not around me, some miss her, although there are friends in the side or a little empty)

* Object-oriented first knowledge
* The process of development: constantly creating objects, using objects, directing objects to do things. Design classes are the relationships between managing and maintaining objects
* Process-oriented emphasis on functional behavior, focus on problem-solving steps
* Object-oriented encapsulation function path object, emphasizing the function of the object, focus on the problem of the need to solve the object
* Object-oriented more in line with the human way of thinking, so that programmers from the executor to the conductor
* Object-oriented is more advanced than process oriented
* Object oriented and process oriented is a kind of programming idea
* Object-oriented based process oriented

* For classes, things that have similar functions and characteristics can be abstracted into a class
* After the method call for the object of the class, the system releases it directly
* After the object of the class is used, the system will call the GC and release him

* Characteristics of member variables
* The default value is given when the object is created
* You can assign values directly when you define an object in a class
* Can be used directly in methods that are not modified by static

* Comparison of member variables and local variables
* Scope member variables are scoped to the object of the class, and the scope of the local variable is the method
* Default value member variable has default value, local variable does not have default value must first initialize to use
* Release mechanism member variables and classes keep consistent local variables are released immediately after use (the life cycle of local variables and methods is the same ma?)
* Storage mechanism member variables are placed in the heap area of the object in the method of local variables placed in the stack area
*
*
* Member variables are divided into static and non-static (static)
* Static: Static members and methods
* Non-static: Nonstatic members and methods
* Static member variables and put methods can be called with reference or with the class name
* Non-static member variables and methods can only be called by reference

In fact, the construction method is also a static method

* Call construction Method Composition: Class name + ()
* Function: Initialize the properties of the object, when there is no custom construction method, can call the system default method of non-parametric construction
*
* How to construct a custom class:
* No parameter + no reference
*
* Basic Composition: Modifier Method Name parameter list method body
* No return value, same as class name
*
* Once a custom class is constructed, the system will no longer have a default construction method
* The interior of the construction method automatically initializes the properties
* Multiple constructor methods are overloaded relationships
*
* Parameterless constructor initializes member variables to null or 0 or FALSE, etc.
* The parameter constructor initializes and assigns the member variable to the value passed in by the parameter.
* In the parameter method, when the method parameter and the member variable name are identical, the method interior is the nearest principle when the parameter is based on other parameters,
* Nearest principle when considering the nearest principle from the memory point of view
*
* For classes: This is a reference type, is a reference data type, represents the current object, and holds the address of the current object
* Use this when you need to get a reference to the current class within a class.
*
* The role of this:
* Distinguish between member variables and local variables
* Call other overloaded construction methods in the constructor method to improve code reusability and simplify code
*
* When calling other methods through this, it must be placed in the first line of the current method.
* Cannot invoke each other and cause a dead loop
* This () as method applies only to construction


Basic operating procedures for program. Java programs in Java (69220153?locationnum=5&fps=1)
>> javac Example.java >> java example.class >> JVM calls ClassLoader loads Example.class into the method area, when the class is executed with the main side Law as the entrance
Enter execute subsystem >> any method that is called by the thread will open up new memory space in the stack area so the execution of the method is non-interfering in the multithreaded environment, but the member variables are stored in the heap area
So they will interfere with each other; >> when there are new objects in the method, pass Classforname (xxx.xxx.xxx). newinstance (); The process of creating a class is to see if the method area has a corresponding. class file,
If you create an object with newinstance, it is not loaded by the full path of the class. Class and then create the object (new object in the heap);

For static member variables and static member methods inside a class, stored in the method area, static member variables exist in the static domain of the method area, and static methods exist in the class's. class binary information (. class
The binary information in is not the same as in the method area, the read. class file is stored in the method area in the format required by the JVM, which includes data structure aspects, static member variables and static methods make it unnecessary to create objects.
Class is ready to use after the load is initialized, and the thread is shared

The variable declared in the method, that is, the variable is a local variable, each time the program calls the method, the system will establish a method stack for the method, the variable declared in the method is placed in the method stack, when the method ends the system will release the method stack, corresponding to the method declared in the stack of the variable with the end of the destruction, This is why local variables can only be valid in a method

* Static code blocks: loaded as the class loads, executed only once throughout the program, with highest execution precedence (above main)
* Construct code block: As the class object is loaded, each creation object is executed once
* Static code blocks: initialization of classes
* Construct code blocks: initialization of objects

Final
1, modifier class, variable (member variable, static variable, local variable), function.
2, the modified class cannot be inherited.
3, the modified function cannot be overridden.
4, the modified variable is a constant and can only be assigned once.
5, using the final modifier not only keeps the object's reference from changing, but also protects the object's life cycle in the callback method, so this is the fundamental meaning of the final and final modified parameters
A final modified variable can only be assigned once, and because of the difference in the mechanism of the assignment, there are two cases:
Member variables: Because the system automatically assigns the initial value after allocating memory, this makes the variable meaningless, so you need to assign a value when defining a member variable, or assign it to a code block, constructor
Local variables: There is no assignment, the system does not allocate it, the stack memory, so can be defined can be assigned or not assigned value, but the parameter as a local variable, the system when the parameter is passed is assigned value, so the parameter can no longer
The second change.
Macro variable: A variable is final decorated and is determined at compile time. Then this variable can be called a macro variable. A macro variable can point a reference to a constant pool.
(51026840/) constant pool

Unwanted data that appears in the program is dirty
Causes the variable to be assigned directly
Solution: Change the member variable to private variable accessing him indirectly through methods;

Encapsulation: (as described by the encapsulation of attributes) the access to member variables is privatized and accessed through a public method. Benefits: Improved accessibility of access security code
Filter and re-assign values inside a method

Inherited
* Special class objects have all the properties and behaviors of a generic class, called the inheritance of a special class to a generic class. A class can be a special class of more than one generic class, or it can inherit properties and behavior from multiple generic classes, but in the Java language, forbid
* Allows a class to inherit properties and behaviors from multiple generic classes, that is, in the Java language, only single inheritance is supported.
* In software development, the existing data types can be used to define the new data types through the inheritance mechanism. The new data type that you define has not only the newly defined members, but also the old members. Therefore, the inheritance of classes makes
* The established software is open, open and extensible, which is an effective method of information organization and classification, through the inheritance of the class, so that the common characteristics can be shared, simplifying the creation of objects, classes, add code
* Re-usability.
* Rewrite
* The constructor method automatically calls super () by default;
* Using construction methods in inheritance
*
* *
* 1. When there is only one constructor with parameters in a subclass, only parameters can be used and no arguments can be used. If you want to use it, you must manually create an argument-free construction method
* 2. When the parent class has only a constructor with parameters, the constructor of the subclass must manually call the parent class with the constructor of the parameter (super (parameter)) in the first row
* 3. When we create a construction method, if we do not write super (), the system will automatically call
* Cause: There are also attributes in the parent class to initialize, and the properties of the object must be initialized by their own constructor, so super () must be called, so there is a super () by default in each constructor method
*
* Why put Super on the first line of the method?
* A: The properties of the parent class may be used in the constructor of a subclass, and the property must be initialized before it is used, otherwise it cannot be used.
*
* In short: In the inheritance system, the best way to be a parent is to write the non-parametric construction method and the constructive method of the argument.
* Rewrite: When a subclass has the same name as the parent class (method name, parameter, etc.), we are called rewriting.
* Note: The equivalent of a subclass of the same name method overrides the parent class, and no longer calls the parent class's method.
*
* Function: Under the premise of not changing the name of the parent class method, we implement some functions on the basis of the function of the original method of the parent class. The expansion of the original function is realized.
*
* This: is a reference data type that represents the current object. The address of the current object is saved
* Super: Not a reference data type. You can call members of the parent class through Super
*
* Precautions:
* 1. Private methods cannot be overridden
* 2. The child class has a method with the same name greater than or equal to the parent class.
* 3. Static methods can only override static methods

* Interface interface
* The name of the interface interface {
* Member variable default public final static
* Member method default public abstract
* General interface does not write member variables, write-only methods and rules and the interface is called the method list
* Sub-class implementation method required
* Inherit extends
* Achieve implements
*
* The interface itself is abstract
*
* Interface and inheritance exist simultaneously
* The interface itself can inherit multiple
* Interfaces can be implemented more
* Abstract class can also be Impl interface
*
*
* Main function in parent class
* The addition of additional functions in the interface is the provision of the class's method behavior.
* Interface cannot be instantiated it is abstract by default
* There can be an inherited relationship between an interface and an interface
*
* The same method appears in the interface of a class, the subclass implementation is not confused, the method in the interface is abstract, can only be implemented by subclasses, the realization of the function is only one part of the subclass;
*
*
* The role of the interface: Java from the single inheritance of the indirect implementation of multiple inheritance, expanded the original function, we can think of the interface is the complement of the class.
* Beginning with JDK 1.7, the interface can have the implementation of the static or default method

* Multi-state upward transformation and downward transformation
*
* Upward transformation: Equivalent to Automatic type conversion, from low type to high type
* >> Converts a parent class type reference (the child class object to which this reference refers) to a subclass type
* Downward transformation: equivalent to coercion type conversion, from high type to low type
* >> Refer the parent class reference to the Child class object
* Object str = new String ();
* (String) str. ToCharArray ();
* Type Tree
*----> Dog, Catheight
* | |
* | |
*-Anmial |
* | |
* | |
* \---Object low
*
* One sentence to explain the following error:
* Object obj = new Object ();
* (String) obj;
* This is not polymorphic and there is no upward transformation
*
*
* Sysout (a instanceof B); A is a subclass of B or B object is true otherwise it is false (i.e. a type must be greater than or equal to B)
* The reference type of this keyword A and B should have an inheritance relationship (no problem in front and back position) if there is no relationship immediately error
*
* Class Son extends person{...}
* Person s = new Son ();
*
* How members are used in polymorphism: they call methods based on the reference type unless coercion of type conversions
* Member variables: Can not be accessed at compile time to look at the parent class, the runtime also look at the parent class
* Member method: Compile-time look at the parent class, run-time look at the sub-class
* Static method: Compile and run all look at the parent class--static method belongs to the class AH so it doesn't matter with the subclass (directly called with the class name)
*
* Summary: In polymorphic case only >> ordinary member method << most special compile time because Java is only quasi-dynamic language so at this time the compilation phase object is not loaded into memory, so
* Method can not be called to see if it has this method in the reference type
* Runtime: Object loaded into memory what we actually do is already the object (subclass type)
*
*
* (https://www.zhihu.com/question/19883873)
* Why is Java not made into dynamic language (quasi-dynamic language)?
* Static type facilitates static analysis of tools, facilitates performance optimization, facilitates code readability
* The more complex the project, the use of static language can take advantage of the compiler to find and avoid problems earlier.
* This is not to say that dynamic languages cannot be used for large projects, but after a certain size, the advantages of a dynamic language's lightness and flexibility are offset by the overhead of the refactoring.
* Another reason for this is performance. In the same vein, not a dynamic language can write efficient code, but you spend more time on optimization,
* and the level of team people are inevitably uneven, not as static language safe.

Inner class
When Class C needs to implement both the function of a and the function of B, you can build an internal class AA to a inheriting BB to B inheritance thereby indirectly implementing multiple inheritance
Local inner class
Definition: A class in a method of a class
Role: You can privatize functions in classes, organize code, and improve reusability
Meaning: For encapsulated methods, the equivalent of a part of his function is object-oriented
Know:
Example: For methods in a Class A () and B () and C () want to have a B () Mask C () method [i.e. B () cannot call C ()]
Allow only a () to run C (), at which point C () is encapsulated in a local inner class in a ()

Class Test{void A () {class c{void C () {}}}void B () {}}

  


TIP:
A: Members of external classes can be accessed directly
B: In a local location, you can create an inner class object that invokes an inner class method through an object to use the local inner class functionality.
Considerations for local internal classes accessing local variables
A: Local inner class access local variables must be final decorated (before 1.7)
B: Why?
Because local variables are called as the method is called, they disappear as the call finishes.
The contents of the heap memory do not disappear immediately. So, we add final retouching
When the final modifier is added, this variable becomes a constant. Since it is a constant. You are gone.
I'm storing data 20 in memory, so I still have the data in use.

Javase Second Study essay (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.