1.int和Integer之间的转换:

  1) int----->Integer

①自动装箱

②Integer的构造方法

③调用Integer的静态方法:static Integer valueOf(int i):返回一个指定int值的Integer对象

代码如下:

int a = 10;
                          Integer i1 = a; //①
                          Integer i2 = new Integer(a);  //②
                          Integer i3 = Integer.valueOf(a);   //③

2) Integer------>int            

              ①自动拆箱

②调用Integer的方法:int intValue():以int类型返回该Integer的值

示例代码:

Integer a = new Integer(10);

int i1 = a;   //①
                        int i2 = a.intValue();   //②

2.String和Integer之间的转换:

    1) Integer---->String

            ①调用Integer的方法:String toString():返回该Integer对象的字符串形式

②调用Integer的静态方法:static String toString(int i):返回一个指定整数的String对象

③调用String的静态方法:static String valueOf(Object obj):返回任意类型的字符串形式

示例代码:

Integer a = new Integer(20);

String str1 = a.toString();   //①

String str2 = Integer.toString(a);  //②

String str3 = String.valueOf(a);   //③

    2) String---->Integer

            ①调用Integer的静态方法:static Integer valueOf(String s):返回指定的 String 的值的 Integer 对象。

                 注意:这里的参数s必须是可以解析成整数的字符串,否则会报异常:NumberFormatException

示例代码:

String str = "123";
                        Integer i = Integer.valueOf(str);  //①

3.int和String之间的转换:

    1) int------>String

①字符串拼接,使用+

②调用Integer的静态方法:static String toString(int i):返回一个指定整数的String对象

③调用String的静态方法:static String valueOf(int i):返回指定int值的字符串形式

示例代码:

int a = 5;
                       String s1 = a +""; //①
                       String s3 = Integer.toString(a);  //②
                       String s2 = String.valueOf(a);   //③

    2) String----->int

        ①调用Integer的静态方法:static int parseInt(String s):将一个可以解析为整数的字符串解析为一个int值

          ②调用Integer的静态方法:static Integer valueOf(String s):返回指定的 String 的值的 Integer 对象。【自动拆箱】

示例代码:

String str = "123";
                     int m1 = Integer.parseInt(str);  //①
                     int m2 = Integer.valueOf(str);   //②--->自动拆箱
                     int m3 = Integer.valueOf(str).intValue();  //②--->手动拆箱

          

int-Integer-String之间的转换方式的更多相关文章

  1. java Int 和 String 之间的转换

    String 转换成 int Integer.parseInt(formParams.get("id")) int 转换成 string Integer.toString(id);

  2. java中int和String之间的转换

    String 转为int int i = Integer.parseInt([String]); int i = Integer.valueOf(my_str).intValue(); int转为St ...

  3. int和string之间的转换

    #include<cstring> #include<algorithm> #include<stdio.h> #include<iostream> # ...

  4. int integer string间的转换

    1.int-->Integer new Integer(i); 2.Integer-->int Integer i = new Integer(1); int k = i.intValue ...

  5. java中Integer 和String 之间的转换

    java中Integer 和String 之间的转换 将数组转换成字符串:char[] array = {'a','b','c','d','e'};String str = new String(ar ...

  6. 基本数据类型、包装类、String之间的转换

    package 包装类; /** *8种基本数据类型对应一个类,此类即为包装类 * 基本数据类型.包装类.String之间的转换 * 1.基本数据类型转成包装类(装箱): * ->通过构造器 : ...

  7. 如何在Byte[]和String之间进行转换

    源自C#与.NET程序员面试宝典. 如何在Byte[]和String之间进行转换? 比特(b):比特只有0 1,1代表有脉冲,0代表无脉冲.它是计算机物理内存保存的最基本单元. 字节(B):8个比特, ...

  8. java字符数组char[]和字符串String之间的转换

    java字符数组char[]和字符串String之间的转换 觉得有用的话,欢迎一起讨论相互学习~Follow Me 使用String.valueOf()将字符数组转换成字符串 void (){ cha ...

  9. c# String ,String[] 和 List<String>之间的转换

    C#对字符串进行处理时,经常需要进行String,String[]和List<String>之间的转换 本文分析一下它们的差异和转换 一. 1. String > String[] ...

随机推荐

  1. Mybatis:缓存,动态SQL,注解SQL以及动态标签使用

    1 转义字符 字符 转义 描述 < < 小于 <= <= 小于等于 > > 大于 >= >= 大于等于 <> <> 不等于 &a ...

  2. [翻译 EF Core in Action 1.11] 何时不应该使用EF Core

    Entity Framework Core in Action Entityframework Core in action是 Jon P smith 所著的关于Entityframework Cor ...

  3. 用C语言做一个横板过关类型的控制台游戏

    前言:本教程是写给刚学会C语言基本语法不久的新生们. 因为在学习C语言途中,往往只能写控制台代码,而还能没接触到图形,也就基本碰不到游戏开发. 所以本教程希望可以给仍在学习C语言的新生们能提前感受到游 ...

  4. 从On-Premise本地到On-Cloud云上运维的演进

    摘要: 从用户的声音中,我们听到用户对稳定.弹性.透明的诉求,我们也在不断升级ECS的运维能力和体验,助力用户建立主动运维体系,赋能业务永续运行.为了让大家更好的了解和用好ECS弹性计算服务,从本期开 ...

  5. Fundebug支付宝小程序BUG监控插件更新至0.2.0,新增test()方法,报错增加Page数据

    摘要: 0.2.0新增fundebug.test()方法,同时报错增加了Page数据. Fundebug提供专业支付宝小程序BUG监控服务,可以第一时间为您捕获生存环境中小程序的异常.错误或者BUG, ...

  6. vue表格实现固定表头首列

    前言 最近在做vue移动端项目,需要做一个可以固定表头首列的表格,而且由于一些原因不能使用任何UI插件,网上找了很久也没什么好方法,所以在解决了问题之后,写下了这篇文章供后来人参考,文章有什么错漏的问 ...

  7. 全球排名第一的开源ERP Odoo v12 最新一键安装体验版正式发布

    引言 Odoo 12.0是目前全球Odoo社区最新推出的产品版本代号,该产品具有划时代的意义,增加了如互联网级的知识库网盘功能.工业互联网的IOT设备矩阵管控功能,全新的Python Sass前端引擎 ...

  8. Linux基本操作——文件相关

    一.前言 无论是IC工程师.FPGA工程师还是嵌入式软件工程师,都或多或少会接触到Linux操作系统.有很多EDA工具只有Linux版本,因此掌握基本的操作和常用命令十分必要.Linux中的数据均以文 ...

  9. IIS web搭建之虚拟主机

    IIS web搭建之虚拟主机 虚拟目录:能将一个网站的文件分散存储在同一个计算机的不同目录和其他计算机. 使用虚拟目录的好处: 1.将数据分散保存到不同的磁盘或者计算机上,便于分别开发和维护. 2.当 ...

  10. pyspider 文档介绍

    一 代码区结构 def on_start(self)是脚本的入口点.单击run仪表板上的按钮时将调用它. self.crawl(url, callback=self.index_page)*是这里最重 ...