当前位置: 首页 > news >正文

【JDK常用的API】包装类

🍬 博主介绍👨‍🎓 博主介绍:大家好,我是 hacker-routing ,很高兴认识大家~
✨主攻领域:【渗透领域】【应急响应】 【Java】 【VulnHub靶场复现】【面试分析】
🎉点赞➕评论➕收藏 == 养成习惯(一键三连)😋
🎉欢迎关注💗一起学习👍一起讨论⭐️一起进步📝文末有彩蛋
🙏作者水平有限,欢迎各位大佬指点,相互学习进步!

目录

获取Integer对象的方式

在以前包装类如何进行计算

总结:

Integer 成员方法

使用字符串进行进制转换

键盘录入,使用nextLine


获取Integer对象的方式

public class demon1 {public static void main(String[] args) {/*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对象*///1、利用构造方法获取intege的对象(jdk5以前)Integer i1 = new Integer(1);Integer i2 = new Integer("2");System.out.println(i1);System.out.println(i2);//2、利用静态方法获取integer的对象(jdk5以前)Integer i3 = new Integer(123);Integer i4 = new Integer("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);//trueInteger 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(i10 == i11);Integer i12 = new Integer(128);Integer i13 = new Integer(128);System.out.println(i12 == i13);*/}
}

在以前包装类如何进行计算

public class demon1 {public static void main(String[] args) {//在以前包装类如何进行计算Integer i1 = new Integer(1);Integer i2 = new Integer(2);//需求:要把两个数据进行相加得到结果3//对象之间是不能直接进行计算的。//步骤://1.把对象进行拆箱,变成基本数据类型//2.相加//3.把得到的结果再次进行装箱(再变回包装类)int result = i1.intValue() + i2.intValue();Integer i3 = new Integer(result);System.out.println(i3);}
}

package com.itheima.a03integerdemo;public class A03_IntegerDemo3 {public static void main(String[] args) {//在JDK5的时候提出了一个机制:自动装箱和自动拆箱//自动装箱:把基本数据类型会自动的变成其对应的包装类//自动拆箱:把包装类自动的变成其对象的基本数据类型//在底层,此时还会去自动调用静态方法valueof得到一个Integer对象,只不过这个动作不需要我们自己去操作了//自动装箱的动作//Integer i1 = 10;//Integer i2 = new Integer(10);//自动拆箱的动作//int i = i2;//结论:在JDK5以后,int和Integer可以看做是同一个东西,因为在内部可以自动转化。}
}

总结:

  • 1.什么是包装类?

    基本数据类型对应的对象

  • 2.JDK5以后对包装类新增了什么特性?

    自动装箱、自动拆箱

  • 3.我们以后如何获取包装类对象?

    不需要new,不需要调用方法,直接赋值即可

Integer i = 10;Integer i1 = 10;
Integer i2 = 10;
Integer i3 = i1 + i2;

Integer 成员方法

使用字符串进行进制转换

public class demon1 {public static void main(String[] args) {/*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);//2、把整数转成八进制String str2 = Integer.toOctalString(100);System.out.println(str2);//3、把整数转化成十六进制String str3 = Integer.toHexString(100);System.out.println(str3);//4.将字符串类型的整数转成int类型的整数//强类型语言:每种数据在java中都有各自的数据类型//在计算的时候,如果不是同一种数据类型,是无法直接计算的。int i = Integer.parseInt("123");System.out.println(i);System.out.println(i + 123);//246//细节1://在类型转换的时候,括号中的参数只能是数字不能是其他,否则代码会报错//细节2://8种包装类当中,除了Character都有对应的parseXxx的方法,进行类型转换String str = "true";boolean b = Boolean.parseBoolean(str);System.out.println(b);}
}

键盘录入,使用nextLine

import java.util.Scanner;public class demon1 {public static void main(String[] args) {//键盘录入Scanner sc = new Scanner(System.in);System.out.println("请输入一个字符串");/* String str = sc.next();System.out.println(str);*///弊端://当我们在使用next,nextInt,nextDouble在接收数据的时候,遇到空格,回车,制表符的时候就停止了//键盘录入的是123 123 那么此时只能接收到空格前面的数据//我想要的是接收一整行数据//约定://以后我们如果想要键盘录入,不管什么类型,统一使用nextLine//特点:遇到回车才停止String line = sc.nextLine();System.out.println(line);double v = Double.parseDouble(line);System.out.println(v);}
}

http://www.lryc.cn/news/329272.html

相关文章:

  • Android Q(10)黑暗模式适配的实现
  • 【git】git使用手册
  • unity中判断方向 用 KeyVertical ,KeyHorizontal 判断ui物体的 方向
  • 前端a4纸尺寸转像素尺寸
  • Android 中 调试和减少内存错误
  • 证券市场概述
  • 什么是数据结构
  • 基于springboot+vue实现的学校田径运动会管理系统
  • HarmonyOS 应用开发之FA模型绑定Stage模型ServiceExtensionAbility
  • Java 中的单例模式
  • 鸿蒙OS开发实例:【ArkTS类库多线程I/O密集型任务开发】
  • OpenStack部署
  • Java中的多线程和线程安全问题
  • java Web会议信息管理系统 用eclipse定制开发mysql数据库BS模式java编程jdbc
  • lock4j学习记录
  • 【C++庖丁解牛】自平衡二叉搜索树--AVL树
  • ES5和ES6的深拷贝问题
  • 阿里云发送短信配置
  • axios封装,请求取消和重试,请求头公共参数传递
  • 隐私计算实训营学习五:隐语PSI介绍及开发指南
  • ES的RestClient相关操作
  • linux通用命令 ssh命令连接慢问题排查
  • 7.卷积神经网络与计算机视觉
  • Linux|如何管理多个Git身份
  • 力扣---最长回文子串---二维动态规划
  • (一)kafka实战——kafka源码编译启动
  • Spring Boot 使用 Redis
  • 火车头通过关键词采集文章的原理
  • Kafka 面试题及参考答案
  • 【Qt 学习笔记】Day1 | Qt 背景介绍