[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.
关于printf方法的变量索引
follow the %, and it must be terminated by a $. For example,
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
CAUTION: Argument index values start with 1, not with 0: %1$... formats the first argument. This avoids confusion with the 0 flag.
输入与输出文件流
* 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.
关于for涉及的局部变量
you define a variable inside a for statement, you cannot use the value of that variable outside the loop.
关于switch语句
case labels must be integers orenumerated constants. You cannot test strings. For example, the following is an error:
使用枚举方法
关于label方法
NOTE: Curiously, you can apply a label to any statement, even anif
statement or a block statement, like this:
array定义新变量
NOTE: You can define an array variable either as
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 executesThere 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.
相关文章推荐
- [Core Java. Volume I. Fundamentals, 8th Edition]-6
- Core Java. Volume I. Fundamentals, 8th Edition 下载地址及读后感
- [Core Java. Volume I. Fundamentals, 8th Edition]-5
- [Core Java. Volume I. Fundamentals, 8th Edition]-7,8
- [Core Java. Volume I. Fundamentals, 8th Edition]-2,3
- [Core Java. Volume I. Fundamentals, 8th Edition]-11
- [Core Java. Volume I. Fundamentals, 8th Edition]-4
- Core Java(TM), Volume I--Fundamentals (8th Edition)
- Core Java, Vol. 2: Advanced Features, 8th Edition
- Core Java Volume I - Fundamentals (10th) 1-8章 阅读笔记
- <Core Java Volume I Fundamentals 9th Edition> Note 01
- Core Java Volume I 读书笔记---第十三章 集合
- Core Java Volume I — 1.2. The Java "White Paper" Buzzwords
- From Core Java Volume Ⅰ(1)
- 《Core Java Volume I》学习笔记之命令行方式开发java程序
- 《Servlet与JSP核心编程》笔记(Note of 《Core Servlets and JavaServer Pages:Volume 1:Core Technologies 2nd》)
- Core Java Volume I — 5.1. Classes, Superclasses, and Subclasses
- 【Core Java Volume 3】反射---编写泛型数组代码
- core java volume
- Core Java Volume I — 4.10. Class Design Hints