int-Integer-String之间的转换方式
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之间的转换方式的更多相关文章
- java Int 和 String 之间的转换
String 转换成 int Integer.parseInt(formParams.get("id")) int 转换成 string Integer.toString(id);
- java中int和String之间的转换
String 转为int int i = Integer.parseInt([String]); int i = Integer.valueOf(my_str).intValue(); int转为St ...
- int和string之间的转换
#include<cstring> #include<algorithm> #include<stdio.h> #include<iostream> # ...
- int integer string间的转换
1.int-->Integer new Integer(i); 2.Integer-->int Integer i = new Integer(1); int k = i.intValue ...
- java中Integer 和String 之间的转换
java中Integer 和String 之间的转换 将数组转换成字符串:char[] array = {'a','b','c','d','e'};String str = new String(ar ...
- 基本数据类型、包装类、String之间的转换
package 包装类; /** *8种基本数据类型对应一个类,此类即为包装类 * 基本数据类型.包装类.String之间的转换 * 1.基本数据类型转成包装类(装箱): * ->通过构造器 : ...
- 如何在Byte[]和String之间进行转换
源自C#与.NET程序员面试宝典. 如何在Byte[]和String之间进行转换? 比特(b):比特只有0 1,1代表有脉冲,0代表无脉冲.它是计算机物理内存保存的最基本单元. 字节(B):8个比特, ...
- java字符数组char[]和字符串String之间的转换
java字符数组char[]和字符串String之间的转换 觉得有用的话,欢迎一起讨论相互学习~Follow Me 使用String.valueOf()将字符数组转换成字符串 void (){ cha ...
- c# String ,String[] 和 List<String>之间的转换
C#对字符串进行处理时,经常需要进行String,String[]和List<String>之间的转换 本文分析一下它们的差异和转换 一. 1. String > String[] ...
随机推荐
- HashMap 实现及原理
1.为什么用HashMap? HashMap是一个散列桶(数组和链表),它存储的内容是键值对(key-value)映射HashMap采用了数组和链表的数据结构,能在查询和修改方便继承了数组的线性查找和 ...
- Spark2.4.0伪分布式环境搭建
一.搭建环境的前提条件 环境:ubuntu-16.04 hadoop-2.6.0 jdk1.8.0_161. spark-2.4.0-bin-hadoop2.6.这里的环境不一定需要和我一样,基本版 ...
- ASP.NET Core实现 随处可见的基本身份认证
概览 在HTTP中,基本认证(Basic access authentication,简称BA认证)是一种用来允许网页浏览器或其他客户端程序在请求资源时提供用户名和口令形式的身份凭证的一种登录验证方式 ...
- TiDB之mac上搭建及调试技巧
此文目的 由于本人最近已经成为TiDB的粉丝,所以就开始各种研究TiDB的源码,研究源码这个事情,首先就需要在自己电脑上不断的调试及修改.TiDB本身的代码是非常容易编译和调试的,但是要把PD.TiK ...
- 强化学习(十三) 策略梯度(Policy Gradient)
在前面讲到的DQN系列强化学习算法中,我们主要对价值函数进行了近似表示,基于价值来学习.这种Value Based强化学习方法在很多领域都得到比较好的应用,但是Value Based强化学习方法也有很 ...
- Protocol Buffers(3):阅读一个二进制文件
目录 Proto文件 序列化 二进制文件解析 反序列化 参考 博客:blog.shinelee.me | 博客园 | CSDN 这篇文章中,我们将定义一个相对复杂的数据结构,直接分析其序列化后的二进制 ...
- [翻译 EF Core in Action 1.7] MyFirstEfCoreApp访问的数据库
Entity Framework Core in Action Entityframework Core in action是 Jon P smith 所著的关于Entityframework Cor ...
- HTTP2和HTTPS来不来了解一下?
一.前言 只有光头才能变强 HTTP博文回顾: PC端:HTTP就是这么简单 PC端:HTTP面试题都在这里 微信公众号端:HTTP就是这么简单 微信公众号端:HTTP面试题都在这里 本文力求简单讲清 ...
- JS点击图片更改照片
<img src="../../img/20190224185111.png" alt="" id="zhaopian"/> - ...
- 软件开发架构介绍||OSI七层协议之物理层、数据链路层、网络层、传输层(mac地址、ip协议、断开协议、tcp协议之三次握手四次挥手)
一.网络编程 软件开发架构 C/S架构 C:客户端 想体验服务的时候才会去找服务端体验服务 S:服务端 24小时不间断的提供服务,即时监听,随时待命 B/S架构 B:浏览器 想体验服务的时候 ...