首页 > 其他分享 >常用API(七)

常用API(七)

时间:2022-08-14 16:59:14浏览次数:50  
标签:常用 int System API new println Integer out

包装类

基本数据类型对应的引用类型(把基本数据类型变成了对象)

如何理解包装类

 

基本数据类型包装类
byte Byte
short Short
char Character
int Integer
long Long
float Float
double Double
boolean Boolean

Integer

获取方式(了解,JDk5之后做了优化)

方法名说明
public Integer(int value) 根据传递的整数创建一个Integer对象
public Integer(String s) 根据传递的字符串创建一个Integer对象
public static Integer valueOf(int i) 根据传递的整数创建一个Integer对象
public static Integer valueOf(String s) 根据传递的字符串创建一个Integer对象
public static Integer valueOf(String s,int radix) 根据传递的字符串和进制创建一个Integer对象
//利用构造方法获取Integer的对象(JDK5以前的方法)
Integer i1 = new Integer(1);
Integer i2 = new Integer("1");
System.out.println(i1);
System.out.println(i2);
//2.利用静态方法获取Integer的对象(JDK5以前的方法)
Integer i3 = Integer.valueOf("123");
Integer i4 = Integer.valueOf(123);
Integer i5 = Integer.valueOf("123",8);
System.out.println(i3);
System.out.println(i4);
System.out.println(i5);
//3.这两种方式的获取对象的区别
//底层原理
//因为在实际开发中,-128~127之间的数据,用的比较多
//如果每次使用都是new对象,那么太浪费内存了
//所以,提取把这个范围之内的每一个数据都创建好对象
//如果要用到了不会创建新的,而是返回已经创建好的对象
Integer i6 = Integer.valueOf(127);
Integer i7 = Integer.valueOf(127);
System.out.println(i6==i7);//true
Integer i8 = Integer.valueOf(128);
Integer i9 = Integer.valueOf(128);
System.out.println(i8==i9);//false
//因为看到了new关键字,在Java中,每一次new都是创建一个新的对象
//所以下面的两个对象都是new出来的,地址值不一样
Integer i10 = new Integer(127);
Integer i11 = new Integer(127);;
System.out.println(i6==i7);//false
//在以前包装类如何进行计算
Integer i1 = new Integer(1);
Integer i2 = new Integer(2);
//对象之间是不能直接进行计算的
//步骤
//1.把对象进行拆箱,变成基本数据类型
//2.相加
//3.把得到的结果再次进行装箱(再变回包装类)
int result = i1.intValue()+i2.intValue();
Integer i3 = new Integer(result);
System.out.println(i3);
//在JDk5的时候提出了一个机制:自动装箱和自动拆箱
//自动装箱:把基本数据类型会自动的变成其对应的包装类
//自动拆箱:把包装类自动的变成其对象的基本数据类型
//在底层,此时还会去调用静态方法valueof得到一个Integer对象,只不过这个动作不需要我们自己去操作了
//自动装箱动作
Integer i1 =10;
Integer i2 =new Integer(10);
//自动拆箱的动作
int i = i2;
//在JDK5以后,int号Integer可以看做是同一个东西,因为在内部可以自动转化

成员方法

方法名说明
public static String toBinaryString(int i) 得到二进制
public static String toOctalString(int i) 得到八进制
public static String toHexString(int i) 得到十六进制
public static int parseInt(String s) 将字符串类型的整数转换成int类型的整数
//1.把整数转换成二进制,十六进制
String str1 = Integer.toBinaryString(100);
System.out.println(str1);//1100100
//2.把整数转换成八进制
String str2 = Integer.toOctalString(100);
System.out.println(str2);//144
//3.把整数转换成十六进制
String str3 = Integer.toHexString(100);
System.out.println(str3);//64
//4.将字符串类型的整数转换成int类型的整数
//强类型语言,没中数据在java中都有各自的数据类型
//在计算的时候,如果不是同一种数据类型,是无法直接计算的
int i = Integer.parseInt("123");
System.out.println(i);
System.out.println(i+1);//124
//细节1:
//在类型转换的时候,括号中的参数只能是数组不能是其他,都在代码会报错
//细节2:
//8中包装类中,除了Character都有对应的parseXxx方法,进行类型转换
String str = "true";
boolean b = Boolean.parseBoolean(str);
System.out.println(b);

标签:常用,int,System,API,new,println,Integer,out
From: https://www.cnblogs.com/kuangshihe/p/16585736.html

相关文章

  • MinimalAPI---部署项目到IIS
    1.安装IIS,详情见:https://product.pconline.com.cn/itbk/vedio/1903/12395139.html2.安装ASP.NETCore运行时环境和程序包下载HostingBundle文件 安装包下载地址:https......
  • 常用函数
    一、常用日期函数1.unix_timestamp:返回当前或指定时间的时间戳selectunix_timestamp();selectunix_timestamp("2020-10-28",'yyyy-MM-dd');2.from_unixtime:将时间......
  • Nuget服务器包管理常用命令
    设置KeynugetsetApiKey[Your-API-Key]-Sourcehttps://www.nuget.org/api/v2/package/打包nugetpackAliang.Common.Helper.csproj-PropConfiguration=Re......
  • 华测RTK采集的GPX数据,如何带属性转出GIS常用格式并分类加载管理?
    0.序部分自然资源和规划局经常性的基于华测中绘RTK设备采集点位、轨迹等相关数据。比如需要保护的古树、古建、古道路信息等。后续一旦需要对这些古物进行改造或者方案设......
  • FluentValidation 验证:WebApi 中使用 注入服务
    比如你要验证用户的时候判断一下这个用户名称在数据库是否已经存在了,这时候FluentValidation就需要注入查询数据库只需要注入一下就可以了publicclassLogin3Reques......
  • 个人linux下开发常用命令
    以下为个人使用的命令:文件和目录操作命令ls全拼list,功能是列出目录的内容及其内容属性信息cd全拼changediretory,功能是从当前工作目录切换到指定工作目录cp全拼......
  • FluentValidation 验证:WebApi 中使用 基本使用
    FluentValidation.AspNetCore引入包publicclassLogin2RequestValidator:AbstractValidator<Login2Request>{ publicLogin2RequestValidator() { RuleFor(x=>......
  • MinimalAPI---MinimalAPI+EFCore+IOC
    1.项目结构数据库:SQLServer项目类型:MinimalAPI   2.MinimalApi.Db类库(1)引入相关的NuGet包Microsoft.EntityFrameworkCore Microsoft.EntityFrameworkCore.Sq......
  • Docker常用命令
    原文链接:https://www.zhoubotong.site/post/68.htmlDocker常用命令删除安装包:sudo apt-get purge docker-ce删除镜像、容器、配置文件等内容:sudo rm -rf /va......
  • 常用快捷键的使用
    部分快捷键的使用alt+f4:关闭窗口windows+e:打开文件管理器windows+d:回到桌面shift+del:永久删除win+tab:切换应用dos命令dos也就是磁盘操作系统进入命令行窗口的......