java包装类型、编码规范

目录

一、JAVA 编码规范

二、 Number 抽象类的学习

三、Integer (int)

四、String Integer int 相互转换

五、包装类型对应

六、大数据运算

七、随机数 Random


一、JAVA 编码规范

  1. 编码对于我们程序员来说,特点重要,原因如下:

    1. 一个软件的生命周期,90%时间都是在维护系统

    2. 良好的编码习惯可以改善代码的可读性

  2. 编码规范:

    • 起名:做到见名知意,遵循标识符规范

      • 不能使用拼音,采用驼峰命名法(StudentInfo)

    • 边写代码边测试,打印中间变量进行观察,确保程序正确性

    • 基本规范:

      • 包名:域名倒写,工具类 utils , 控制层 controller

      • 类名:一般使用名词,并且首字母大写,不要使用jdk 内置名字

      • 接口名:单词前面习惯加字母 i (IUser)

      • 方法名:(动词),eat(),saveUser();

      • 变量名:首字母小写,多个单词用驼峰,studentName

      • 常量名:final 修饰的,多个单词全部大写,用_隔开,MAX_VALUE,MIN_VALUE

二、 Number 抽象类的学习

 //获取 int 类型的值
public abstract int intValue();

//获取 long 类型的值
public abstract long longValue();

public abstract float floatValue();

public abstract double doubleValue();

public byte byteValue() {
    return (byte)intValue();
}

public short shortValue() {
    return (short)intValue();
}

三、Integer (int)

 

  1. 构造器学习

    public Integer(String s) throws NumberFormatException {
        this.value = parseInt(s, 10);
    }
    //将字符串解析成 int 类型
    public static int parseInt(String s) throws NumberFormatException {
        return parseInt(s,10);
    }
    ​
    //将字符串解析成 Integer 类型的数
    public static Integer valueOf(String s) throws NumberFormatException {
        return Integer.valueOf(parseInt(s, 10));
    }
    ​
    //将Integer 对象转换成 int 数
    public int intValue() {
        return value;
    }

  2. 自动装箱和拆箱(语法糖)

    //自动装箱
    Integer integer3 = 10;
    Integer integer3 = Integer.valueOf(10);
    //自动拆箱
    int intValue = integer3;
    int intValue = integer3.intValue();
  3. 整数类型做了一个缓存处理

    private static class IntegerCache {
        //缓存了 [-128,127]
    }

 

四、String Integer int 相互转换

  1. String 转 Integer

    Integer.valueOf()
    ​
    new Integer();
  2. String 转 int

    Integer.valueOf();
  3. Integer 转 int

    integer 对象.intValue();

 

五、包装类型对应

基本类型默认值包装类型包装类型默认值
int0Integernull
long0Longnull
booleanfalseBooleannull
byte0Bytenull
short0Shortnull
float0.0Floatnull
double0.0Doublenull
char''Characternull

 

  1. 注意:开发过程中,建议使用包装类型

六、大数据运算

 

  1. BigInteger : java 开发中,我们的数超过了 long 类型,使用它

    public BigInteger(String val) {
        this(val, 10);
    }
    • 四则运算:

    public BigInteger add(BigInteger val) {}
    ​
    public BigInteger subtract(BigInteger val) {}
    ​
    public BigInteger multiply(BigInteger val) {}
    ​
    public BigInteger divide(BigInteger val) {}
  2. BigDecimal:具有精度运算的

    • 需求:

      • 打印 0.09 + 0.01

      • 打印 1 - 0.34

      • 打印 1.403 / 100

    • 注意:做金钱运算使用 BigDecimal

    • 里面有加减乘除的方法

        BigDecimal bigDecimal = new BigDecimal("0.09");
        BigDecimal bigDecimal1 = new BigDecimal("0.01");
        BigDecimal add = bigDecimal.add(bigDecimal1);
        System.out.println(add); 

 

七、随机数 Random

  1. 需求:随机生成 1- 100 之间的数,生成10次

    Random random = new Random();
    ​
    for (int i = 0; i < 10; i++) {
        int j = random.nextInt(100);
        System.out.println(j);
    }
  2. 需求:随机生成 100-200 之间的随机数,生成10次

    //多思考
    Random random = new Random();
    ​
    for (int i = 0; i < 10; i++) {
        int j = random.nextInt(100) + 100;
        System.out.println(j);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值