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

利用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. 



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章