【java基础】基础类以及包装类的一些认识

113 阅读3分钟

java 基础类以及包装类的一些认识

基础回顾

image-20230628154715010

使用中的一些问题

自动转换

低类型可以向高类型自动转换。

image-20230628154838107

强制转换

高类型的向底类型转换,但可能会数据溢出或者精度丢失

自动装箱以及自动拆箱

  1. 自动拆箱 包装类——>基本数据类型 (原理是调用了xxxValue方法)
  2. 自动装箱 基本数据类型——>包装类 (原理是调用了valueOf方法)
 @Test
     public void d() {
         /*自动装箱:valueOf*/
         Integer i=123;//原理是 Integer i=Integer.valueOf(123);
         
         /*自动拆箱*/
         int i1=i+1;//原理是    int i1=i.intValue()+1;
         
         Integer a=123;
         Integer b=123;
         Integer c=a+b;
         /*原理为Integer c=Integer.valueOf(a.intValue()+b.intValue());*/
     }

基础类以及包装类的应用场景

基础类就是用来计算,当我们需要计算或者比大小时,可以直接使用基础类型,无需使用包装类型。

包装类就是在面向对象中的时候,需要使用基础类数据时,很不方便,就用包装类代替。

比如 在统计一些数量时的属性。

  <select id="findCount" parameterType="string" resultType="java.lang.Integer">
         select count(1) as amount
         from monitor_station a
         where a.couty_name=#{county}
          and a.fsu_status='交维'
        group by a.couty_name
     </select>

返回的 resultType 是 java.lang.Integer ,为什么用这个,不直接用int 呢?

可能会出现反馈 null 的情况,到时候就不好处理了。

基础类与包装类比大小

在 Java 中,可以使用 基础类 数据类型直接进行大小比较,而不需要使用 包装 类。

如果你有一个 Double 对象,可以通过调用 doubleValue() 方法获取其对应的 double 值后再进行比较。例如:

 Double number1 = 3.14;
 Double number2 = 2.71;
 ​
 if (number1.doubleValue() > number2.doubleValue()) {
     System.out.println("number1 大于 number2");
 } else if (number1.doubleValue() < number2.doubleValue()) {
     System.out.println("number1 小于 number2");
 } else {
     System.out.println("number1 等于 number2");
 }
 ​

包装类如何判断是否为0

需要注意的是,当 Integer 对象为null时,调用 intValue() 方法会引发 NullPointerException。因此,在使用这种方法之前,应该先判断对象是否为null。例如:

 Integer number = null;
 ​
 if (number != null && number.intValue() == 0) {
     System.out.println("number 是0");
 } else {
     System.out.println("number 不是0");
 }
 ​

通过添加 number != null 的条件判断,确保避免了空指针异常的发生。

double 类型的值保留三位小数

可以使用 DecimalFormat 类来格式化数字。通过创建 DecimalFormat 对象并指定 "#.###" 格式,可以保留三位小数。# 是占位符,表示该位置可能有数字,如果没有数字则不显示。使用 format() 方法对 double 值进行格式化后,得到保留三位小数的字符串。

 import java.text.DecimalFormat;
 ​
 public class Main {
     public static void main(String[] args) {
         double number = 3.1415926;
         
         // 创建 DecimalFormat 对象,指定保留三位小数的格式
         DecimalFormat decimalFormat = new DecimalFormat("#.###");
         
         // 使用 DecimalFormat 格式化 double 值
         String formattedNumber = decimalFormat.format(number);
         
         System.out.println("Formatted number: " + formattedNumber);
     }
 }
 ​

int 转 double的一些问题

在进行类型转换时,可能会出现精度丢失的情况。因为 double 类型能够表示更大范围的值,但精度有限。如果 int 值较大或包含小数部分,则在转换为 double 时可能会存在舍入误差。

我这就没有注意,搞了一个bug。例如:

 public class TestMain {
     public static void main(String[] args) {
 ​
         Double ordinary = 0.01D;
         Integer powerTotal = 36;
         Double ordinaryAmout = 30.00D;
         Double larger = 0.1D;
         Integer countTotal=424;
 ​
         // 计算停电比例
         double percent = 0;
 ​
         if(countTotal.intValue() != 0) {
             percent = (double)powerTotal.intValue() / countTotal.intValue();
             System.out.print(percent);
         }
 ​
       if (percent >= ordinary.doubleValue() && powerTotal >= ordinaryAmout.doubleValue() && percent < larger.doubleValue()) {
             System.out.print("-------------->123");
         }
     }
 }

image-20230628173142918

这块,要是不强转 double 类型的话,36/424 就是整数 除整数 ,最后是0.00 ,没有办法保留两位小数.

这种方法只是对结果进行格式化显示,实际进行的计算仍然是浮点数除法。如果需要精确计算小数点后的位数,可以考虑使用 BigDecimal 类进行高精度计算。