Java Basic Knowledge Learning notes Summary __java

Source: Internet
Author: User
Tags exception handling garbage collection modifier throw exception

Java Learning notes Summary

Java Basics Review

1, the abstract class can have constructors, you can have a Non-abstract parent class

2, the garbage collection mechanism is the heap inside of the memory, the stack of data automatically into the stack automatically stack

3, reference type of data in the heap, in memory only the stack of operations, the new keyword in the heap to allocate a piece of memory

4, Java is the value of delivery

5, string is immutable character, that is, once the memory is allocated, the value of this memory will not change, and the string assignment is, will redistribute a piece of memory, string pool: StringBuffer

6, in the heap of things called objects

Ø Run-time Exceptions:

n ArithmeticException This exception is thrown when an exception operation condition occurs. For example, an integer "divided by 0" throws an instance of this class.

n ArrayIndexOutOfBoundsException the exception that is thrown when an array is accessed with an illegal index. If the index is negative or greater than or equal to the size of the array, the index is an illegal index.

n Arraystoreexception an exception that is thrown when an object of the wrong type is attempted to be stored in an array of objects. Such as:

U objectx[] = new STRING[3];

U x[0]= New Integer (0);

n ClassCastException the exception is thrown when the view converts the object to a subclass that is not an instance. Such as:

U objectx = new Integer (0);

U System.out.println ((String) x);

N IllegalArgumentException indicates that an illegal or incorrect argument is passed to the method

n Negativearraysizeexception If the application view creates an array with a negative size, the exception is thrown

Ø compile-time Exceptions:

When n Nosuchmethodexception cannot find a particular method, the exception is thrown

7. Unicode code range

Chinese characters: [\U4E00,\U9FA5]

Number: [0x30,0x39]

lowercase letters: [0x61,0x7a]

Capital letter: [0X41,0X5A]

8. Final class

L Final class can modify non abstract class, Non-abstract class member methods and variables (static, instance, local variables)

L Final class cannot be inherited, no subclasses

L The final method can be inherited, but cannot be overridden by the method of the Quilt class

L Final member variables represent constants that must be assigned when declaring a member variable (either by assigning a value at the time of declaration or by assigning values in the constructor), and cannot be changed after assignment

U publicfinalint E; Final whitespace, the initial value must be assigned when the object is initialized
public Test3 (int x) {
E= x;
}

L final cannot be used for modified construction method 2.5

9. Static can only modify the member variable, which means that all objects share this data

L Static can modify member variables and methods, internal classes, can also form static code blocks, cannot modify local variables

L A member variable modified by static can be invoked directly with the class name, which is the following syntax:

U class name. static amplification name ();

U class name. static variable name;

I cannot use the This and super keywords in static methods, and cannot directly access member variables and member methods without static

L because the static method is independent of any instance, the static method must be implemented, not abstract

L Run the static code block first, then run the constructor

10. What does the static and final piece represent?

L staticfinal is used to decorate member variables and member methods, which can be understood as global variables (the concept of no global variables in Java)

L staticfinal modifier variable, indicating that once the value is not modified, and through the class name can be accessed

L Staticfinal Cosmetic method that is not overwritten and can be accessed directly through the class name

L staticfinal Modify some container type (ArrayList, HASHMAP) variables, can not change the container variable native, but you can change the objects stored in the container

11, class member variable classification

A variable modified by static is called a static variable or a class variable;

Variables not modified by static are called instance variables.

12, command line parameters

Java main class parameters (i.e. command-line arguments)

Java input command-line arguments in eclipse, in the selected class, right-click Run As-runconfigurations-arguments-program arguments-variables-Select String_prompt

13. Integer.valueof (String) converts a string into an Integer wrapper class

14, A. java file can have many classes. However, note the following points:

A, the public permission's class can only have one (also may not have one, but only has 1 at most)

b, the file name of this. java file must be the class name of the public class (in general, where the main method is placed is the entry of the program). )

C, if there is no public class in this file, then the filename is a class name.

D, when compiling this. java file with Javac, it generates a. class file for each class

15. Object oriented, process oriented

Three main characteristics of object-oriented: encapsulation, inheritance, polymorphism

