Fundamentals of Java Core technology

Source: Internet
Author: User
Tags garbage collection throwable

First of all, talk about my understanding of the Java platform, Java itself is an object-oriented language, notable features have two aspects, one is called "write once, run everywhere", it is very easy to get cross-platform capabilities, but also garbage collection (GC), Java through the garbage collector to reclaim the allocation of memory, In most cases, programmers don't have to worry about allocating and recycling their memory.

The phrase "Java is interpreted and executed" is not very accurate. The source code that we develop is first compiled into bytecode by Javac, and then the bytecode is converted into the final machine code by the interpreter embedded in the Java virtual machine at runtime. Long-seeing JVMs, such as the JVM provided by Oracle JDK in most cases, provide JIT (just-in-time) compilers, so-called dynamic compilers, which JIT can compile hot-spot code into machine code at runtime, where some hotspot code is compiled and executed. Instead of explaining the execution. So it is inaccurate to say that Java is a parsing operation.

For Java features are: (1) Basic language Features, object-oriented (encapsulation, inheritance, polymorphism), reflection, generics ... (2) Java Class Library: Core class library, such as Io/nio, network, Utils ... ; Security libraries, JDK, management, and massive third-party class libraries (3) Java Virtual machines: garbage collector, Runtime, dynamic compilation, accessibility, such as JFR (4) Tools: Accessibility tools such as jars, jdeps, compilers, Javac, Sjavac.

The above is the Java language and the understanding of the platform, the following one by one to conquer a few Java basic understanding, from the point and face, and gradually understand some of the Java language features:

First, the world will never be error-prone procedures, the abnormal situation intertwined with us these programmers, only a better understanding of anomalies, handling exceptions, our program is reliable; the Java language designed a perfect exception handling mechanism to reduce the threshold of writing and maintaining, please compare Exception and Error, another difference between runtime exceptions and general exceptions?

Answer: Exception and Error inherit the Throwable class, in Java only instances of the Throwable type can be thrown (throw) or catch (catch), which is the basic constituent type of the exception handling mechanism. Exception is an unexpected situation that can be expected during normal operation of the program and should be captured and processed accordingly.

Error refers to under normal circumstances, is unlikely to occur, most of the error will lead to the program's abnormal, non-recoverable state, since it is not normal to capture, the common OutOfMemoryError class

Exception is also divided into can check the exception and not check the exception, can check the exception in the source code must be displayed for capture processing, which is part of the compiler check, do not check the exception is called runtime exception, class NullPointerException, The ArrayIndexOutOfBoundsException class, which determines the capture as needed.

Diagram Summary:

Second, to cut into the Java language elements, a classic Java basic problem, talk about final, finally, finalize what is the difference? The typical answer is that final can be used to modify classes, methods, variables, the final modified class represents the non-inheritable extension, the final variable is not modifiable, the final method is not overridable, and finally is a mechanism for Java to ensure that the key code must be executed. You can use try-catch-finally or try-finally to perform actions such as turning off JDBC connections, guaranteeing unlock locks, and so on. Finalize is a method of basic class Java.long.Object, designed to ensure that objects are reclaimed by a specific resource before being collected by garbage collection. Finalize has not been recommended for use.

We recommend using the final keyword to clearly represent the semantics and logical intent of our code, which has proven to be a good practice in many scenarios, such as:

You can declare a method or class as final to explicitly inform others that these behaviors are not allowed to be modified. Viewing Java's core class library also sees that a significant portion of the declaration is marked as final, guaranteeing the security of the platform.

Using final modified parameters and variables, you can clearly avoid unexpected assignments that result in errors.

The final variable produces some degree of immutable effect that can be used to protect read-only data, especially in concurrent programming, to reduce the additional synchronization overhead.

For finally, it's OK to know how to use it, and the Java platform has gradually replaced the finalize implementation with Java.lang.ref.Cleaner.

Fundamentals of Java Core technology

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.