Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1734187
  • 博文数量: 297
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 3006
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-06 22:04
个人简介

Linuxer, ex IBMer. GNU https://hmchzb19.github.io/

文章分类

全部博文(297)

文章存档

2020年(11)

2019年(15)

2018年(43)

2017年(79)

2016年(79)

2015年(58)

2014年(1)

2013年(8)

2012年(3)

分类: Java

2017-03-06 14:48:30

1.  enum 居然有public static void main(String[] args) ,该例子出自wicked cool java.

点击(此处)折叠或打开

  1. package hezhb_test;

  2. //enum Dessert{PIE, CAKE, ICECREAM, BROWNIE};
  3. enum FruitCategory{SWEET, CITRUS,SMELLY,UNKNOWN};
  4. //enum Fruit{APPLE, ORANGE,GRAPEFRUIT,BANANA,DURIAN};

  5. public enum MyFruit {
  6.     
  7.     APPLE{ FruitCategory getCategory(){
  8.             return FruitCategory.SWEET;}
  9.     },
  10.     
  11.     ORANGE{ FruitCategory getCategory(){
  12.         return FruitCategory.CITRUS;}
  13.     },
  14.     
  15.     GRAPEFRUIT{ FruitCategory getCategory(){
  16.         return FruitCategory.CITRUS;}
  17.     },
  18.     
  19.     BANANA { FruitCategory getCategory(){
  20.         return FruitCategory.SWEET;}
  21.     },
  22.     
  23.     DURIAN { FruitCategory getCategory(){
  24.         return FruitCategory.SMELLY;}
  25.     };
  26.     
  27.     abstract FruitCategory getCategory();
  28.     
  29.     public static void main(String[] args){
  30.         MyFruit a = MyFruit.APPLE;
  31.         //toString() returns "APPLE"
  32.         System.out.println("The toString(); for a:"+a);
  33.         //getCategory() returns "SWEET"
  34.         System.out.println("a.getCatetory() is: "+a.getCategory());
  35.         for(MyFruit f: MyFruit.values()){
  36.             System.out.println("Fruit is :"+f);
  37.         }
  38.     }
  39. }

2.  写个ThreadSleepDemo看看纳秒的精度,感觉ms和ns的精度有点吓人啊。根本是无法控制的,误差肯定会很大.
当前我犯了个错误而eclipse居然没发现,我知道long在c里面printf要用%ld,我就顺手写了个%ld,然后执行出错,但是eclipse不认为我有错。
报错是这个:

点击(此处)折叠或打开

  1. Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = 'l'
后来我改成了%d才能执行,看了看这个,发现java 没有%ld

点击(此处)折叠或打开

  1. package multithread;

  2. public class ThreadSleepDemo {
  3.     public static void main(String[] args){
  4.         Runnable r = ()->{
  5.             try{
  6.                 Thread.sleep(10);            //sleep 10ms
  7.                 Thread.sleep(0,10000);        //sleep 10000ns,0.01ms
  8.             }catch(InterruptedException e){
  9.                 e.printStackTrace();
  10.             }
  11.         };
  12.         
  13.         Thread tA = new Thread(r);
  14.         long startTime = System.nanoTime();
  15.         r.run();
  16.         long endTime = System.nanoTime();
  17.         long eclapsed = endTime -startTime;
  18.         System.out.printf("elapsed time is %d",eclapsed);
  19.     }
  20. }


阅读(2417) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~