双语版Java程序设计
上QQ阅读APP看本书,新人免费读10天
设备和账号都新为新人

2.5 Lexical Elements

Java is a programming language and like any other language there are certain rules about how“words” in the language are formed and what constitutes a legal “sentence” in the language. In addition, Java has rules about how the sentences can be put together into a complete program, somewhat like the less formal rules about how to format a formal letter (e.g. with the address of the recipient, and opening salutation etc.) or how to structure a book (e.g. with chapters, a table of contents etc.). In this section we will begin by looking at the various “words” and symbols, called lexical elements, that are used to construct Java programs.

Java和其他程序语言一样,有一些特定的语句规则,这些规则约束了哪些单词可以使用,以及如何构成程序语言,这有点类似于写一封规范的信(例如,收信人的称呼、开场白等),或者是如何构建一本书(例如,篇章、目录等)。

The most fundamental element in the structure of a program is a single character that can be displayed on a computer screen or typed at a computer keyboard. Prior to Java, most programming languages in common use during the last 10-20 years have used what is known as the ASCII character set. This character set provides for 256 different characters. This may seem like a lot, but when you consider all of the human languages in the world and the various symbols they use, it turns out that 256 is not enough. Therefore, Java has adopted what is called the Unicode character set. This set provides for more than 64,000 different characters.

When a Java compiler first begins to analyze a Java program it groups the individual characters together into larger lexical elements usually called tokens(符号). Some of the tokens will only be one character long such as the plus sign “+” which is the Java symbol used to add two numbers together(among other things). Other tokens will be many characters long such as the keywords like class and public. These basic tokens are then combined into larger language structures such as expressions, method calls, class definitions etc.

当Java编译器开始分析Java程序时,它将各个字符集合在一起成为更大的元素,通常称为符号。有些符号只有一个字符长,例如“+”在Java语言中将两个数字加在一起。其他符号可以为多个字符,如关键词class和public。