Java Basics Summary 2

Source: Internet
Author: User
Tags modifiers

Xi. Packing Class (Integer)

A) Common methods:
I. Integer.max_value ();//Gets the maximum value of int
II. Integer.min_value ();//Gets the minimum value of int
Iii. tobinarystring ()//Turn binary
Iv. tohexstrng ()//Turn hex

12. Abstract class

A) Characteristics of abstract classes:
I. Abstract methods must be in the abstract class;
II. Abstract methods and abstract classes must be modified by the abstract keyword;
Iii. Abstract classes can not create objects with new, because invoking abstract methods is meaningless;
Iv. the methods in the abstract class must be used to make the subclass object call after all the abstract methods have been replicated by the subclass;
V. If the subclass covers only a subset of the abstract methods, then the subclass is an abstract class
b) Format: "Access control character" class name {
Properties
Access control return type method name (formal parameter list) {
method body; "Return value"
}
Access controller abstract return type method name (formal parameter list);
}
Example: Package B;
public class abstractdemo{
public static void Main (string[] args) {Chinese c = new Chinese ();}
}
Abstract class chinese{
public abstract void Show ();
}
Abstract class d{
public void Show () {System.out.println ("213");}
}

13, Interface (interface)--– cannot instance object, that is, cannot new interface

A) Definition: When a method in an abstract class is abstract, the class can be represented in the form of an interface
b) Format:
1. Common definitions in interfaces: constants, abstract methods
2. The members in the interface have fixed modifiers
Constants: public static final
Method: Public Abstract
The members in the interface are public.
c) interface can not be set up objects, need to implement the quilt class, sub-class docking in the mouth of the abstract method is overwritten, can be instantiated, otherwise the subclass is an abstract class
Interfaces support multiple implementations of implements, but interfaces cannot implement interfaces and can inherit (multiple-inheritance) interfaces
D) Example
public class interfacedemo{
public static void Main (string[] args) {Subinter si = new Subinter (); System.out.println (SI. NUM); System.out.println (Subinter.num); System.out.println (inter.num);}
}
Interface inter{
public static final int NUM = 3;public abstract void show ();
}
Class Subinter implements INTER{//implementation interface
public void Show () {}
}

14. Single Case design mode

A) Definition: is a fixed solution to the same problem, a single example is to create only one object
b) Want to ensure that the object is unique: 1, in order to avoid other programs to set up such objects, first control other programs to establish such objects, 2, in order to allow other programs to access, in this class to customize the object; 3, for easy access, provide access
c) Usage scenario: When only one object needs to be created
d) define the steps:
1. How to construct private
2. Instance object of private static
3. public static external access method
I. Code implementation:
1. First type: A hungry man-first initialization
private int A;
Private A () {};
private static A = new A ();
public static Get () {
Return A;
}
2. The second type: lazy--call-time initialization-delay loading
private int A;
Private A () {};
private static A = new A ();
public static Get () {
if (a==null) {a = = new A ();
}
return A;
}
E) Benefits:
I. Controlling the use of resources
II. Number of control instances generated
Iii. enabling communication between multiple unrelated two threads or processes

XV, Package

A) Definition: it is to wrap the fields and methods, and modify them with certain modifiers to achieve the external information hiding, and expose the methods that can be exposed.
b) java.lang.*;--– base package, default Import
c) Requirements: 1, must be placed in the first non-annotated line; 2, the package name is usually the inversion of the domain name, the package name must be lowercase, does not contain keywords, can not start with Java, javax
d) Role: 1, easy to manage; package needs; Prevent class name collisions
e) Import keyword for the package;
f) commonly used packages have; Java.lang.annotation\java.net\java.nio\java.sql\java.util\javax.sql\
Javax.xml\javax.swing

16, inheritance (extends)-Class and class is a relationship-relational relationship

