Java小结_第二章_Java应用、输入、输出及运算符简介(Introduction to Java Applications; Input/Output and Operators)



Summary

Section 2.2 Your First Program in Java: Printing a Line of Text

• A Java application executes when you use the java command to launch the JVM.

• Comments document programs and improve their readability. The compiler ignores them.

• A comment that begins with // is an end-of-line comment—it terminates at the end of the line on which it appears.

• Traditional comments can be spread over several lines and are delimited by /* and */.

• Javadoc comments, delimited by /** and */, enable you to embed program documentation in your code. The javadoc utility program generates HTML pages based on these comments.

• A syntax error (also called a compiler error, compile-time error or compilation error) occurs when the compiler encounters code that violates Java’s language rules. It’s similar to a grammar error in a natural language.

• Blank lines, space characters and tab characters are known as white space. White space makes programs easier to read and is ignored by the compiler.

• Keywords are reserved for use by Java and are always spelled with all lowercase letters.

• Keyword class introduces a class declaration.

• By convention, all class names in Java begin with a capital letter and capitalize the first letter of each word they include (e.g., SampleClassName).

• A Java class name is an identifier—a series of characters consisting of letters, digits, underscores (_) and dollar signs ($) that does not begin with a digit and does not contain spaces.

• Java is case sensitive—that is, uppercase and lowercase letters are distinct.

• The body of every class declaration is delimited by braces, { and }.

• A public class declaration must be saved in a file with the same name as the class followed by the “.java” file-name extension.

• Method main   is the starting point of every Java application and must begin with public static void main(String[] args) otherwise, the JVM will not execute the application.

• Methods perform tasks and return information when they complete them. Keyword void indicates that a method will perform a task but return no information.

• Statements instruct the computer to perform actions.

• A string in double quotes is sometimes called a character string or a string literal.

• The standard output object (System.out) displays characters in the command window.

• Method System.out.println displays its argument in the command window followed by a newline character to position the output cursor to the beginning of the next line.

• You compile a program with the command javac. If the program contains no syntax errors, a class file containing the Java bytecodes that represent the application is created. These bytecodes are interpreted by the JVM when you execute the program.

• To run an application, type java followed by the name of the class that contains the main method.

Section 2.3 Modifying Your First Java Program

System.out.print displays its argument and positions the output cursor immediately after the last character displayed.

• A backslash (\) in a string is an escape character. Java combines it with the next character to form an escape sequence. The escape sequence \n represents the newline character.

Section 2.4 Displaying Text with printf

System.out.printf method (f means “formatted”) displays formatted data.

• Method printf’s first argument is a format string containing fixed text and/or format specifiers. Each format specifier indicates the type of data to output and is a placeholder for a corresponding argument that appears after the format string.

• Format specifiers begin with a percent sign (%) and are followed by a character that represents the data type. The format specifier %s is a placeholder for a string.

• The %n format specifier is a portable line separator. You cannot use %n in the argument to System.out.print or System.out.println; however, the line separator output by System.out.println after it displays its argument is portable across operating systems.

Section 2.5 Another Application: Adding Integers

• An import declaration helps the compiler locate a class that’s used in a program.

• Java’s rich set of predefined classes are grouped into packages—named groups of classes. These are referred to as the Java class library, or the Java Application Programming Interface (Java API).

• A variable is a location in the computer’s memory where a value can be stored for use later in a program. All variables must be declared with a name and a type before they can be used.

• A variable’s name enables the program to access the variable’s value in memory.

• A Scanner (package java.util) enables a program to read data that the program will use.

Before a Scanner can be used, the program must create it and specify the source of the data.

• Variables should be initialized to prepare them for use in a program.

• The expression new Scanner(System.in) creates a Scanner that reads from the standard input object (System.in)—normally the keyboard.

• Data type int is used to declare variables that will hold integer values. The range of values for an int is –2,147,483,648 to +2,147,483,647.

• Types float and double specify real numbers with decimal points, such as 3.4 and –11.19.

• Variables of type char represent individual characters, such as an uppercase letter (e.g.,A), a digit (e.g., 7), a special character (e.g., * or %) or an escape sequence (e.g., tab, \t).

• Types such as int, float, double and char are primitive types. Primitive-type names are keywords; thus, they must appear in all lowercase letters.

• A prompt directs the user to take a specific action.

Scanner method nextInt obtains an integer for use in a program.

• The assignment operator, =, enables the program to give a value to a variable. It’s called a binary operator because it has two operands.

• Portions of statements that have values are called expressions.

• The format specifier %d is a placeholder for an int value.

Section 2.6 Memory Concepts

• Variable names (p. 50) correspond to locations in the computer’s memory. Every variable has a

name, a type, a size and a value.

• A value that’s placed in a memory location replaces the location’s previous value, which is lost.

Section 2.7 Arithmetic

• The arithmetic operators are + (addition), - (subtraction), * (multiplication), / (division) and % (remainder).

• Integer division yields an integer quotient.

• The remainder operator, %, yields the remainder after division.

• Arithmetic expressions must be written in straight-line form.

• If an expression contains nested parentheses, the innermost set is evaluated first.

• Java applies the operators in arithmetic expressions in a precise sequence determined by the rules of operator precedence.

• When we say that operators are applied from left to right, we’re referring to their associativity. Some operators associate from right to left.

• Redundant parentheses can make an expression clearer.

Section 2.8 Decision Making: Equality and Relational Operators

• The if statement makes a decision based on a condition’s value (true or false).

• Conditions in if statements can be formed by using the equality (== and !=) and relational (>, <, >= and <=) operators.

• An if statement begins with keyword if followed by a condition in parentheses and expects one statement in its body.

• The empty statement is a statement that does not perform a task.

 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值