Java(2)


        //BigInteger
        BigInteger i1 = new BigInteger("12343124341234");
        BigInteger i2 = new BigInteger("374683744957");
        BigInteger sum = i1.add(i2);
        // BigInteger转换成 long
        System.out.println(i1.longValueExact());

        //BigDecimal
        BigDecimal bd = new BigDecimal("123.233");
        System.out.println(bd.multiply(bd));
        //BigDecimal 用 scale() 表示小数位数
        System.out.println(bd.scale());//3
        BigDecimal bd2 = bd.stripTrailingZeros();//去掉末尾的0
        BigDecimal d3 = new BigDecimal("1234500");
        BigDecimal d4 = d3.stripTrailingZeros();
        System.out.println(d3.scale()); // 0
        System.out.println(d4.scale()); // -2
        //比较BigDecimal的值是否相等,必须使用compareTo()而不能使用equals()。


        //生成 [min, max]范围内的随机数
        double min = 0, max = 0;
        double x = Math.random(); // x 的范围是[0,1]
        // x - 0 / 1 - 0 = random - min / max - min
        x = x * (max - min) + min;
        int y = (int) x; //随机整数
        // 安全的随机数
        SecureRandom sr = null;
        try
        {
            sr = SecureRandom.getInstanceStrong();
        } catch (NoSuchAlgorithmException e) {
            sr = new SecureRandom();
        }
        int[]  n = new int[3];
        sr.nextInt(); //填充随机数


public class hello
{
    public static void main(String[] args) throws UnsupportedEncodingException
    {
        // 异常处理
        /*
        必须捕获的异常,包括Exception及其子类,但不包括RuntimeException及其子类,
        这种类型的异常称为Checked Exception。
        不需要捕获的异常,包括Error及其子类,RuntimeException及其子类。
         在方法定义的时候,使用 throw xx表示该方法可能抛出的类型,
         只要是方法声明的checked exception, 不在调用层捕获,也必须在更高层中捕获,
         最终必须在main()方法中捕获,
         */
        byte[] bs = toGBK("中文");
        System.out.println(Arrays.toString(bs));


    }

    static byte[] toGBK(String s) throws UnsupportedEncodingException
    {
        return s.getBytes("GBK");
    }

    static byte[] toGB(String s)
    {
        try {
            return s.getBytes("GBK");
        } catch (UnsupportedEncodingException e) {
            System.out.println(e);
            e.printStackTrace();// 打印异常栈
            return s.getBytes();
        }
    }
}

//多catch语句,多个catch从上到下匹配,多个catch语句只能有一条语句被执行
        //finally 语句: 确保有无错误都被执行
        String a = "12";
        String b = "x9";
        // TODO: 捕获异常并处理
        int c = stringToInt(a);
        int d = stringToInt(b);
        System.out.println(c * d);
    }

    static int stringToInt(String s) throws NumberFormatException
    {
        s = s.trim();
        StringBuilder ss = new StringBuilder();
        for (int i = 0; i < s.length(); i++)
        if(s.charAt(i) >= (int)'0' &&s.charAt(i) <= (int)'9')
        {

        }
        int res = -1;
        try {
            res = Integer.parseInt(s);
        } catch (NumberFormatException e)
        {
            e.printStackTrace();
        } finally {
          return res;
        }
//抛出异常
//        try {
//            process1();
//        } catch (Exception e)
//        {
//            e.printStackTrace();
//        }
        //因为只能抛出一个异常,没有被抛出的异常被称为被屏蔽的异常
        //如果想要获知所有的异常,
        // 先用origin保存原始异常,然后调用throwable.addSuppressed()将原始异常添加
        Exception origin = null;
        try{
            System.out.println(Integer.parseInt("abc"));
        } catch (Exception e){
            origin = e;
            throw e;
        } finally {
            Exception e = new IllegalArgumentException();
            if (origin != null)
            {
                e.addSuppressed(origin);
            }
            throw e;
        }
        
        
    }
    static void process1()
    {
        try {
            process2();
        } catch (NullPointerException e)
        {
            throw new IllegalArgumentException(e);
        }
    }
    static void process2()
    {
        throw new NullPointerException();
    }

Logger logger = Logger.getLogger(hello.class.getName());
        logger.info("Start process...");
        try {
            "".getBytes("invalidCharsetName");
        } catch (UnsupportedEncodingException e) {
            // TODO: 使用logger.severe()打印异常

            logger.severe(e.toString());
        }
        logger.info("Process end.");

// 使用日志,为了替代System.out.println() 可以定义到格式

JDK的Logging定义了7个日志级别,从严重到普通:
SEVERE
WARNING
INFO
CONFIG
FINE
FINER
FINEST
因为默认级别是INFO,因此,INFO级别以下的日志,不会被打印出来。
使用日志级别的好处在于,调整级别,就可以屏蔽掉很多调试相关的日志输出。

Logger logger = Logger.getGlobal();
        logger.info("start process...");
                logger.warning("memory is running out...");
                logger.fine("ignored");
                logger.severe("process will be terminated");

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值