Java 简明教程
Java - Features
Java 编程语言最初开发用于在 embedded systems、机顶盒、电视上工作。因此,按照要求,它最初设计用于在各种平台上工作。经过多年的发展,Java 已演变成最流行的用于开发基于 Internet 的应用程序的一种语言。
Java programming language was initially developed to work on embedded systems, settop boxes, television. So by requirements, it was initially designed to work on varied platforms. Over the period of multiple years, Java evolved to become one of the most popular language used to develop internet based applications.
Java 是一种功能丰富的语言,并且随着每个新版本的发布,它都在不断发展。它被广泛应用于数十亿台设备上。以下是 Java 语言的主要功能 -
Java is a feature rich language and with every new version, it is continously evolving. It is widely used across billions of devices. Following are the main features of the Java language -
Object Oriented
在 Java 中,一切都是对象。Java 基于对象模型,因此可以轻松扩展。作为一种具有面向对象功能的语言,Java 支持以下基本 concepts of OOPs −
In Java, everything is an Object. Java can be easily extended since it is based on the Object model. As a language that has the Object-Oriented feature, Java supports the following fundamental concepts of OOPs −
-
Instance
-
Message Passing
Platform Independent
与包括 C 和 C++ 在内的许多其他编程语言不同,当 Java 被编译时,它不会被编译成特定平台的机器码,而是编译成与平台无关的字节码。此字节码通过 Web 分发,并由 Java Virtual Machine (JVM) 在其运行的任何平台上解释。
Unlike many other programming languages including C and C++, when Java is compiled, it is not compiled into platform specific machine, rather into platform independent byte code. This byte code is distributed over the web and interpreted by the Java Virtual Machine (JVM) on whichever platform it is being run on.
Java 是以 Write Once, Run Anywhere (WORA) 方式设计的。用 Java 编写的代码不直接依赖于其运行的计算机类型。在 Java 中的代码在 ByteCode 中进行编译,而 ByteCode 与平台无关。Java 虚拟机(JVM)可以理解字节码。Java 提供了特定于平台的 JVM。特定于平台的 JVM 负责正确解释字节码,因此开发人员可以自由地编写代码,而不用担心平台(如 Windows、Linux、Unix、Mac 等)。此功能使 Java 成为一种平台无关的语言。
Java is designed in Write Once, Run Anywhere (WORA) way. Code written in Java is not directly dependent on the type of machine it is running. A code is Java is compiled in ByteCode which is platform independent. Java Virtual Machine, JVM can understand the byte code. Java provides platform specific JVMs. It is the responsibility of platform specific JVM to interpret the byte code correctly thus developers are free to write code without worrying about platforms like windows, linux, unix, Mac etc. This feature makes Java a platform neutral language.
因为字节码可以通过 Web 分发,并在其上运行的任何平台上由虚拟机(JVM)解释。这使得 java 代码高度可移植,并且对于在多个平台上运行的应用程序很有用。
As byte code can be distributed over the web and interpreted by the Virtual Machine (JVM) on whichever platform it is being run on. It makes java code highly portable and useful for application running on multiple platforms.
Simple
Java 被设计为易于学习。如果您理解了 OOP Java 的基本概念,那么掌握起来会很容易。
Java is designed to be easy to learn. If you understand the basic concept of OOP Java, it would be easy to master.
Java 非常容易学习。它从 C、C++ 继承了许多功能,并去除了指针、运算符重载、多重继承、显式内存分配等复杂功能。它提供自动垃圾回收。通过提供包含数千个有用函数的大量库,Java 使开发人员的生活变得轻松。
Java is very easy to learn. It inherits many features from C, C++ and removes complex features like pointers, operator overloading, multiple inheritance, explicit memory allocation etc. It provides automatic garbage collection. With a rich set of libraries with thousands of useful functions, Java makes developers life easy.
Secure
Java 的安全功能使其能够开发防病毒、防篡改系统。身份验证技术基于公钥加密。
With Java’s secure feature it enables to develop virus-free, tamper-free systems. Authentication techniques are based on public-key encryption.
Java 在设计上高度安全,因为它不要求开发人员与底层系统内存或操作系统进行交互。字节码是安全的,并且缓冲区溢出、内存泄漏等一些安全缺陷非常罕见。Java 异常处理机制允许开发人员处理几乎所有类型的错误/异常,这些错误/异常可能会在程序执行时发生。自动垃圾回收有助于保持系统内存空间利用率受到控制。
Java is by design highly secure as it is not asking developers to interact with underlying system memory or operation system. Bytecode is secure and several security flaws like buffer overflow, memory leak are very rare. Java exception handling mechanism allows developers to handle almost all type of error/exceptions which can happen during program execution. Automatic garbage collection helps in maintaining the system memory space utilization in check.
Architecture-neutral
Java compiler 生成与架构无关的对象文件格式,这让编译代码可以在许多处理器上执行,前提是存在 Java 运行时系统。
Java compiler generates an architecture-neutral object file format, which makes the compiled code executable on many processors, with the presence of Java runtime system.
Java 编译器生成了一个与体系结构无关的目标文件格式,该格式使已编译代码在很多处理器上可执行,并存在 Java 运行时系统。随着处理器体系结构或特定于计算机的处理器的进步,java 代码保持独立于处理器的任何特定要求。由于 java 是一个开放标准,因此甚至可以为自定义体系结构准备一个特定的 JVM。如同现今,我们几乎为所有流行的平台、体系结构准备了 JVM,java 代码完全独立。例如,在 Windows 计算机中创建的 java 程序可在 linux 计算机上运行,而无需修改任何代码。
Java compiler generates an architecture-neutral object file format, which makes the compiled code executable on many processors, with the presence of Java runtime system. With advancement in processor architectures or machine specific processors, java code remains independent of any specific requirement of a processor. As java is an open standard, even a specific JVM can be prepared for a custom architecture. As in today’s time, we’ve JVM available for almost all popular platforms, architectures, java code is completely independent. For example, a java program created in Windows machine can run on linux machine without any code modification.
Portable
与体系结构无关且对规范没有实现依赖性的方面使 Java 具有可移植性。Java 中的编译器是用 ANSI C 编写的,具有明确的可移植性边界,这是一个 POSIX 子集。
Being architecture-neutral and having no implementation dependent aspects of the specification makes Java portable. Compiler in Java is written in ANSI C with a clean portability boundary, which is a POSIX subset.
由于具有这种可移植性,java 从一开始就大受欢迎。它对于基于互联网的应用程序特别有用,因为不同位置的平台各不相同,并且可以在多个平台上使用相同的代码库。因此,多个地点之间的开发人员协作非常容易。
Due to this portability, java was an instant hit since inception. It was particulary useful for internet based application where platforms varied from place to place and same code base can be used across multiple platform. So collaboration between developers was easy across multiple locations.
Robust
Java 通过主要强调编译时错误检查和运行时检查来努力消除容易出错的情况。自动垃圾回收、强大的内存管理、没有指针、没有直接访问系统内存、异常处理、错误处理是 Java 成为一种强大、可靠的语言的一些关键特性。
Java makes an effort to eliminate error prone situations by emphasizing mainly on compile time error checking and runtime checking. Automatic garbage collection, strong memory management, no pointers, no direct access to system memory, exception handling, error handling are some of the key features which makes Java a Robust, strong language to rely on.
Multithreaded
使用 Java 的多线程特性,可以编写能够同时执行许多任务的程序。这个设计特性让开发人员能够构建能够流畅运行的交互式应用程序。
With Java’s multithreaded feature it is possible to write programs that can perform many tasks simultaneously. This design feature allows the developers to construct interactive applications that can run smoothly.
多线程程序包含两个或多个可以同时运行的部分,并且每个部分可以在同一时间处理不同的任务,从而充分利用可用的资源,尤其是在计算机具有多个 CPU 时。
A multi-threaded program contains two or more parts that can run concurrently and each part can handle a different task at the same time making optimal use of the available resources specially when your computer has multiple CPUs.
根据定义,多任务处理是指多个进程共享公共处理资源的情况,例如 CPU。 Multithreading 将多任务处理的理念扩展到您可以在其中将单个应用程序中的特定操作细分为各个线程的应用程序中。每个线程都可以并行运行。OS 不仅在不同应用程序之间分配处理时间,也为应用程序中的各个线程分配。
By definition, multitasking is when multiple processes share common processing resources such as a CPU. Multithreading extends the idea of multitasking into applications where you can subdivide specific operations within a single application into individual threads. Each of the threads can run in parallel. The OS divides processing time not only among different applications, but also among each thread within an application.
通过使用多线程,您可以以在相同程序中可以并行执行多项活动的方式来编写代码。
Multi-threading enables you to write in a way where multiple activities can proceed concurrently in the same program.
Interpreted
Java 字节码会即时翻译为本机机器指令,并且不会存储在任何地方。由于链接是一个渐进式的轻量级过程,因此开发过程更加快速和分析化。
Java byte code is translated on the fly to native machine instructions and is not stored anywhere. The development process is more rapid and analytical since the linking is an incremental and light-weight process.
JVM 位于 javac 编译器和底层硬件之间,javac(或任何其他编译器)编译器将 Java 代码编译成字节码中,该字节码可以被特定于平台的 JVM 所理解。然后 JVM 在代码执行时使用 JIT(即时)编译将字节码编译成二进制文件。
JVM sits in between the javac compiler and the underlying hardware, the javac (or any other compiler) compiler compiles Java code in the Bytecode, which is understood by a platform specific JVM. The JVM then compiles the Bytecode in binary using JIT (Just-in-time) compilation, as the code executes.
High Performance
通过使用即时编译器,Java 可以实现高性能。JVM 使用 JIT 编译器来改善程序的执行时间。以下是 JIT 编译器所执行的一些常规优化:
With the use of Just-In-Time compilers, Java enables high performance. JVM uses JIT compiler to improves the execution time of the program. Below are some general optimizations that are done by the JIT compilers
-
Method inlining
-
Dead code elimination
-
Heuristics for optimizing call sites
-
Constant folding
Dynamic
Java 被认为比 C 或 C++ 更具动态性,因为它旨在适应不断变化的环境。Java 程序可以携带大量的运行时信息,该信息可用于验证和解析在运行时对对象的访问。
Java is considered to be more dynamic than C or C++ since it is designed to adapt to an evolving environment. Java programs can carry extensive amount of run-time information that can be used to verify and resolve accesses to objects on run-time.