小集合

 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。
                                       

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值