Java
Java is a high-level, general-purpose, object-oriented programming language. The main design goals of the language were robustness, portability, high performance and security. Java is a multithreaded and distributed programming language. It can be used to create console applications, GUI applications, web applications, both on PCs or embedded systems.
Java是一种高级的,通用的,面向对象的编程语言。该语言的主要设计目标是鲁棒性,可移植性,高性能和安全性。Java是一种多线程和分布式编程语言。它可用于在PC或嵌入式系统上创建控制台应用程序,GUI应用程序,Web应用程序
Java is a programming language created by Sun Microsystems in 1991. The first publicly available version of Java was released in 1995. Today, the language is developed by Oracle corporation.
Java excels in creating portable mobile applications, programming various appliances and in creating enterprise applications.
Java popularity
There are currently several widely used programming languages. Java belongs to the most popular languages today. Several surveys put it into the top three languages in the world.
Java platforms
Java has four programming platforms:
- Java Platform, Standard Edition (Java SE)
- Java Platform, Enterprise Edition (Java EE)
- Java Platform, Micro Edition (Java ME)
- JavaFX
All Java platforms consist of a Java Virtual Machine (JVM) and an application programming interface (API). The Java Virtual Machine is a program, for a particular hardware and software platform that runs Java applications. An API is a collection of software components that we can use to create other software components or applications.
所有Java平台均包含Java虚拟机(JVM)和应用程序编程接口(API)。Java虚拟机是用于运行Java应用程序的特定硬件和软件平台的程序。API是我们可以用来创建其他软件组件或应用程序的软件组件的集合
Java SE is used for developing desktop applications. Java SE's API provides the core functionality of the Java programming language. It consists of a virtual machine, development tools, deployment technologies, and other class libraries and toolkits used in Java applications. Java EE is built on top of the Java SE platform. The Java EE platform provides an API and runtime environment for developing and running web applications and large-scale, multi-tiered, scalable, reliable, and secure enterprise applications. Java ME is a subset of the Java SE. It provides an API and a small-footprint virtual machine for running Java applications on small devices, like mobile phones. JavaFX is a platform for creating rich Internet applications using a lightweight user-interface API.
Java SE用于开发桌面应用程序。Java SE的API提供了Java编程语言的核心功能。它由虚拟机,开发工具,部署技术以及Java应用程序中使用的其他类库和工具包组成。Java EE构建在Java SE平台之上。Java EE平台提供了一个API和运行时环境,用于开发和运行Web应用程序以及大规模,多层,可伸缩,可靠和安全的企业应用程序。Java ME是Java SE的子集。它提供了一个API和一个占地面积小的虚拟机,用于在小型设备(例如手机)上运行Java应用程序。JavaFX是使用轻量级用户界面API创建Internet应用程序的平台
Strictly speaking, Java SE is a platform specification. Java Platform, Standard Edition Development Kit is an official implementation of the Java SE by Oracle. There are also other implementations. For example free and open source OpenJDK or IBM's J9.
严格来说,Java SE是平台规范。Java平台标准版开发套件是Oracle对Java SE的正式实现。
JVM
Java virtual machine (JVM) executes Java bytecode. The JVM is included in the JRE and JDK. Java source code is written in files with the .java
extension. The javac
Java compiler will compile the Java source code into the Java bytecode; the compiled files have the .class
extension. This bytecode is executed by JVM. The java tool is a launcher for Java applications. Oracle's JVM is called HotSpot. HotSpot is a Java virtual machine for desktops and servers. It has advanced techniques such as just-in-time compilation and adaptive optimization designed to improve performance.
Java虚拟机(JVM)执行Java字节码。JVM包含在JRE和JDK中。Java源代码写在扩展名为.java的文件中。javac Java编译器会将Java源代码编译为Java字节码;编译文件的扩展名为.class。该字节码由JVM执行。Java工具是Java应用程序的启动器。Oracle的JVM被称为HotSpot。HotSpot是用于台式机和服务器的Java虚拟机。它具有先进的技术,例如即时编译和旨在提高性能的自适应优化。
JRE
JRE (Java Runtime Environment) is a set of tools for executing Java applications. The JRE does not contain tools and utilities such as compilers or debuggers for developing Java applications.
JRE(Java运行时环境)是用于执行Java应用程序的一组工具。JRE不包含用于开发Java应用程序的工具和实用程序,例如编译器或调试器
JDK
JDK (Java Development Kit) is a superset of the JRE. It contains JRE and tools such as the compilers and debuggers necessary for developing Java applications. We need to install JDK to build and run our Java programs.
JDK(Java开发工具包)是JRE的超集。它包含JRE和开发Java应用程序所需的工具,例如编译器和调试器。我们需要安装JDK来构建和运行我们的Java程序。
Compiling a Java application
We create a simple Java program using command line tools.
$ mkdir -p src/com/zetcode
Inside the current working directory, which is the main project directory, we create the com/zetcode
subdirectory. Java source files are organized in modules called packages. The packages must match the directory structure.
$ mkdir bin
The compiled Java bytecode goes to the bin
directory.
Note: In Java, the public class name must match the name of the file in which it is defined.
$ touch src/com/zetcode/SimpleEx.java
A SimpleEx.java
source file is created in the com/zetcode
subdirectory. Java source files have a .java
extension.
com/zetcode/SimpleEx.java
package com.zetcode;
public class SimpleEx {
public static void main(String[] args) {
System.out.println("This is simple Java example.");
}
}
This is a source code for a simple Java example. This example prints a message to the console.
package com.zetcode;
The package name must correspond to the directory structure in which the source file is located.
public class SimpleEx {
The public class name is required to match the file name.
$ javac -d bin src/com/zetcode/SimpleEx.java
Using the javac
compiler, we compile the source code. Notice that we compile the Java source code from the root project directory. The compiled files go the bin
directory.
$ tree
.
├── bin
│ └── com
│ └── zetcode
│ └── SimpleEx.class
└── src
└── com
└── zetcode
└── SimpleEx.java
6 directories, 2 files
The compiler generates Java bytecode, which is executed by the Java Virtual Machine. The bytecode has a .class
extension.
$ java -cp bin com.zetcode.SimpleEx
This is simple Java example.
With the java
application launcher, we execute the program. It starts a Java runtime environment, loading a specified class, and invoking that class's main method. The .class
extension is excluded; it is assumed. The program name is a fully qualified name of the program — com.zetcode.SimpleEx
. It includes the name of the program and its package. With the -cp
option we tell the launcher where to look for the class files.
Running single-file source code
Since Java 11, it is possible to run single .java
files without defining a package structure and without the need to compile the source code first.
$ ls
SimpleEx.java
There is only one single file in the project directory.
SimpleEx.java
public class SimpleEx {
public static void main(String[] args) {
System.out.println("This is simple Java example.");
}
}
We don't have to define a Java package.
$ java SimpleEx.java
This is simple Java example.
Sources
The following sources were used to create this tutorial: