您的位置:首页 > 编程语言 > Java开发

[Core Java. Volume I. Fundamentals, 8th Edition]-3

2013-06-21 17:12 267 查看
利用StringBuilder加速碎片语句的拼接

need to build up strings from shorter strings,Every time you concatenate strings, a newString object is constructed. This is time consuming and it wastes memory.

StringBuilder builder = new StringBuilder();
builder.append(ch); // appends a single character 
 builder.append(str); // appends a string
完毕后
String completedString = builder.toString();


关于printf方法的变量索引

follow the %, and it must be terminated by a $. For example,

System.out.printf("%1$s %2$tB %2$te, %2$tY", "Due date:", new Date());

prints Due date: February 9, 2004

< flag indicates that the same argument as in the preceding format specification should be used again. That is, the statement

System.out.printf("%s %tB %<te, %<tY", "Due date:", new Date());
yields the same output as the preceding statement.

CAUTION: Argument index values start with 1, not with 0: %1$... formats the first argument. This avoids confusion with the 0 flag.


输入与输出文件流

PrintWriter out = new PrintWriter("myfile.txt");
Scanner in = new Scanner(new File("myfile.txt"));
java.util.Scanner 5.0

* Scanner(File f)

constructs a Scanner that reads data from the given file.

* Scanner(String data)

constructs a Scanner that reads data from
the given string.

java.io.PrintWriter 1.1

* PrintWriter(File f)

constructs a PrintWriter that writes data to the given file.

* PrintWriter(String fileName)

constructs a PrintWriter that writes data to the file with the given file name.

java.io.File 1.0

* File(String fileName)

constructs a File object that describes a file with the given name. Note that the file need not currently exist.

java中的变量重复定义是错误的,虽然c++支持这种写法

C++ NOTE: In C++, it is possible to redefine a variable inside a nested block. The inner definition then shadows the outer one. This can be a source of programming errors; hence, Java does not allow it.

public static void main(String[] args) 
                      { 
                         int n; 
                         . . . 
                         { 
                            int k; 
                            int n; // error--can't redefine n in inner block 
                            . . . 
                         } 
                      }


关于for涉及的局部变量
you define a variable inside a for statement, you cannot use the value of that variable outside the loop.

for (int i = 1; i <= 10; i++) 
                       { 
                          . . . 
                       } 
                       . . . 
                       for (int i = 11; i <= 20; i++) // ok to define another variable named i 
                       { 
                          . . . 
                       }
以上写法在as3中不成立,但在java中是成立的

关于switch语句

case labels must be integers orenumerated constants. You cannot test strings. For example, the following is an error:

String input = . . .; 
                      switch (input) // ERROR 
                      { 
                         case "A": // ERROR 
                            . . . 
                            break; 
                         . . . 
                      }
as3中的switch自由了去了

使用枚举方法

Size sz = . . .; 
                     switch (sz) 
                     { 
                        case SMALL: // no need to use Size.SMALL 
                           . . . 
                           break; 
                        . . . 
                     }


关于label方法

Scanner in = new Scanner(System.in); 
                         int n; 
                         read_data: 
                         while (. . .) // this loop statement is tagged with the label 
                         { 
                            . . . 
                            for (. . .) // this inner loop is not labeled 
                            { 
                                System.out.print("Enter a number >= 0: "); 
                                n = in.nextInt(); 
                                if (n < 0) // should never happen—can't go on 
                                   break read_data; 
                                   // break out of read_data loop 
                                . . . 
                            } 
                         } 
                         // this statement is executed immediately after the labeled break 
                         if (n < 0) // check for bad situation 
                         { 
                            // deal with bad situation 
                         } 
                         else 
                         { 
                            // carry out normal processing 
                         }


NOTE: Curiously, you can apply a label to any statement, even anif
statement or a block statement, like this:

label : 
                                 { 
                                    . . . 
                                    if (condition) break label ; // exits block 
                                    . . . 
                                 } 
                                 // jumps here when the break statement executes
There is also a labeled form of the continue statement that jumps to the header of the loop with the matching label.

array定义新变量

NOTE: You can define an array variable either as

int[] a;
or as

int a[];
Most Java programmers prefer the former style because it neatly separates the type int[] (integer array) from the variable name.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: