java学习笔记12:基本数据类型包装类、Math类和Random类

一、基本数据类型包装类

基本数据类型对应包装类
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charChar
booleanBoolean

1、 Integer 类在对象中包装了一个基本类型 int 的值。Integer 类型的对象包含一个 int 类型的字段。

1)、获取int 类型所表示的最大值和最小值
int minValue = Integer.MIN_VALUE;
int maxValue = Integer.MAX_VALUE;
2)、创建Integer对象
//Integer( int value)
//构造一个新分配的 Integer 对象,它表示指定的 int 值。
//Integer(String s)
//构造一个新分配的 Integer 对象,它表示 String 参数所指示的 int 值。
int num=100;
Integer integer = new Integer(num);
System.out.println(integer);
String str="1000";
Integer integer1 = new Integer(str); //要一个字面上全是数字的字符串
System.out.println(integer1);

2、NumberFormatException 数字格式化异常
a.将String类型转换为指定的数据类型时,当String类型不满足需要转换的类型时,抛出该异常。
b.将String类型转换成其他类型时,空格可能要先去掉。
c.超出转换类的值的范围
d.空值也有可能引起错误。
e.进制不同。
3、数字与字符串相互转换

public class MyDemo2 {
    public static void main(String[] args) {
        //(1)、数字-----字符串  100-----"100"
        int num=100;
        //方式1
        String str=num+"";
        //方式2
        String s = String.valueOf(num); //把数字转成字符串的静态方法
        //方式3
        String s1 = new Integer(num).toString();
        //(2)、字符串---数字   "100"--------100
        String str4="100";
        //方式1
        Integer integer = new Integer(str4);
        int i = integer.intValue();
       //方式2
        int parseInt = Integer.parseInt(str4);//静态方法,把字符串转成数字 记住这个方法
        System.out.println(parseInt);
        //(3)、转不同的进制
        String s2 = Integer.toBinaryString(100);//以二进制无符号整数形式返回一个整数参数的字符串表示形式。
        String s3 = Integer.toHexString(100);//以十六进制无符号整数形式返回一个整数参数的字符串表示形式。
        String s4 = Integer.toOctalString(100);//以八进制无符号整数形式返回一个整数参数的字符串表示形式。    
    }
}

4、Character常用方法

public class CharacterDemo {
  public static void main(String[] args) {
     Character c = Character.valueOf('c');
     String str = "abfdfAA102032";  
     //static boolean isUpperCase ( char ch)
     //确定指定字符是否为大写字母。
     //static boolean isSpaceChar ( char ch)
     //确定指定字符是否为 Unicode 空白字符。
     //static boolean isLowerCase ( char ch)
     //确定指定字符是否为小写字母。
     //static boolean isDigit ( char ch)
     //确定指定字符是否为数字。
     boolean a = Character.isUpperCase('a');
     System.out.println(a);
  }
}

5、拆箱与装箱
(1)、手动拆装箱

public class IntegerDemo2 {
    public static void main(String[] args) {
        //手动装箱
        int num=100;
        //方式1
        //Integer integer = new Integer(num);
        //方式2
        Integer integer = Integer.valueOf(num);
        Integer inter2 = Integer.valueOf("1000");
        //手动拆箱
        Integer integer4 = new Integer(100);
        int i = integer4.intValue();
        System.out.println(i+10);
    }
 }

(2)、自动拆装箱

public class IntegerDemo {
    public static void main(String[] args) {
        //JDK1.5 之后 引入了自动拆装箱的机制
        //自动装箱:将基本类型自动包装成对应的引用类型
        //自动拆箱:将包装类型自动转成对应的基本类型
        //int num=100;
        Integer a=100; //自动装箱
        Integer b=200;// 自动装箱
        int sum=a+b;//自动拆箱
        System.out.println("---------------------");
        Integer c=10; //自动装箱
        c+=20;//自动拆箱 自动装箱
    }
}

二、Math类
A、概述:Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。
B、 成员变量

  • public static final double E : 自然底数
  • public static final double PI: 圆周率

C、成员方法

  • public static int abs(int a) 取绝对值
  • public static double ceil(double a) 向上取整
  • public static double floor(double a) 向下取整
  • public static int max(int a,int b) 获取最大值
  • public static int min(int a, int b) 获取最小值
  • public static double pow(double a,double b) 获取a的b次幂
  • public static double random() 获取随机数 返回带正号的 double 值,该值大于等于 0.0 且小于 1.0。
  • public static int round(float a) 四舍五入
  • public static double sqrt(double a)获取正平方根

D、使用Math类的举例

public class MathDemo {
    public static void main(String[] args) {
        System.out.println(Math.PI);
        System.out.println(Math.E);
        //常用的静态方法
        double num = Math.random();    
        System.out.println(Math.abs(-1));
        System.out.println(Math.ceil(3.14));
        System.out.println(Math.floor(3.14));
        System.out.println(Math.max(19, Math.max(1, 10)));
        System.out.println(Math.min(1,90));
        System.out.println(Math.pow(2,3));
        System.out.println(Math.round(6.59));//拿小数点后面第一个数来舍入
        System.out.println(Math.sqrt(4));
        System.out.println(Math.pow(8.0, 1.0/3.0)); //求一个数的立方根
        double sin = Math.sin(Math.PI / 2.0);
        System.out.println(sin);
    }
}

三、Random类
A、概述:此类用于产生随机数如果用相同的种子创建两个 Random 实例,则对每个实例进行相同的方法调用序列,它们将生成并返回相同的数字序列。
B、构造方法

  • public Random() 没有给定种子,使用的是默认的(当前系统的毫秒值)
  • public Random(long seed) 给定一个long类型的种子,给定以后每一次生成的随机数是相同的。

C:成员方法

  • public int nextInt()//没有参数 表示的随机数范围 是int类型的范围
  • public int nextInt(int n)//可以指定一个随机数范围

D:使用Random类举例

public class RandomDemo {
    public static void main(String[] args) {
        //创建随机类
        Random random = new Random();
        for (int i = 0; i < 1000; i++) {
           // int num= random.nextInt(); 不指定随机整数的范围,生成的数在int类型的范围内
            int num = random.nextInt(10);  //随机数的范围 大于等于 0 小于 10
            boolean b = random.nextBoolean();
            double v = random.nextDouble();
            System.out.println(num);
            System.out.println(b);
            System.out.println(v);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值