1。获取你系统默认字符集 并打印 System.out.println( java.nio.charset.Charset.defaultCharset() );
2。想知道你的这么类的完整名称吗
System.out.println(Me.class.getName()+".class")
//这里面是两个//才可以,一定要知道原因
System.out.println(this.getClass().getName().replaceAll("//.","/") + ".class");
//下面这个也是可以的,需要JDK1.5,这个pattern专门用了配合模式匹配
//需要这样 import java.util.regex.Pattern;
System.out.println(this.getClass().getName().replaceAll(Pattern.quote("."),"/") + ".class");
//下面这个也许更好,用平台相关的文件分隔符
//需要这样 import java.util.regex.Pattern;import java.util.regex.Matcher;当然还有File类
System.out.println(this.getClass().getName().replaceAll("//.",
Matcher.quoteReplacement(File.separator)) + ".class");
下面的一个方法是最好的了,我不知道JDK1.4底下是否能用,用replace,你可得注意,这个也是全部替代哦
System.out.println(this.getClass().getName().replace(".", File.separator) + ".class");
3。Random的妙用
import java.util.Random;
System.out.println("PGM".charAt(rnd.nextInt(3)) + "ain");
上面这个程序片断比下面的这个要简洁很多
private static Random rnd = new Random();
StringBuffer word = null;
switch(rnd.nextInt(3)) {
case 1:
word = new StringBuffer("P");
break;
case 2:
word = new StringBuffer("G");
break;
default:
word = new StringBuffer("M");
break;
}
word.append('a');
word.append('i');
word.append('n');
System.out.println(word);
下面是我从书上看到的一个更好更加通用的例子
public static void main(String[] args) {
String a[] = {"Main","Pain","Gain","summer","spring"};
System.out.println(randomElement(a));
}
private static Random rnd = new Random();
private static String randomElement(String[] a){
return a[rnd.nextInt(a.length)];
}
4。null
String s = null;
System.out.println(s instanceof String);
尽管null 对于每一个引用类型来说都是其子类型,但是instanceof 操作符被定
义为在其左操作数为null 时返回false。