import FAQ
- Q: Does importing all classes in a package make my object file (.class or .jar) larger?
A: No, import only tells the compiler where to look for symbols.
- Q: Is it less efficient to import all classes than only the classes I need?
A: No. The search for names is very efficient so there is no effective difference.
- Q: Doesn't it provide better documentation to import each class explicitly?
A: This shows good intentions, but ...
- It's hard to remember to remove classes when they are no longer used, so the import list is surprisingly often wrong. It can seriously slow down reading because unusual or unexpected class imports make me look for that class, only to discover that it must have been used in an earlier version.
- Explicit class imports permit accidentally defining classes with names that conflict with the standard library names. This is very bad. Using "*" to import all classes prevents this dangerous naming accident.
- It's annoying to always update this list, altho if you use NetBeans, fixing the list is only a click away (see below).
- Q: I've imported java.awt.*, why do I also need java.awt.event.*?
A: The wildcard "*" only makes the classes in this package visible, not any of the subpackages.
- Q: Why don't I need an import to use String, System, etc?
A: All classes in the java.lang package are visible without an import.
- Q: Is the order of the imports important?
A: No. Group them for readability.
Static imports in Java 5
Java 5 added an import static option that allows static variables (typically constants) to be referenced without qualifying them with a class name. For example, after
import static java.awt.Color;
It would then be possible to write
Color background = RED;
instead of
Color background = Color.RED;
Adding this "feature" wasn't the best idea because it leads to name pollution and confusion about which class constants come from. Even Sun (see References below) basically advises not to use it!
- 使用Character.isJavaIdentifierStart(char ch)可以判断一个字符是不是适合做为java标识符的首字符
- 使用Character.isJavaIdentifierPart(char ch)可以判断一个字符是不是适合做标识符的非首字符
4.良好的命名很重要(转自java rulers),例如
class Reuse {
Reuse Reuse(Reuse Reuse) {
Reuse: for (;;) {
if (Reuse.Reuse(Reuse) == Reuse)
break Reuse;
}
return Reuse;
}
}
你能清晰而准确的看懂这个程序吗?
注:规则的例外:假如已经有了长时间的使用习惯和相互约定,就不用盲从java命名规则
5.关于行分隔符的问题(java rulers)
应该程序应该尽是使用line.separator的系统属性来实现平台无关行分隔,或者例如在写文件的时候使用Writer的newLine()或是PrintWriter的println()方法。
注:line.separator不适用于HTTP的网络程序,因为HTTP中因定CRLF(\r\n)为行分隔符
6.一个好玩的程序(受java rulers的启示)
看java rulers说在java中支持Unicode码做为变量名和类型声明等。就突发其想,写了以下程序:
\u0070\u0061\u0063\u006b\u0061\u0067\u0065\u0020\u0063\u006f\u006d\u002e\u0074\u0069\u0062\u0063\u006f\u002e\u0063\u0064\u0063\u002e\u006c\u0069\u0075\u0067\u0061\u006e\u0067\u002e\u006a\u0061\u0076\u0061\u002e\u0072\u0075\u006c\u0065\u0072\u0073\u003b
\u0063\u006c\u0061\u0073\u0073\u0020\u0054\u0065\u0073\u0074\u007b\u0070\u0075\u0062\u006c\u0069\u0063\u0020\u0073\u0074\u0061\u0074\u0069\u0063\u0020\u0076\u006f\u0069\u0064\u0020\u006d\u0061\u0069\u006e\u0028\u0053\u0074\u0072\u0069\u006e\u0067\u005b\u005d\u0020\u0061\u0072\u0067\u0073\u0029\u0020\u007b\u000a\u0053\u0079\u0073\u0074\u0065\u006d\u002e\u006f\u0075\u0074\u002e\u0070\u0072\u0069\u006e\u0074\u006c\u006e\u0028\u0022\u004c\u0069\u0075\u0067\u0061\u006e\u0067\u0022\u0029\u003b\u000a\u007d\u007d
你能看懂这个程序吗?如果你想看运行结果,可以把这个内容复制,然后在Eclipse某个工程的src目录上粘贴,就可以看有到有一个包:com.tibco.cdc.liugang.java.rulers和该包下的一个Test类会生成。最后运行结果是输出一个:Liugang
可以写好一串java代码的字符串,然后使用以下两个方法进行转换:
public static String toUnicode(String str){
String result = "";
for(int i = 0;i<str.length();i++){
char iChar = str.charAt(i);
result += toUnicode(iChar);
}
return result;
}
public static String toUnicode(char ch){
String hexString = Integer.toHexString(ch);
for(int i =hexString.length();i<4;i++){
hexString = "0"+hexString;
}
return "\\u"+hexString;
}
例如:
System.out.println(toUnicode("package com.tibco.cdc.liugang.java.rulers;"));
System.out.println(toUnicode("class Test{public static void main(String[] args) {\nSystem.out.println(\"Liugang\");\n}}"));
注:居然又发现了Eclipse的一个强大之处,以前只知道一串java内容直接在文件夹上粘贴,可以生成对应的Java类,这次居然发现,这个内容还可以是Unicode码!