栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

辉仔日记之学代码第九期——基本数据的包装类

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

辉仔日记之学代码第九期——基本数据的包装类

目录

前言:

一、类型对应的包装类

二、Integer类

三、Integer和String的相互转换 

   1.int转换成String

                方法一:直接在数值后面加一个空字符串

                 方法二:通过String类静态方法valueOf()

2、String转换成int

                方法一:先将字符串数字转成Integer,再调用valueOf()方法

                方法二:通过Integer静态方法parseInt()进行转换

四、字符串排列 


前言:

为什么基本数据要有个包装类呢?辉仔个人觉得啊,单单实现一个基本数据的作用,未必功能也太少了吧,当我像变成其他数据类型呢?肯定有个方法更加的简便易操作!

一、类型对应的包装类

这里面除了int和char类型的包装类是他们的英文全拼,其他都是首字母大写!

除了Character和Boolean不是“数字型”,其他都是java.lang.Number的子类

基本数据类型包装类
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

二、Integer类

         这里我们着重复习Integer这个类,因为我们经常都会使用到它,它是一个包装在对象中原始类型的int值。

方法名说明
public static Integer valueOf(int i)返回表示指定的 int 值的 Integer 实例
public static Integer valueOf(String s)返回一个保存指定值的 Integer 对象 String

       代码示例:

        //public static Integer valueOf(int i):返回表示指定的 int 值的 Integer 实例
        Integer i3 = Integer.valueOf(100);
        System.out.println(i3);

        //public static Integer valueOf(String s):返回一个保存指定值的Integer对象 String
        Integer i4 = Integer.valueOf("100");
        System.out.println(i4);

三、Integer和String的相互转换 

一般有两种转换方式

   1.int转换成String

                方法一:直接在数值后面加一个空字符串

代码示例:

  //int --- String
        int number = 100;
        //方式1
        String s1 = number + "";
        System.out.println(s1);

                 方法二:通过String类静态方法valueOf()

代码示例:

 //方式2

         int number = 100;
        //public static String valueOf(int i)

        String s2 = String.valueOf(number);
        System.out.println(s2);

 

2、String转换成int

                方法一:先将字符串数字转成Integer,再调用valueOf()方法

代码示例:

//String --- int
        String s = "100";
        //方式1:String --- Integer --- int
        Integer i = Integer.valueOf(s);
        //public int intValue()
        int x = i.intValue();
        System.out.println(x);

                方法二:通过Integer静态方法parseInt()进行转换

代码示例:

 //方式2
         String s = "100";
        //public static int parseInt(String s)
        int y = Integer.parseInt(s);
        System.out.println(y);

四、字符串排列 

代码示例:

public class IntegerTest {
    public static void main(String[] args) {
        //定义一个字符串
        String s = "91 27 46 38 50";

        //把字符串中的数字数据存储到一个int类型的数组中
        String[] strArray = s.split(" ");
//        for(int i=0; i 

       

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/306394.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号