A) Implementation mode: Inheritance (inheritance) and combination (composition)
I. Inheritance is implemented in the following ways: implementation inheritance, visual inheritance ———-class is an inheritance (generalization) relationship between classes
1. Implementation of inheritance refers to the ability to use the properties and methods of the base class without additional coding;
2. Visual inheritance refers to the ability of the child window (class) To use the base window (class) to look and implement code;
Ii. composition (Aggregation relationship): interface inheritance and pure virtual class inheritance
1. Interface inheritance refers to the ability to use only the names of properties and methods, but the subclasses must provide the implementation.
b) Initialization order: The Father class static initialization block-"subclass static initialization Block-" The parent class object initialization block-"Parent class construction Method-" Subclass of the object initialization block-"subclass of the construction method, such as:
Package A;
public class B extends a{
Static{system.out.println ("————-4 ————");} {System.out.println ("————-5 ————");} Public B () {System.out.println ("————-6 ————");} public static void Main (string[] args) {b b = new B (); new B ();}
}
Class a{
Static{system.out.println ("————-1 ————");} {System.out.println ("————-2 ————");} Public A () {System.out.println ("————-3 ————");}
}

17, polymorphic (upward transformation)

A) The disadvantages of polymorphism
I. A new approach to subclasses is lost after an upward transformation
II. The upward transformation is safe, but the downward transformation is not safe
III. Example:
Package A;
Transition up ———— print type look right
Cons: Lost the Child class new methods, properties
public class A extends super{
public void F (Super s) {System.out.println (s);} public static void Main (string[] args) {//super a = new A ();//[email protected]-– prints out a-type variable//system.out.println (a);a = new A (); A.f (a);//[email protected] ——-upward transformationSuper[] s = new super[] (new A (), New Super ());//system.out.println (a.length); Super[] s = new super[]{new A (), New Super ()}; System.out.println (s.length); Object o = new int[3];int[] B = (int[]) o; System.out.println (b.length);//Compile error, loss of child class new methods, properties}
}
Class super{}
b) Late binding
I. Associating a method call with the same method body is called binding. If the program is bound before execution, called pre-binding, Java in addition to the static and final method, all other methods are late
Ii. overload-– Pre-binding--– static binding--compile phase
override-– late binding--– dynamic binding--run phase
Example: Super a = new subclass ();//Upward Transformation ——-overload look to the left, override to the right
c) Ways to achieve polymorphism
I. Overriding override: means that subclasses redefine the method of the parent class
II. Overloading (overload): means that multiple functions with the same name are allowed, and the parameter tables of these functions are different (perhaps the number of arguments is different, perhaps the parameter types are different, the order may be different)
Iii. covariant rewriting: Subclass methods can be more specific than the return type of the parent class (the return type of the subclass is a subclass of the parent method return type)
d) Only common methods are polymorphic, final, static, private methods are not polymorphic
Field (Domain member = = field) is not polymorphic--they are all pre-binding--the declaration type binding of the same variable
c) Design principles:
1. Opening and closing principle-open to expansion, closed for modification, the key to realize is abstraction
2. The principle of substitution on the Richter scale-for the base class principle-where any base class can appear, subclasses must be able to appear

18. String (Declaration, create, initialize, manipulate (compare equals/==, convert, find, intercept, split, split, replace, connect))

