首页 > 编程语言 >【Java基础】包装类的使用

【Java基础】包装类的使用

时间:2022-09-19 18:44:59浏览次数:83  
标签:Java String 包装 数据类型 基础 int Integer new

目录

1.包装类是什么

基本数据类型变为引用数据类型,每一个都是一个类。
image

Integer age = new Integer(18);
int age = 18;

2.为什么要用包装类

(1)使基本数据类型具有类的特点,创建对象,调用类的方法,符合面向对象。
(2)基本数据类型的默认值各不相同int的为0,double为0.0...,而对象的默认值都为null,非常方便用来做判空操作,数据库中数据为空的话,使用int类型接收会报错。
(3)阿里巴巴开发手册中强制要求pojo类属性使用包装类。

int i = 0;
Integer i = null;

3.包装类和基本数据类型之间的转换

基本数据类型 --> 包装类

(1)使用构造器

int value = 50;
Integer value2 = new Integer(value);

//字符串作为参数
Integer value3 = new Integer("50");

Integer aa = new Integer("aa"); //异常java.lang.NumberFormatException

(2)自动装箱
JDK1.5中新增了这个自动装箱拆箱的功能,类型必须匹配。

Integer value4 = 50;
Integer value5 = 50.4;  //报错

包装类 --> 基本数据类型

(1)调用包装类对象的xxxValue()方法

Integer value2 = new Integer(50);
int i = value2.intValue();

Double d1 = new Double(50.3);
double d2 = d1.doubleValue();

(2)自动拆箱

Integer v1 = new Integer(30);
int v2 = v1;

4.和String类型之间的转换

基本数据类型 --> String类型

(1)String类的valueOf()静态方法
(2)连接符+

int v1 = 50;
String str1 = String.valueOf(v1);
String str2 = v1+"";

包装类 --> String类型

Integer v1 = new Integer(50);
String str1 = v1.toString();
String str2 = v1.toString(v1);

String类型 --> 基本数据类型

(1)包装类构造器new Integer(str)
(2)包装类的parseInt(str)静态方法

String str1 = "50";
int v1 = new Integer(str1);
int v2 = Integer.parseInt(str1);

String类型 --> 包装类

(1)包装类构造器

String str1 = "55";
Integer v1 = new Integer(str1);

5.面试题

题1

Object o1 = true ? new Integer(1) : new Double(2.0);
System.out.println(o1);

输出结果为1.0
image
三目运算符对于包装类的操作,都会先转换成基本类型,然后根据返回值的接收对象是否是包装类再决定是否装箱。
(1)new Integer()new Double()编译自动拆箱为int和double
(2)表达式2和表达式3要求类型一致,int类型自动提升为double
(3)自动装箱为Double,赋值给o1
(4)打印时,执行子类Double的toString()方法(多态)

题2

Integer i = new Integer(1);
Integer j = new Integer(1);
System.out.println(i == j);//false

Integer m = 1;
Integer n = 1;
System.out.println(m == n);//true

Integer x = 128;
Integer y = 128;
System.out.println(x == y);//false

new出来的对象地址肯定不同,结果为false。
Integer类有一个静态内部类,里面定义了一个cache数组存放[-128,127],在这个范围内直接使用,超出则去new。

点击查看代码:Integer静态内部类
private static class IntegerCache {
        static final int low = -128;
        static final int high;
        static final Integer cache[];

        static {
            // high value may be configured by property
            int h = 127;
            String integerCacheHighPropValue =
                sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
            if (integerCacheHighPropValue != null) {
                try {
                    int i = parseInt(integerCacheHighPropValue);
                    i = Math.max(i, 127);
                    // Maximum array size is Integer.MAX_VALUE
                    h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
                } catch( NumberFormatException nfe) {
                    // If the property cannot be parsed into an int, ignore it.
                }
            }
            high = h;

            cache = new Integer[(high - low) + 1];
            int j = low;
            for(int k = 0; k < cache.length; k++)
                cache[k] = new Integer(j++);

            // range [-128, 127] must be interned (JLS7 5.1.7)
            assert IntegerCache.high >= 127;
        }

        private IntegerCache() {}
    }

自动装箱拆箱原理

标签:Java,String,包装,数据类型,基础,int,Integer,new
From: https://www.cnblogs.com/zhishu/p/16697271.html

相关文章

  • Java实践二
    关于BigInteger:由于计算机使用固定的位数来保存数值,因此,能处理的数值大小是有限的,当要处理的数值超过了这一范围时,计算机将会自动截断数值的二进制表示为它所能处理的最多......
  • BigDecimal除法报错Method threw ‘java.lang.ArithmeticException‘ exception.
    今天使用BigDecimal数据类型做除法的时候,遇到以下报错:Methodthrew‘java.lang.ArithmeticException’exception.Non-terminatingdecimalexpansion;noexactreprese......
  • javascript的类的继承
     先看下java的继承,java的继承是发生在类之间,他们的对象之间没有继承关系,子类的对象通过子类创建对象,同时对象中不存储函数只有数据,函数需找到类定义以后读取里面的函数,类......
  • 了解3D世界的黑魔法-纯Java构造一个简单的3D渲染引擎
    简介: 对于非渲染引擎相关工作的开发者来说,可能认为即使构建最简单的3D程序也非常困难,但事实上并非如此,本篇文章将通过简单的200多行的纯Java代码,去实践正交投影、简单三......
  • JavaScript break 和 continue 语句
    break语句用于跳出循环。continue用于跳过循环中的一个迭代。break语句break语句可用于跳出循环。break语句跳出循环后,会继续执行该循环之后的代码continue语句......
  • 数据结构与算法【Java】07---树结构基础部分
    前言数据data结构(structure)是一门研究组织数据方式的学科,有了编程语言也就有了数据结构.学好数据结构才可以编写出更加漂亮,更加有效率的代码。要学习好数据结构就......
  • JavaScript for 循环
    循环可以将代码块执行指定的次数。for循环:如果向多次运行相同代码,且每次的值都是不同,那就使用循环不同类型的循环JavaScript支持不同类型的循环:for -循环代码块一......
  • JavaScript if...Else 语句
    if语句      语法二:        if(条件表达式){          语句……        }else{     ......
  • HTML基础
    第二天HTML基础列表标签ul无序列表(只允许包含li标签)li可以包含任意内容 ol有序列表(只允许包含li标签)li可以包含任意内容 dl自定义列表(只允......
  • JavaScript switch 语句
    语法switch(n){case1:执行代码块1break;case2:执行代码块2break;default:与case1和case2不同时执行的代码} n通常为变量,随后表达式的值会......