Java学习一

1.注释大量代码最简单的方法就是:把代码放在一个永远不会执行的if语句块里,例如:if(false){//大量语句}
 

import FAQ

  1. 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.

  2. 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.

  3. 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).
  4. 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.

  5. 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.

  6. 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!

注:使用诸如java.util.*这样的导入并不会把java.util包下的所有类导入,而只有在类中使用到的类会被导入!
 
3.关于标识符(Identifier)
  • 使用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码!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值