Process oriented: the characteristics of the underlying language of C language

16, classes and objects:

A class can have more than one object, and a class is a description of the object

a member of a class:

Fields: Encapsulating private

Method: The behavior of the class

Constructor: Has the same name as the class and does not have a return value. Function: Initializes a member variable. Default constructor: Parameterless and has no return value with the same name, when a class shows the structure

Builder, the class does not set its own default constructor

Inner class

17, the relationship between classes: The highest degree of inheritance coupling

A Inheritance (IS-A): Single inheritance Multiple implementation extends benefits: Reduce code redundancy, disadvantage: too high coupling

B Realize:

C Combination (HAS-A):

Synthesis: The relationship between the individual and the whole, the individual and the whole life cycle, the relationship between the company and the department

Aggregation: The relationship between the individual and the whole, the life cycle is different, the student and the school

Association: Bidirectional Association

Class person{

Petpet;}

classpet{

Personowner;

}

D Dependency (USE-A): A class is either a parameter of another class method or a return value, called a dependency

18. Java Design principles

Low coupling high cohesion

Scalability and maintainability

19, Inheritance:

members in subclasses:

1 A visual (not private) member (field, method) that inherits from the parent class and cannot inherit the constructor of the parent class

2 Members (fields, methods, constructors) that are unique to the subclass

3 the Member (method) polymorphism of the overloaded parent class

conditions for overloading:

A) occurs between the same class or a parent-child class

b The method name is the same, and the argument list (number of parameters, data type of parameter, order of parameters) is different

c) Return value does not matter

d) Access rights don't matter

ambiguous Overload:

MA (12,34)//ambiguous overload, compilation error

Publicvoid MA (int i,double j) {//print 1}

Publicvoid Ma (double i,int j) {//Print 2}

Construct to load:

This :

this. Members (fields, methods): Calling fields, methods of this class

This (argument list): Calls to other construction methods of this class must be in the first sentence of the construction method

Public person (int age) {

This is (age, "");//Call the following builder

}

publicperson (int age,string name) {

This.age= age;//Call fields of this class

This.name= name;

}

Super: Object of the parent class

Super. Member: Invoke the member of the parent class (method, field)

Super (argument list): Invokes the constructor method of the parent class, must be in the first sentence of the subclass construction method

If there are no parameterless constructors in the parent class, you must explicitly call the parent class with the constructor of the parameter (super (parameter class table or this (parameter list)) in the subclass. If the parent class has a constructor with no parameters, the constructor of the parent class is not explicitly invoked in a subclass because the system defaults to super ();

4 Rewrite the member (method) polymorphism of the parent class

overridden conditions:

A) occurs between the parent-child class

b The method name is the same, and the parameter list (number of parameters, data type of parameter, order of parameters) is the same

c) The return value must be the same

d) The access rights of the subclass override method are greater than or equal to the access rights of the overridden method of the parent class

E) Subclasses cannot throw exceptions larger than the parent class exception (RuntimeException exception)

5Hide the members of the parent class (field)

Polymorphic object: Invoke hidden member, calling the parent class's

Call overrides the member, calling the subclass's

20. Static: Class Members

Fields: All objects Shared

method: static method can not directly access non-static members, if need to access, generate instance access;

Non-static methods can directly access static and Non-static members

Cannot have this, super keyword in static methods

BLOCK: executes only once in memory and is typically used to initialize static members

Inner class

21, Final: final State of

Variable: Cannot assign a value for the second time and must have an initial value

Method: cannot be overridden

Class: Cannot be inherited

22. Abstract:

Abstract methods must be in abstract classes, abstract classes do not necessarily have abstract methods

Abstract class has no instance

An abstract class can have a parent class and a parent class can be an abstract class or a Non-abstract class

Abstract classes have constructors that cannot be used to produce instances of this class, which are used to indirectly produce instances of subclasses.

Abstract cannot be used with static, final, private

23. Exception Handling:

1) Try{}catch () finally{}

2) throws exception class after throws method declaration

3 An instance of throw exception class after throw sentence (usually if)

4 Custom Exception class extends exception or runtimeexception

24. The difference between method override (override) and overload (Overlord)

