01、基本数据类型的包装类

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

基本数据类型是数据数值

int a = 10;

引用类型是对象引用

String item = "abc";

//http请求中提交的数据[字符串] price = 180 num = 3
String str_price = "180";  //180
String str_num = "3";      //3

//可以使用包装类的方法  字符串-->int  【parse方法\valueOf(s)】
int price = Integr.parseInt(str_price);//
System.out.println(price);//180
int num = Integer.parseInt(str_num);
System.out.println(num);//3
System.out.println(price*num);//540
 

包装类是基本数据类型的引用类型,提供基本数据类型的数据转换和数据操作的方法。

其实就两个要记住的别的首字母大写即可 一个是Integer和Character

--byte Byte

--short Short

--int Integer

--long Long

--boolean Boolean

--float Float

--double Double

--char Character

-- 十进制转换为二、八、十六进制

// 实现进制的转换 java中默认是十进制
System.out.println(" 十进制转为二进制: " + Integer.toBinaryString(15));// 十进制转为二进制: 1111
System.out.println(" 十进制转为八进制 : " + Integer.toOctalString(15));// 十进制转为八进制: 17
System.out.println(" 十进制转为十六进制: " + Integer.toHexString(15));// 十进制十六进制: f

-- 比较 两个数的大小 前比后大的数是1 后比前大是-1 相等是0

System.out.println("比较两个数的大小:" + Integer.compare(10, 10));// 0
System.out.println("比较两个数的大小:" + Integer.compare(-10, 10));// -1
System.out.println("比较两个数的大小:" + Integer.compare(20, 10));// 1
System.out.println("比较两个数的大小:" + Integer.compare(30, 10));// 1
System.out.println("比较两个数的大小:" + Integer.compare(10, 20));// -1

-- 基本数据类型 包装类【自动装箱、自动拆箱】

Integer priceInt = price;// 自动拆箱:基本数据类型:基本数据类型 封装为包装类
int priceItem = priceInt;// 自动拆箱:包装类转换为基本数据类型
int item = priceInt + 3;
System.out.println(item);// 6

--实现基本数据类型和引用类型之间的转换

--自动装箱 将基本数据类型的数据封装为基本数据类型的包装类

--自动拆箱 将基本数据类型的包装类拆封为基本数据类型的数据

-- 数值格式化异常

double d = Double.parseDouble("12.33");
System.out.println(d);
		
// 需要在parseDouble中加上" "
// double d1 = Double.parseDouble(12.33);//String类型的包装类无法转换为double浮点数类型

// Exception in thread "main" java.lang.Error: Unresolved compilation problem:
// The method parseDouble(String) in the type Double is not applicable for the
// arguments (double)


//System.out.println(d1);

Double.parseDouble("12.33");// 数值格式化异常:java.lang.NumberFormatException
// Double.parseDouble("12.33b");//数值格式化异常:java.lang.NumberFormatException

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Lantzruk

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值