Basic Java Knowledge

Source: Internet
Author: User
Tags array length finally block reflection shallow copy thread class

In the 1.public static void Main () method, static and public can be swapped for positions, can be final decorated, or can be synchronize decorated.

When the final-modified variable is not static, you can assign a value at the time of declaration, or you can assign a value in the constructor:

2. A static block of code in a class executes before the main method, regardless of whether the static code block precedes or follows main. Precisely, this is because a static block of code executes when the class is loaded and executes only once. The main method is the class-loaded portal, which loads the class (new) when it encounters main, and then loads the static code block (either before or after main) before executing the contents of the main method. A non-static block of code is executed every time the new object. The static code block is loaded only once , and if main is already executed, then new is no longer executed.

3. Initialization order: The parent class takes precedence over the subclass, initializes the parent class and initializes the subclass, but note that the static code block is special, because the static code block is in the class load phase and takes precedence over other parts, so the parent class and subclass static code blocks are loaded, and the other parts of the parent class and subclass are loaded first.

(1) Parent class static code block (including static initialization block, static property, but not static method) (2) subclass static code block (including static initialization block, static property, but not static method) (3) parent class non-static blocks of code (including non-static initialization blocks, non-static properties) (4) Parent class constructor(5) Subclass non-static code block (including non-static initialization block, non-static attribute) (6) Subclass Constructor 4. Scope

5.final keywords

Final Modified class: The class cannot be inherited, and the property is final by default. such as String

Final Modification Method: methods cannot be overwritten.

Final modified variable: The variable cannot be modified.

The final decorated reference type is an array, and the reference (address) cannot be modified, but the referenced content can be modified, and the values in the arrays can be changed.

6.String class

The string class is final, indicating that it cannot be inherited or exposed. The class uses the final char[] array to store the string. The array is also final decorated, private decorated, so the reference (address immutable), but the contents of the array can be changed, the designer each time the array is manipulated, the original array will not be manipulated, but create a new array equal to the original array, to operate, and return the new array.

Because the bottom is an array, for a string s= "s", the array length is fixed, s+ "a", this time will be a new array operation, return a new string, not in the original array operation, so there are two objects, "s" is unchanged so the string is immutable

7. Three major features

2) Inheritance: Inheritance is the derivation of new classes from existing classes, which can absorb data properties and behaviors of existing classes, and can extend new capabilities. Inheritance avoids repeating descriptions of common features between general classes and special classes.

3) Encapsulation: Encapsulation is the same kind of things in common (including properties and methods) into the same class, easy to use. Classes can protect their own methods and properties, allowing only trusted class operations to hide untrusted information

4) Polymorphism: polymorphism refers to the same operation in different objects, will produce different semantics, produce different results. Polymorphism is implemented by overloading (compile-time polymorphism) and overwriting (run-time polymorphism). The parent class reference can only invoke methods and properties that exist in the parent class, and cannot invoke the extended part of the subclass, because the parent class reference refers to the parent class that inherits from the object of the heap class;

Overloads are implemented by different parameters, not by returning parameters, method permissions, and throwing exceptions.

8.float F = 3.4 Compilation does not pass, should f=3.4f.9.& is bitwise AND Operation,&& is logical with Ten , Heap and the Stack What's the difference?

A: The stack is a linear collection, its addition and deletion of elements should be done in the same paragraph, the stack is processed in a last-in, first-out way; The heap is a constituent element of the stack.

11. Abstract classes and interfaces

First, none of them can be instantiated.

Here's a comparison of the syntax differences between the two:
1. Abstract classes can have construction methods, and interfaces cannot have constructors.
2. Abstract classes can have ordinary member variables or static member variables, but there are no ordinary member variables in the interface, all public static final static member variables.
3. Abstract classes can contain non-abstract ordinary methods, all the methods in the interface must be abstract, and cannot have non-abstract ordinary methods.
4. The access type of an abstract method in an abstract class can be public,protected and (the default type, although
Eclipse does not give an error, but it should not), but the abstract method in the interface can only be of the public type, and the default is the public abstract type.
5. An abstract class can contain static methods (non-abstract methods can be static), and interfaces cannot contain static methods
7. A class can implement multiple interfaces, but can inherit only one abstract class. Java is there a memory leak in the, please describe it briefly. "Foundation"