overriding override: The same method as the signature in the parent class appears in the subclass, the access modifier is equal to the parent class, the return value type, and the argument list is the same

Overload Overlord: A method within a class that has two and more than two method names, different argument lists (parameter types, number of parameters, order of parameter types)

25, dynamic polymorphism and static polymorphism

Dynamic polymorphism (Run-time polymorphism): It is known in the runtime that the method is invoked, which means the method overrides

Static polymorphism (compile-time polymorphism): The method is known at compile time, which refers to the method overload

26, if there is no null parameter construction in the parent class

Then the subclass must be displayed in the same way as super () to call the parent class construct

Recommendation: If a parameter construct is present in the parent class, you must manually add an empty parameter construct

Example: The following code runs as result (B)

classa{

public A (int A) {System.out.println ("a");}

}

Class B extends a{

PUBLICB () {

Super (5);//must add this piece, using the method shown in super () to call the parent class

}

Public Voidma () {System.out.println ("Ma");}

}

public class test{

public static void Main (String[]args) {

b b=new B ();

B.ma ();

}

}

A. Print Output MA

B. Compilation errors

C. Print output A

D. Print output a MA

27. Static:

Represents a static, decorated content that belongs to a class member, is shared by all objects of the class, and is recommended for use by the class name. The modified content is executed when the class loads, only the content that is modified by the static, and the decorated content is executed once when the class is loaded.

What can be modified: cosmetic attributes, cosmetic methods, as code blocks using JDBC to load drivers

static{

Class.forName ("");//used as a code block

}

Characteristics:

1. The modified content belongs to the class member

2. The modified method can not be rewritten and may be overloaded

3. The modified content, executes once

Example: public class helloworld{

Public staticvoid Main (String args[]) {

Method ();

}

Public Voidmethod () {

try{

System.out.println ("Hello i Execute");

}

finally{

System.out.println ("I will not execute");

}

}

}

What is the output after the compilation is run. D

A. " Hello i Execute "

B. " I'm not going to do it.

C. " Hello i Execute "

"I'm not going to do it."

D. Code cannot compile

Because the main method is a static method decorated with static, the invoked method must also be decorated with static, and method ();

28, Hashcode () returns the object's memory address of the corresponding int type data, but we can rewrite the Hashcode () method, return is no longer the memory address.

Hashcode () The same object, the two objects are not necessarily the same, and if two objects are the same, hashcode () must be the same.

29. Access Modifiers in Java

Public: All

Protected: yourself, the same package, subclasses can access

Default: Defaults are not added, you can access them in the same package, but they are less restrictive than the protected level.

Friendly: Self, in the same package can be accessed

Private: Self

Example : The following code runs as result: (C)

1. Abstract class Abstrctit {

2. Abstractfloat getfloat ();

3.}

4. Public class Abstracttest extends Abstractit {

5. Privatefloat f1 = 1.0f;

6. Privatefloat GetFloat () {return f1;}

7.}

A. Successful compilation

B. Line 6th generates an error, causing the run to fail

C. Line 6th generates an error, causing the compilation to fail

D. Line 5th generates an error, causing the compilation to fail

The default access modifier is default,default less than private (that is, more advanced), more restrictive than protected, and all sixth-line override methods use the private access modifier character the parent class, all of which fail to compile

30, Math.random () Gets the value of [0,1], which takes less than 1;

Usage: (int) math.random () *n+1; an integer from 1 to n

31 . Conversions between arrays, collections, and strings

A. Arrays.aslist ()//converting arrays to collections

Example: List List = arrays.aslist (names);//Convert an array to a collection.

B.string.valueof ()///Converts an array to a string, or converts an integer to a string

Example:char []ch1 = {' A ', ' C ', ' A ', ' B ', ' A '};

String str2 = string.valueof (CH1);

Example: int num = 123456;

String str =string.valueof (num);

C.tochararray () Converts a string to an array

Example: String str = "GDFGDFGD";

Char [] ch = str.tochararray ();

d.arrays.tostring () converts a character array into a string-specific format output, such as [1,2,3]

Example: String str1 = arrays.tostring (CH);

System. out. println (STR1);

e.str.re

Related Article

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.