JAVA packaging class and Number

Packaging and Number-----Preface

Generally, built-in data types such as byte, int, long, double, etc. are usually used when numbers are required. However, in the actual development process, you often encounter situations where you need to use objects instead of built-in data types. To solve this problem, the Java language provides a corresponding wrapper class for each built-in data type.


1. Packaging and Number?

JAVA all data types Integer, Long, Byte, Double, Float, Short abstract class is a subclass of
Number. ~~~~~~~~~~~~~~~~~~~~~
~ ~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~
simple type -------- ----Reference type
byte-------------------Byte
short------------------Short
int-- -------------------Integer
long------------------Long
double--------- -----Double
char------------------Character
boolean-------------Boolean

Second, use steps

The code is as follows (example):

int k=100;
Integer k=100; 100是简单类型,但是可以直接赋值给复杂类型,装箱操作
int k1=k;  k是复杂类型,但是可以直接赋值给简单类型,这是拆箱操作

This kind of packaging that is specifically supported by the compiler is called boxing, so when a built-in data type is used as an object, the compiler will box the built-in type as a packaging class. Similarly, the compiler can unbox an object as a built-in type. The Number class belongs to the java.lang package.

String ss="123";    //输入是字串,如果进行数据类型转换
int kk=Integer.parseInt(ss);//Integer kk=...
Integer k2=Integer.valueOf(ss);//int k2=..
//如果转换失败则报错,例如ss="123d";;;可以是具体数据类型转换为字串类型


The examples here are only int-Integer ; you can try other types yourself.

Guess you like

Origin blog.csdn.net/weixin_42437438/article/details/112910316