A: Yes, there are useless but accessible objects that cannot be recycled by GC, resulting in memory resources being consumed.

Unable to allocate memory (insufficient memory), memory overflow

Error and the Exception What's the difference? "Foundation" all Inherit Throwable

A: Error indicates system-level errors and exceptions that the program does not have to handle, a serious problem in situations where recovery is not impossible but difficult, such as memory overflow, which cannot be expected to be handled by the program, and exception represents an exception that needs to be captured or handled by the program. is a design or implementation problem; That is, it indicates that it will never happen if the program is running normally.

Run-time exceptions represent exceptions that make the program unrecoverable, the compiler does not check for these exceptions, it does not need to be snapped, and can optionally be thrown without a declaration;

A checked exception indicates an exception that the program can handle, needs to be captured, or thrown and declared.

Common run-time exceptions:

Null pointer exception class: NullPointerException
Type cast Exception: ClassCastException
Array negative subscript exception: Negativearrayexception
Array subscript out-of-bounds exception: ArrayIndexOutOfBoundsException
Violation of security principle exception: Secturityexception
File ended exception: Eofexception
File not found exception: FileNotFoundException
string conversion to numeric exception: NumberFormatException
Manipulating Database Exceptions: SQLException
Input/Output exception: IOException
Method not found exception: Nosuchmethodexception

Try-finally

In try-finally, when a variable is returned in a try, such as return a. But finally the A is modified (there is no return a in finally), and the try still returns a that is not modified. Because the return content is saved in the temporary stack before the return, then the finally content is executed, and then the finally content is finished and then return. However, when the return statement is in finally, it overwrites the original return

1, regardless of the occurrence of wood anomalies, finally block code will be executed;
2, when there is return in the try and catch, finally will still execute;
3, finally is executed after the return of the expression after the operation (at this time does not return the value of the operation, but first to save the value to return, the pipe finally in the code, the return value will not change, still is the value of the previous saved), So the function return value is determined before the finally execution, but finally is executed before the return;
4, finally, it is best not to include return, or the program will exit early, the return value is not a try or catch in the saved return value. Finally there is a return, which overwrites the previous return5, and the Catch has System.exit (0). Indicates that the entire virtual machine is released, the JVM stops working, and the program exits normally, and no longer executes finally ThreadLocal

Threadlocal a copy is created for a variable in each thread, each thread can access its own internal copy variable.

The value stored by the threadlocal is thread-closed, mutually exclusive between threads, used primarily to share some data within the thread, to avoid passing parameters

threads, each thread maintains an implicit reference to a copy of its thread's local variables, as long as the thread is active and the ThreadLocal instance is accessible; After the thread disappears, all copies of its threaded local instance are garbage collected

There is a map in the thread class that stores a copy of the variables for each thread.

For the problem of multi-thread resource sharing, the synchronization mechanism adopts the way of "changing space by Time", and Threadlocal adopts the way of "space Change Time".

Arrays.sort and Collections.sort using the sort

Arrays.sort Summary: lenthth<47 insert Sort

47<length<286 Two-axis quick sort (two hubs, dividing the array into three parts, with three parts recursively).

length>286 and continuity (ascending or descending) not good for two-axis quick sequencing

length>286 and continuous good merge sort

Collections.sort called the Arrays.sort.

Non-abstract class implementation interface two of the same two small principle: the same method name, the same parameter type

The subclass return type is less than or equal to the parent class method return type.
Subclass throws exception less than Equals parent class method throws an exception,
The subclass access permission is greater than or equal to the parent class method access.

Clone function

When you clone an object from an object, modifications to the cloned object do not affect the original object.

To use Clone (), follow these steps:

1. Implement the Cloneable interface; This is an identity interface, there is no method

2. Rewrite the Clone () method in object;

3. Call Super.clone to complete the shallow copy. Self-operation, complete deep copy

Shallow copy: Only basic member variables are considered, and other objects referenced in the object (including arrays) are not copied.

Deep copy: Re-copy the other objects referenced.

Reflection

The main function is to create the class object dynamically at run time.

Create class: Class.forName () class name. class instance. GetClass ()

How objects are created

New Reflection Clone deserialization

Basic Java Knowledge

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.