Java教程
  • Introduction
  • Getting Started
    • The Java Technology Phenomenon
      • About the Java Technology
      • What Can Java Technology Do?
      • How Will Java Technology Change My Life?
    • The Hello World Application
    • A Closer Look at the Hello World Application
  • Learning the Java Language
    • Object-Oriented Programming Concepts
      • What Is an Object?
      • What Is a Class?
      • What Is Inheritance?
      • What Is an Interface?
      • What Is a package?
    • Language Basics
      • Java Language Keywords
    • Annotations
      • Annotations Basics
      • Declaring an Annotation Type
      • Predefined Annotation Types
      • Repeating Annotations
      • Type Annotations and Pluggable Type Systems
    • Generics
      • Why Use Generics?
      • Generic Types
        • Raw Types
      • Generic Methods
      • Bounded Type Parameters
        • Generic Methods and Bounded Type Parameters
      • Generics, Inheritance, and Subtypes
      • Type Inference
      • Wildcards
        • Upper Bounded Wildcards
        • Unbounded Wildcards
        • Lower Bounded Wildcards
        • Wildcards and Subtyping
        • Wildcard Capture and Helper Methods
        • Guidelines for Wildcard Use
      • Type Erasure
        • Erasure of Generic Types
        • Erasure of Generic Methods
        • Effects of Type Erasure and Bridge Methods
        • Non-Reifiable Types
      • Restrictions on Generics
Powered by GitBook
On this page
  • 源代码注释
  • 类定义
  • main 函数

Was this helpful?

  1. Getting Started

A Closer Look at the Hello World Application

现在你已经成功运行你的Hello World!程序,你可能很好奇其中的原理。现在再来看一遍代码:

class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!"); // Display the string.
    }
}

这个程序由以下三个重要的部分组成。

源代码注释

下面这段代码中加粗的部分就是Comments(注释)


/**
 * The HelloWorldApp class implements an application that
 * simply prints "Hello World!" to standard output.
 */
class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!"); // Display the string.
    }
}

编译器会忽略注释的内容但是对其他开发者来说是很有用的,Java支持一下三种注释:

  • /* text */,编译器会忽略/*到*/之间所有的内容

  • // text,从//开始到一行的行尾内容都会被编译器忽略

类定义

下面这段代码中加粗就是类定义代码块的开头:


/**
 * The HelloWorldApp class implements an application that
 * simply displays "Hello World!" to the standard output.
 */
class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!"); // Display the string.
    }
}

如上所示,最基础的类定义就是:

class name {
    . . .
}

关键字(keyword)class表示类定义的开始,这里的类名是name,类的内容写在两个花括号{和}之间,这里我们用. . .表示

main 函数

下面这段代码中加粗就是main函数的开头:


/**
 * The HelloWorldApp class implements an application that
 * simply displays "Hello World!" to the standard output.
 */
class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!"); //Display the string.
    }
}

在Java中,所有的应用必须包含一个main函数:

public static void main(String[] args)

函数必须包含修饰符public和static(修饰符顺序没有关系)但是一般会这样写public static,参数名也可以随意制定,但是一般选择args或argv。 Java的main函数与C或者C++的很相似,是程序的入口随后将调用程序所需的其他函数。main函数使用字符串数组作为唯一的参数。运行时此数组将被传递给程序,比如运行java MyApp arg1 arg2。此数组的每个字符串称为command-line argument(命令行参数),通过它可以控制程序而不需要重新编译,比如一个排序程序在命令行参数填入-descending就可以对数据进行倒序排列。在Hello World!程序中我们忽略了命令行参数,但是你应该知道它的存在。

最后,我们来看输出Hello World!的代码:

System.out.println("Hello World!");

这里我们使用核心包里的System将消息Hello World!打印到standard output。

PreviousThe Hello World ApplicationNextObject-Oriented Programming Concepts

Last updated 5 years ago

Was this helpful?

/** documentation */,这样的文档注释(简称doc comment),编译器会和第一种注释一样忽略这种注释。而自动生成文档时javadoc会使用这些注释。更多内容请参考

Javadoc™ tool documentation