A) The Unicode code is used in Java, the memory space of the string is related to its properties, and the string creates the data flow in the JVM when it is initialized.
b) Declaration Creation initialization
I. Format: String variable name = initial value;
II. String variable name = new string (initial value);
c) Operation:
I. Comparison (equals and = =)
1. Equals is content, = = is the address, equalsignorecase ()-Ignores case
2. Example 1:string d = "123";
string D1 = new String ("123");
System.out.println (D.equals (D1)); True
System.out.println (D==D1);//false
Ii. charAt (int index)--Returns the char value at the specified index
Iii. Length ()--– returns the lengths of the strings
Iv. Conversion
1. ToString ()--returns itself
2. toLowerCase ()-converts all strings to lowercase
3. toUpperCase ()-converts all strings to uppercase
V. Find
1. IndexOf (String key)--the index of the first key returned from the previous lookup
2. IndexOf (String key,int Formindex)-Returns the index of the first key found starting at the specified position
3. LastIndexOf () ——— search from the back to return the index of the last key
Vi. interception
1. substring (int bindex,int eindex)--intercepts the source string (Begin,end) and returns a new string
Vii. splitting
1. Split (string regex)-splits the source string into several new strings based on a regex
Viii. replacement
1. Replace (char Oldchar,char Newchar);
IX. Connection
1. + or concat connection-a new string is concatenated at the end of the source string, returning a new string
2. "A" + "B";
X. Creating an Object
1. String str1 = "abc";
System.out.println (str1 = = "abc");
Steps:
1) Open a space in the stack to store reference str1,
2) Creates a space in the string pool, storing the string constant "abc",
3) Reference str1 points to the string constant "ABC" in the Pool,
4) The address of the str1 refers to the address where the constant "ABC" is located, and the output is true
2. String str2 = new String ("abc");
System.out.println (str2 = = "abc");
Steps:
1) Open a space in the stack to store reference str2,
2) Create a space in the heap to store a new string object "ABC",
3) Reference str2 to the new string object "ABC" in the Heap,
4) The object address of the str2 refers to the address in the heap, while the constant "ABC" address is in the pool and the output is false
3. String str3 = new String ("abc");
System.out.println (STR3 = = str2);
Steps:
1) Open a space in the stack to store reference STR3,
2) A new space is opened in the heap to hold another (different from str2) new String object,
3) Reference STR3 point to the other newly created string object
4) STR3 and str2 point to different string objects in the heap, the address is not the same, the output is false
4. String STR4 = "a" + "B";
System.out.println (STR4 = = "AB");
Steps:
1) Open a space in the stack to store reference STR4,
2) According to the compiler to consolidate the known amount of optimization function, the pool to open a space to hold the merged string constant "AB",
3) Reference STR4 point to the constant "AB" in the pool,
4) STR4 refers to the constant "AB" in the pool, the output is true
5. Final String s = "a";
String STR5 = s + "B";
System.out.println (STR5 = = "AB");
Steps:
Same 4
6. String S1 = "a";
String s2 = "B";
String STR6 = s1 + s2;
System.out.println (STR6 = = "AB");
Steps:
1) stack in the middle of the storage reference s1,s1 point to the pool string constant "a",
2) stack in the middle of the storage reference S2,s2 point to the pool string constant "B",
3) in the stack to open a middle storage reference STR5,
4) S1 + S2 restores a new string object "AB" by the last step of the StringBuilder by the ToString () method, so that a space in the heap holds this object,
5) Reference STR6 to the new string object restored in the heap (S1 + s2),
6) STR6 points to the object in the heap, while the constant "AB" is in the pool and the output is false
7. String STR7 = "abc". SUBSTRING (0, 2);

Steps:
1) Open a space in the stack to store reference STR7,
2) The substring () method restores a new string object "AB" (as distinct from STR6), which creates a space in the heap to hold the object,
3) Reference STR7 points to the new string object in the heap,
8. String str8 = "abc". toUpperCase ();
Steps:
1) Open a space in the stack to store reference STR6,
2) The toUpperCase () method restores a new string object "ABC", which does not open a new space to hold the string constant "abc",
3) Reference str8 to a new string object in the heap

19, this

A) This is a pointer to the current object only;
b) Use:
I. If used in a construction method, represents the object that the current constructor method is initializing, such as:
1. Person p = new person ();
2. Public person (int id) {
This.id = id;//is equivalent to p.id = ID;
3.}
II. If used in a method, represents the object that is currently calling the method, such as:
1. Public show (int id) {
This.id = id;//is equivalent to p.id = ID;
2.}
———— p.show ();

20. static--Data sharing

A) The static modifier can be used to decorate the class's member variables and methods-only once when the class is loaded
I. When modifying a member variable, the variable becomes a class variable or a static variable, which is subordinate to the class, and all objects of the class share the same static variable (life cycle is long, the class is loaded and stored in the method area)
Ii. when modifying a method, the method becomes a class method or a static method that can be called directly with the class name. Method name () or by using the object reference name after the object is created. Method () Call
Iii. static methods cannot access non-static methods:
1. A static method is also called a class method, which is invoked directly using the class name. method to invoke; a non-static method must first create an object and invoke it on the object, in the format: Object name. Method Name ();
2. You may not have an instance object when the static method is called, and you cannot call a non-static method

21, Difference and contact:

A) break and continue and return
I. Continue and break can all end this cycle, but continue the next round after the end of the round, break jumps directly out of the loop
II. Break can be used in multiple loops to jump out of the loop that is closest to it, or it can be used to jump out of a loop in a multi-branch statement Swtich
III. Continue can only be used in loop statements to end this cycle and perform the next cycle;
Iv. continue and the statements after break are not executed
V. Return is the terminating method and returning the data, the statement after the return of the method body does not execute
b) class variable (static), instance variable, local variable
I. Definition: class variables and instance variables are defined in a method outside a class, local variables are defined within a method, in a parameter, in a code block
II. modifier: Class variables must be modified with static, instance variables without static decoration, class variables and instance variables can not be assigned value, local variables must be initialized first
Iii. use: Class variable invocation format is: class name. variable name; an instance variable is called when an instance object must first be created, called (object. Variable name) on the object; Local variables can be used directly
Iv. Load: Class variable (stack) loaded at class load, end of class, instance variable (in heap) loaded at object invocation, end at end of call, local variable (in stack) starts at method start call, end at end of method
c) This and super
I. Action Properties--–this. Property: Indicates that the property in this class is called, and if the attribute in this class does not exist, the lookup starts from the parent class; Super. Property: Represents a property in the calling parent class
Ii. method of Operation--–this. Method (): Indicates that the method in this class is called, and if the method in this class does not exist, it starts from the parent class; super. Method (): Represents a method in the calling parent class;
III. Call the constructor method--this (); Call the other constructor methods in this class, Super (): Represents the constructor method for calling the parent class
Iv. look up the scope ——— this first from the sub-class lookup, if not from the parent class lookup, super do not check the subclass of the parent class directly;
V. Special: This can represent the current object
d) string with StringBuffer, StringBuilder
I. String is an immutable object, each time the string type is changed to produce a new object, StringBuffer is a variable-length character sequence, and no new object is created after modification
Ii. The essence of StringBuffer is that it has the capacity of this property, the default capacity is 16, when the initialization length is greater than 16 o'clock, its capacity is twice times the initialization length.
Iii. string manipulation of characters is "+", concat; StringBuffer methods for manipulating characters are insert, append, delete (start int,endint)
Iv. Initialization format:
1. String st = "234"; string s = new string ();
2. StringBuffer sb = new StringBuffer ();
V. StringBuffer and StringBuilder are variable-length character sequences, which are secure in multi-threading, and the latter are unsafe
e) equals and = =
I. If it is the basic type comparison, then can only use = = to compare, cannot use equals;
II. For basic types of packaging, such as Boolean, Character, Byte, short, Integer, Long, Float, double and so on the reference variable, = = is the comparative address, and equals is the comparative content;
III. In a string or class that overrides the Equals method: Equals is more than content, = = is more than the address
Iv. when the Equals method is not overridden in another class, equals is used the same as = =, which is more than the address
f) Overloading and rewriting
I. Overloading and rewriting are polymorphic representations of classes, overloading is a polymorphic representation of a class, overriding is a polymorphic representation of a subclass inheriting a parent class, and overloading and rewriting are all methods;
Ii. overloading requires different parameter lists (type, number, order) with the same name, regardless of return type, overriding requires the same name as the parameter list (type, number, order), the same return type (co-reference data type, is its subclass or the same, the base data type is the same), the access modifier is greater than or equal to the parent class Throws an exception to be less than or equal to the parent class (it can be compiled if the thrown exception is runtimeexception)
Iii. overloads can be used in construction methods and common methods, and overrides can only be used in normal methods
g) integers and int
I. Type: int is the basic data type, and integer is the reference data type;
II. Integer is the packaging of int, which belongs to the packing class;
Iii. Default value: The default value of int is 0,integer default value is null.
h) class, abstract class, interface
1, abstract class needs to have the abstract modification, the class does not need
2, abstract class can have common methods and abstract methods, but ordinary class only have common methods
3, abstract classes and ordinary classes can inherit, but the subclass of the abstract class must override all the abstract methods of its parent class, while the ordinary class does not require
4, abstract class can not be instantiated, the ordinary class may be instantiated
5, interface is abstract class abstraction, interface can only have abstract methods and constants, the interface supports multiple inheritance, does not contain the construction method and initialization block
i) final, finally and finalize
I. Final is used to declare attribute methods and classes, respectively: properties are immutable, methods are not overridden, classes are not inheritable
II. Finally is part of the exception handling statement, which indicates that always executes
Iii. Finalize is a method of object that is called by the garbage collector when it executes.

22. Cannot coexist

A) super () and this (): all must be placed in the non-commented first line of the method
b) Final and Abstract: One final, one is abstract
c) Abstract and private

Java Basics Summary 2

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.