java新手需了解的常用类、拆装包干货

Java入门到精通(一)语言基础
Java入门到精通(二)流程控制
Java入门到精通(三)数组介绍
Java入门到精通(四)字符串
Java入门到精通(五)面向对象
java入门到精通(六)面向对象知识拓展
讲给老年人听的异常处理
给大家安利一个jdk文档查看网站各种jdk版本文档都可以查看,我下面文章只提及常见的方法
jdk文档查看

包装类

包装类可以将基本数据类型的数据转换为对象进行处理。
包装类及其对应的基本类型

包装类对应的数据类型
Bytebyte
Integerint
Floatfloat
Characterchar
Shortshort
Longlong
Doubledouble
Booleanboolean

Integer类

Integer的构造函数

Integer(int val)
//Constructs a newly allocated Integer object that represents the specified int value.
//传入一个int变量获取Integer对象
Integer(String str)
//Constructs a newly allocated Integer object that represents the int value indicated by the String parameter.
//传入一个String变量获取Integer对象,注意:字符串变量一定要是数值型否则编译器会抛出NumberFormatException。
方法返回值描述
valueOf(String str)Integer返回保存指定的String值的Integer对象
parseInt(String str)int返回包含在由str指定的字符串中的数字的等价整数值
toString()String返回一个表示该Integer值的String对象
equals(Object obj)boolean比较此对象与指定的对象是否相等
intValue()int以int型返回此Integer对象
compareTo(IntegeranotherInteger)int在数字上比较两个Integer对象、如果这两个值相等,则返回0;如果小于IntegeranotherInteger返回负值,反之返回正值

Integer 常量

常见的常量有哪些?
MAX_VALUE = 2^31-1
MIN_VALUE = -2^31
SIZE 用来以二进制的形式来表示int值的位数
TYPE 表示基本类型int的Class实列

Double类

Double对象的构造函数

Double(double value)
//Constructs a newly allocated Double object that represents the primitive double argument.
//传入double形式的参数创建Double对象
Double(String s)
//Constructs a newly allocated Double object that represents the floating-point value of type double represented by the string.
//传入String变量作为参数创建Double对象
方法返回值描述
valueOf(String str)Double返回保存指定的String值的Double对象
parseDouble(String str)double将String变量转换为Double返回一个Double值
doubleValue ()double以double形式返回Double对象
intValue()int将double转换为int型
compareTo(IntegeranotherInteger)int在数字上比较两个Integer对象、如果这两个值相等,则返回0;如果小于IntegeranotherInteger返回负值,反之返回正值
equals(Object obj)boolean比较此对象与指定的对象是否相等
toString()String返回一个表示该double值的String对象

Boolen类

Boolean(boolean value)
//Allocates a Boolean object representing the value argument.
//创建表示value参数的对象
Boolean(String s)
//Allocates a Boolean object representing the value true if the string argument is not null and is equal, ignoring case, to the string "true".
//以String变量作为参数创建对象
方法返回值描述
booleanValue()boolean将Boolean对象的值以对应的boolean值返回
equals(Object obj)boolean比较此对象与指定的对象是否相等并且值不为null
parseBoolean(String s)boolean将字符串转换为Boolean
toString()String返回一个表示该boolean值的String对象
valueOf(String s)boolean返回一个用指定字符串表示值的Boolean值

Character类

Character的构造函数

Character(char value)
Constructs a newly allocated Character object that represents the specified char value.
//将基本数据类型char转换为Character对象
方法返回值描述
compareTo(IntegeranotherInteger)int在数字上比较两个Integer对象、如果这两个值相等,则返回0
equals(Object obj)boolean比较此对象与指定的对象是否相等
toUpperCase(char ch)char将ch变为大写
toLowerCase(char ch)char将ch变为小写
toString()String返回一个表示该char值的String对象
charValue()char返回此Character对象的值
isDigit(char ch)boolean判断是否是数字
isLetter(char ch)boolean判断是否是字母

Math数学类

static double	E
//自然对数底数e的值
//The double value that is closer than any other to e, the base of the natural logarithms.
static double	PI
//表示圆周率PI的值
//The double value that is closer than any other to pi, the ratio of the circumference of a circle to its diameter.

Math类常见的方法

方法介绍
sqrt(double a)a的平方根
pow(double a,double b)a的b次方
ceil(double a)将a向上取整
floor(double a)将a向下取整
max(double a,double b)将a和b做比较返回最大值
min(double a,double b)将a和b作比较返回最小值
abs(double a)放回a的绝对值
random()生成随机数
//random()方法 产生1~100的随机数切包括100
System.out.println(1 + Math.random()*100);
System.out.println(Math.random()*101);

Random类

java编译器以系统当前时间作为随机数生成器种子,因为每时每刻的时间不可能相同,所以生成的随机数将不同,但是如果运行速度太快,也会生成两次运行结果相同的随机数。

Random random = new Random();
int i = random.nextInt() * 100;//产生随机整数
float v = random.nextFloat()*100;//产生随机浮点数
boolean b = random.nextBoolean();//产生随机布尔值

拆装包

装包

自动装包:把基本类型用它们对应的引用类型包装起来,使它们具有对象的特质,可以调用toString()、hashCode()、getClass()、equals()等方法。

Integer a = 10;//装包

编译器调用的是valueOf(int i)返回一个表示指定int值的Integer对象,如下:

Integer a = 10;   =>    Integer a = Integer.valueOf(10);
        Integer a = 100;
        Integer b = 200;
        Integer c = 100;
        Integer d = 200;
        Integer e = new Integer(10);
        Integer f =  new Integer(10);
        System.out.println(a == b);//false
        System.out.println(a == c);//true
        System.out.println(b == d);//false
        System.out.println(e == f);//false
        System.out.println(b.equals(d));//true
        System.out.println(e.equals(f));//true
  • a与b两个值不相等,并且使两个不同的对象所以会输出false
  • a与c输出true两个值相等,且是同一个对象,所以输出true
  • b与d两个不同的对象,值相等,但是==比较的是两个对象的地址所以输出false
  • e与f两个不同的对象,值相等,两个对象的地址不同输出false
  • 最后两个输出语句是调用equals方法比较地址下面的值

以下是装包的源码:

public static Integer valueOf(int i){
      if(i>=IntegerCache.low && i<=IntegerCache.high){
             return IntegerCache.cache[i+(-IntegerCache.low)];
             }
       return new Integer(i);
       }
  • 两个Integer比较的时候,由于直接赋值的时候会进行自动的装箱,一个是-128<=
    x<=127的整数,将会直接缓存在IntegerCache中,当赋值在这个区间的时候,不会创建新的Integer对象,而是从缓存中获取已经创建好的Integer对象。

  • 当赋值不在这个范围内,编译器直接创建对象,就是上述代码中的Integer b 与 Integer d作比较。

拆包

所谓拆包就是将引用类型转换为基本数据类型,编译器内部会调用int intValue()返回该Integer对象的int值。

int a = new Integer(10);//拆箱

自动装箱和拆箱是由编译器来完成的,编译器会在编译期根据语法决定是否进行装箱和拆箱动作。

Integer a = 100;
int b = 100;
Integer c = 200;
System.out.println(c > a);//true
System.out.println(b == a);//true
  • c 与 a将c、a拆包成基本数据类型 然后相互作比较输出true
  • b 与 a将a拆包成基本数据类型然后与b作值比较

这篇文章对装包、拆包做出了详细的讲解大家可看看

描述不对的地方麻烦大佬补充
看完感觉有用的点个赞,没有的也点个赞

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值