Java Utility Knowledge Record--up to Java8

Source: Internet
Author: User
Tags finally block instance method throw exception

Record Java practical knowledge points, cut-off (including) to Java8, only as a description of the outline, not related to specific details.

Variable:
The wrapper class for int and long supports unsigned bit operations, meaning that their in-memory bits can be used to represent positive numbers all.
"_" can be used to separate numeric values (shaping, floating-point numbers), and can be used in a variety of numerical systems (binary, 16-binary, etc.). Example: 3.14_15f,0xcafe_babe.
If a constant (static final) is a base type or string literal, the compiler will replace the constant with the corresponding value and change the constant, and all classes that use this constant will need to be recompiled.
You can use static initialization blocks when initializing static variables, or you can refer to static methods directly in an assignment expression. Example: static int i = Staticmethod (); Instance variables do the same thing, but be aware that the instance method covers the quilt class (you can use the final method).

Inherited:
Method overloads are static multiple allocations (consider both static and method parameters), which are determined at compile time, and method overrides are dynamic single dispatches (only the actual call object is considered) and are determined at run time.
In the source code, the method signature is determined only by the name and parameters, and the method signature in the bytecode also includes the return type, exception, and so on.
The criteria for a method override include: Subclasses have access to the parent class's methods, the method signature is the same, permission, return type, throw exception are compatible.
Static methods in an instance can be inherited, but cannot be overwritten, and are statically dispatched. Static methods in the interface cannot be inherited.
When multiple inheritance occurs, the class method takes precedence over the interface default method, and two interfaces inherit methods from the same interface, which takes precedence if an interface overrides this method.

Generic type:
Generics allow a type to be used as a parameter. Java generics are type-checked only for generics at compile time, by erasing implementations.
A parameterized type is a generic type with a type parameter, which refers to generics without generic parameters (non-generic types are not primitive types).
For compatibility, parameterized and native types can be converted to each other, thus undermining the stability of generics. Example:list<string> sLi = new arraylist<> (); List Rawli = Sli;rawli.add (1);
Different parameterized types of the same generic cannot be converted to each other, but using wildcards and boundary qualifiers can change this behavior.
In a container, a lower bound wildcard qualifier can add an upper bound subset element, except that null cannot add other elements after the Nether, unbounded wildcards are qualified.
You can use helper methods to capture the wildcard character's type information, thereby relieving the restriction of using wildcards. If you can pass a list<?> variable to a method that accepts list<t> as a parameter, the type of the wildcard character is captured.

Inner class:
The member inner class is created from an external class instance, Outer.Inner Inner = Out.new Inner (); An instance variable with the same name as the external class can be accessed through the Outer.this (external class class name. This) in the inner class.
External class instance variables are not subject to this restriction when using local variables (or method parameters) of an external class in an inner class (partial, anonymous class), which must be final or equivalent to final (because the local variable is different from the object life cycle).
Non-static inner classes cannot have static members, except for static blocks of code (constants).
An interface is inherently static and cannot be defined in a local block.

Lambda:
Lambda expressions use functional interfaces to simplify the writing of code, which is more concise than the inner class.
When a method overload is encountered, the lambda expression uses parameters, method return types, and so on to infer the target type, which determines which method to invoke.
Lambda expressions use lexical scopes, and lambda expressions themselves do not introduce additional scope hierarchies or inherit variables from interfaces (target types). When you use an external local variable in a lambda expression, the variable must also be final or equivalent to final.
The lexical scope range is determined by the position in the code, where the dynamic scope is determined by the runtime environment, and most programming languages use lexical scopes.
A functional interface (functional interface) refers to an interface that contains only one abstract method, but there can be more than one default or static method in the interface.
JAVA8 pre-defined a number of functional interfaces, such as predicate<t> used to determine a condition, consumer<t> to perform a behavior, function<t,r> Accepts an operation of type T and returns the value of type R (T maps to R).
Stream is used for streaming operations on arrays, containers, and so on, and the method in stream accepts a functional interface type as a parameter and returns a stream, so it can be called in a chain.

Annotations:
Annotations are a specific interface that is used to describe a program. Annotations themselves are only metadata, but they can be processed by a program for the annotated program.
Type annotation can be used wherever a type is used, including variable declarations, construction instances (new expressions), transformations, inheritance, exception throws, and so on.
Type annotations can be used to enhance Java type checking to avoid potential errors in the program, such as @nonnull can be used to avoid nullpointerexception.
Repeatable annotations can be used multiple times on a single target. Because of compatibility issues, repeatable annotations also need to define the appropriate container annotations to use (not knowing why generics are not available in the annotations, otherwise the container annotations do not need to be defined repeatedly).
The target (@Target) of the container annotation must be a subset of the target of the repeatable annotation, or it cannot be compiled.

Abnormal:
When the method executes an error, it throws a corresponding exception to the method call stack frame, and if the exception is not handled, it is passed along the call stack until the thread is terminated. Exceptions cannot be captured across threads.
The exception to be examined is the exception that the program can expect and should be handled, the error refers to an unexpected exception outside the program, and a run-time exception that refers to an exception caused by a code problem within the program and should be avoided. Errors and run-time exceptions are non-inspected exceptions.
Exceptions are always caught by the first matching catch block, and multiple exceptions can be caught in a catch block, such as catch (e1| E2 e), when capturing multiple exceptions, the exception parameter is an implicit final type.
If there is a return value in a try, catch, the return value is stored first (where the return value is not the variable itself, but the specific value or object reference that the variable points to), the finally block is executed, and then the return value in the try, catch is overwritten if finally has a return. Finally throws an exception that suppresses (suppress) the exception thrown in the try, catch.
The Try-with-resources statement automatically shuts down (calls the Close method) to implement Java.lang.AutoCloseable (Java.io.Closeable as its sub-interface) of resources (Resource), with the closing order reversed from the declaration order, and the exception thrown in the try block Suppresses the exception that is thrown when a resource is closed in try-with-resources. Catch, finally blocks are executed after the resource is closed.
Calling the Getsuppressed method on the exception object (as defined in Throwable) can get the suppressed exception.
The exception declaration thrown in the method can be a collection of all the specific subclass exceptions that may be thrown when the other method is called with the exception type thrown.

Traditional I/O:
I/O (traditional I/O, non-NIO) needs to switch between the application (user state) and the operating system (kernel state).
Buffering improves performance by reducing the number of transitions between programs and external activities (disk, network, and so on).
The character stream is the encapsulation of the byte stream, which uses the byte stream for I/O operations, based on which the conversion between the characters and bytes is provided.

Java Utility Knowledge Record--up to Java8

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.