包装&工具类

一.包装类

    它是把基本数据类型封装成类的形式。

  1.常用

    (1)Long;(2)Integer;(3)Double;(4)Float;(5)Boolean:字符串除了true(不区分大小写)之外,都转成false。

  2.提供类型转换方法

    (1)实例方法(2)静态方法:parseLong()。

二.工具类

    1.Math数学工具类

       (1)四舍五入round;

       (2)上限值  ceil():大于或等于它的最小整数;

       (3)下限值floor():小于或等于它的最大整数;

       (4)随机数random():0~1之间的随机数。

     2.Random 随机数

      (1)可以在构造方法里传递随机数种子;

      (2)根据随机数种子计算出来的伪随机数序列;

      (3)nextInt  (最大值);

      (4)默认的随机数种子是时间值。

包装实现方法:

 1 package org.hanqi.array;
 2 
 3 import java.util.Random;
 4 
 5 public class BaoZhuang {
 6 
 7     public static void main(String[] args) {
 8         
 9         
10         //包装类
11         Long l=new Long(100);
12         
13         //把字符串转成数值
14         Long l1=new Long("1000");
15         
16         String  str =1000+"";
17         
18         //从包装类转成基本数据类型
19         long l2=l1.longValue();
20 
21         System.out.println("l2="+l2);
22 
23         long l3=Long.parseLong("1200");
24         
25         
26         //int 
27         Integer i=new Integer("100");
28         
29         Integer.parseInt("100");
30         
31         //float 
32         Float f=new Float("123.45");
33         
34         Float.parseFloat("123.45");
35         
36         //double
37         Double d=new Double("12345.67");
38         
39         Double.parseDouble("12345.67");
40         
41         //boolean
42         Boolean b=new Boolean("true");
43         
44         System.out.println(b.booleanValue());
45         
46         
47         
48         //数学工具类
49         
50         System.out.println(Math.PI);
51         
52         //四舍五入
53         System.out.println(Math.round(1234.56789));
54         
55         double de=1234.56;
56         
57         //保留小数点后2位
58         System.out.println(Math.round(de*100)/100.0);
59         
60         //舍去小数点后数字
61         
62         //下限值:小于或等于它的最大整数
63         System.out.println(Math.floor(de));
64         
65         //上限值:大于或等于它的最小整数
66         System.out.println(Math.ceil(de));
67         
68         
69         //随机数 0~1之间
70         
71         System.out.println(Math.random());
72         System.out.println(Math.random());
73         System.out.println(Math.random());
74         System.out.println(Math.random());
75         System.out.println(Math.random());
76         System.out.println(Math.random());
77         System.out.println(Math.random());
78         
79         System.out.println();
80         
81         Random r=new Random();
82         
83         //随机数种子
84         //伪随机数
85         //根据种子计算出来的
86         //由种子决定随机数的产生序列
87         //r=new Random(10);
88         
89         for(int m=0;m<10;m++)
90         {
91             System.out.println(r.nextInt(30));
92         }
93         
94     }
95 }
BaoZhuang

原文地址:https://www.cnblogs.com/arxk/p/5267322.html