Home >Java >JavaBase >Is Java compiled or interpreted?

Is Java compiled or interpreted?

Guanhui
GuanhuiOriginal
2020-06-06 14:50:5410661browse

Is Java compiled or interpreted?

Is Java compiled or interpreted?

Java is neither a traditional compiled language nor an interpreted language. Java is first compiled into a ".class" bytecode file and then interpreted and executed by the JVM virtual machine. , so Java can be said to be either compiled or interpreted.

Java Features

1. Simplicity

Java looks very similar to C, but for To make the language small and familiar, the designers removed many of the features available in C that the average programmer rarely uses. For example, Java does not support the go to statement and instead provides break and continue statements as well as exception handling. Java also eliminates C's operator overload (overload) and multiple inheritance features, and does not use the main file, eliminating the need for preprocessors. Because Java has no structures, arrays and strings are objects, so no pointers are needed. Java can automatically handle object references and indirect references and realize automatic collection of useless units, so that users do not have to worry about storage management issues and can spend more time and energy on research and development.

2. Object-oriented

Java is an object-oriented language. For programmers, this means paying attention to the data in the application and the methods to manipulate the data, rather than thinking strictly in terms of procedures. In an object-oriented system, a class is a collection of data and methods for operating data. Data and methods together describe the state and behavior of an object. Each object is an encapsulation of its state and behavior. Classes are arranged in a certain system and hierarchy so that subclasses can inherit behavior from superclasses. In this class hierarchy there is a root class, which is the class with general behavior. Java programs are organized using classes.

Java also includes an extended collection of classes, which form various packages (Package) that users can use in their own programs. For example, Java provides classes that generate graphical user interface components (java, awt package), where awt is the abbreviation of abstract windowing toolkit (abstract windowing toolkit), classes that handle input and output (java, io package), and classes that support network functions. (java, net package).

3. Distribution

Java is designed to support applications on the network. It is a distributed language. Java not only supports various levels of network connections, but also supports reliable stream network connections with the Socket class, so users can generate distributed clients and servers.

The network becomes a distribution vehicle for software applications. Java programs only need to be written once and run anywhere.

4. Compilation and Interpretability

The Java compiler generates byte-code (byte-code) instead of usual machine code. Java bytecode provides an architecture-neutral object file format, and the code is designed to efficiently deliver programs to multiple platforms. Java programs can run on any system that implements a Java interpreter and run-time system.

In an interpreted environment, the standard "linking" phase of program development largely disappears. If Java still has a linking stage, it is just the process of loading new classes into the environment. It is an incremental and lightweight process. Therefore, Java supports rapid prototyping and easy experimentation, which will lead to rapid program development. This is an elegant development process that contrasts with the traditional, time-consuming "compile, link, and test."

5. Robustness

Java was originally used as a language for writing software for consumer home electronics products, so it is designed to write highly reliable and robust software. Java eliminates certain programming errors, making it fairly easy to write reliable software in it.

Java is a strongly typed language, which allows extended compile-time checking for potential type mismatch issues. Java requires explicit method declarations and does not support C-style implicit declarations. These strict requirements ensure that the compiler catches calling errors, which results in more reliable programs.

One of the most important enhancements in reliability is Java's storage model. Java does not support pointers, which eliminates the possibility of overwriting storage and corrupting data. Similarly, Java's automatic "garbage collection" prevents storage leaks and other harmful errors related to dynamic storage allocation and deallocation. The Java interpreter also performs many runtime checks, such as verifying that all array and string accesses are within bounds.

Exception handling is another feature in Java that makes programs more robust. An exception is a signal that some abnormal condition, similar to an error, has occurred. Using try/catch/finally statements, programmers can find the error handling code, which simplifies the task of error handling and recovery.

6. Security

Java’s storage allocation model is one of its main ways to defend against malicious code. Java has no pointers, so programmers cannot get behind the scenes and fake pointers to memory. More importantly, the Java compiler does not handle storage arrangement decisions, so the programmer cannot guess the actual storage arrangement of the class by looking at the declaration. Storage references in compiled Java code determine the actual storage address at runtime by the Java interpreter.

The Java runtime uses a bytecode verification process to ensure that code loaded onto the network does not violate any Java language restrictions. Part of this security mechanism includes how classes are loaded from the Internet. For example, loaded classes are placed in separate namespaces rather than local classes, preventing a malicious applet from replacing standard Java classes with its own versions.

7. Portability

Java makes language declarations independent of implementation aspects. For example, Java explicitly states the size and operation behavior of each basic data type (these data types are described by Java syntax).

The Java environment itself is portable to new hardware platforms and operating systems. The Java compiler is also written in Java, while the Java runtime system is written in ANSIC language.

8. High performance

Java is a language that is compiled first and then interpreted, so it is not as fast as a fully compiled language. But there are situations where performance is critical. To support these situations, Java designers created a "just-in-time" compiler, which translates Java bytecode into machine code for a specific CPU (central processing unit) at runtime, as well. That is to achieve full compilation.

The Java bytecode format was designed with the needs of these "just-in-time" compilers in mind, so the process of generating machine code is fairly simple, and it produces pretty good code.

9. Multi-threading

Java is a multi-threaded language. It provides support for multi-threaded execution (also known as lightweight processes) and can handle different tasks, making programming with clues very easy. easy. Java's lang package provides a Thread class, which supports methods for starting threads, running threads, stopping threads, and checking thread status.

Java's thread support also includes a set of synchronization primitives. These primitives are based on the widely used synchronization scheme developed by C. A. R. Haore in the style of supervisors and condition variables. Using the keyword synchronized, programmers can indicate that certain methods in a class cannot run concurrently. These methods are under the control of supervisory procedures to ensure that variables are maintained in a consistent state.

10. Dynamics

The Java language is designed to adapt to changing environments. It is a dynamic language. For example, classes in Java are loaded on demand, and some are even obtained over the network.

Recommended tutorial: "Java Tutorial"

The above is the detailed content of Is Java compiled or interpreted?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn