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[] ...
随机推荐
- springboot+redis分布式锁-模拟抢单
本篇内容主要讲解的是redis分布式锁,这个在各大厂面试几乎都是必备的,下面结合模拟抢单的场景来使用她:本篇不涉及到的redis环境搭建,快速搭建个人测试环境,这里建议使用docker:本篇内容节点如 ...
- java基础系列之ConcurrentHashMap源码分析(基于jdk1.8)
1.前提 在阅读这篇博客之前,希望你对HashMap已经是有所理解的,否则可以参考这篇博客: jdk1.8源码分析-hashMap:另外你对java的cas操作也是有一定了解的,因为在这个类中大量使用 ...
- 前端笔记之Vue(一)初识SPA和Vue&webpack配置和vue安装&指令
一.单页面应用(SPA) 1.1 C/S到B/S页面架构的转变 C/S:客户端/服务器(Client/Server)架构的软件. C/S 软件的特点: ① 从window桌面双击打开 ② 更新的时候会 ...
- 【转载】Docker+Kubernetes 干货文章精选
主要涉及到以下关键字: K8S.Docker.微服务.安装.教程.网络.日志.存储.安全.工具.CI/CD.分布式.实践.架构等: 以下盘点2018年一些精选优质文章! 漫画形式: 漫画:小黄人学 S ...
- Kubernetes集群部署关键知识总结
Kubernetes集群部署需要安装的组件东西很多,过程复杂,对服务器环境要求很苛刻,最好是能连外网的环境下安装,有些组件还需要连google服务器下载,这一点一般很难满足,因此最好是能提前下载好准备 ...
- 为什么需要Docker?
前言 只有光头才能变强. 文本已收录至我的GitHub仓库,欢迎Star:https://github.com/ZhongFuCheng3y/3y 估计大家也可能听过Docker这项技术(在论坛上.招 ...
- 【Android】OkHttp3总结与封装
开始使用 在app目录下的build.gradle中添加依赖: implementation 'com.squareup.okhttp3:okhttp:3.13.1' implementation ' ...
- Windows 10-限制Windows更新上传带宽
Windows Update Delivery Optimization可帮助您更快,更可靠地获取Windows更新和Microsoft Store应用程序. Windows Update Deliv ...
- PowerShell禁止执行脚本解决方法
无法加载文件 C:\***.p s1,因为在此系统中禁止执行脚本.有关详细信息,请参阅 "get-help about_signing". 所在位置 行:1 字符: 18 + .\ ...
- July 05th. 2018, Week 27th. Thursday
Pleasure in the job puts perfection in the work. 乐于工作才能有完美表现. From Aristole. Do you want promotion? ...