Java中int与String间的类型转换
int -> String
int i=12345;
String s="";
除了直接调用i.toString();还有以下两种方法
第一种方法:s=i+"";
第二种方法:s=String.valueOf(i);
这两种方法有什么区别呢?作用是不是一样的呢?是不是在任何下都能互换呢?
String -> int
s="12345";
int i;
第一种方法:i=Integer.parseInt(s);
第二种方法:i=Integer.valueOf(s).intValue();
这两种方法有什么区别呢?作用是不是一样的呢?是不是在任何下都能互换呢?
以下是答案:
|
第一种方法:s=i+""; //会产生两个String对象 第一种方法:i=Integer.parseInt(s);//直接使用静态方法,不会产生多余的对象,但会抛出异常 |
A. 有两个方法:
1). int i = Integer.parseInt([String]); 或
i = Integer.parseInt([String],[int radix]);
2). int i = Integer.valueOf(my_str).intValue();
注: 字串转成 Double, Float, Long 的方法大同小异.
1.) String s = String.valueOf(i);
2.) String s = Integer.toString(i);
3.) String s = "" + i;
注: Double, Float, Long 转成字串的方法大同小异.
JAVA数据类型转换
这是一个例子,说的是JAVA中数据数型的转换.供大家学习
package shenmixiaozhu;
import java.sql.Date;
public class TypeChange {
public TypeChange() {
}
//change the string type to the int type
public static int stringToInt(String intstr)
{
Integer integer;
integer = Integer.valueOf(intstr);
return integer.intValue();
}
//change int type to the string type
public static String intToString(int value)
{
Integer integer = new Integer(value);
return integer.toString();
}
//change the string type to the float type
public static float stringToFloat(String floatstr)
{
Float floatee;
floatee = Float.valueOf(floatstr);
return floatee.floatValue();
}
//change the float type to the string type
public static String floatToString(float value)
{
Float floatee = new Float(value);
return floatee.toString();
}
//change the string type to the sqlDate type
public static java.sql.Date stringToDate(String dateStr)
{
return java.sql.Date.valueOf(dateStr);
}
//change the sqlDate type to the string type
public static String dateToString(java.sql.Date datee)
{
return datee.toString();
}
public static void main(String[] args)
{
java.sql.Date day ;
day = TypeChange.stringToDate("2003-11-3");
String strday = TypeChange.dateToString(day);
System.out.println(strday);
}
虽然都能在JAVA API中找到,整理一下做个备份。
Java中int与String间的类型转换的更多相关文章
- JAVA中int与String类型的相互转换
Java的int和String类型间互相转换,小功能但是经常用到,下面是几种实现的方法: 字符串类型String转换成整数int 1. int i = Integer.parseInt([String ...
- JAVA中int、String的类型转换
int -> String int i=12345;String s="";第一种方法:s=i+"";第二种方法:s=String.valueOf(i); ...
- Java中int和String互相转换的多种方法
1 如何将字串 String 转换成整数 int? A. 有两个方法: 1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt([ ...
- STL_string.【转】C++中int、string等常见类型转换
ZC:#include <sstream> ZC:貌似还有 istringstream 和 ostringstream ... https://www.cnblogs.com/gaobw/ ...
- C++中int、string等常见类型转换
1.int型与string型的互相转换 最佳实践: int型转string型 void int2str(const int &int_temp,string &string_temp) ...
- JAVA中int转string及String.valueOf()的使用
日常java开放中,经常会遇到int和String的互转,一般图省事的做法就是: String length = ""+100; length的生成需要使用两个临时字符串" ...
- JAVA中int转String类型有三种方法
String.valueOf(i) Integer.toString(i) i+"" i+""也就是一个int型的常量.+上个空的字符串,这里牵涉到了strin ...
- java中int和String之间的转换
String 转为int int i = Integer.parseInt([String]); int i = Integer.valueOf(my_str).intValue(); int转为St ...
- java中int转String 固定位数 不足补零
转载自:http://ych0108.iteye.com/blog/2174134 String.format("%010d", 25); //25为int型 0代表前面要补的字符 ...
随机推荐
- windows系统下系统变量path误删恢复方法
每台计算机安装程序不同,环境变量path会有不同,若误删了环境变量path,可以如下完美解决. Win+R 输入regedit打开注册表(开始-运行里输入regedit) 找到 HKEY_LOC ...
- 脱离matlab运行可执行程序的步骤
MCR是由matlab的运行环境,占用不到600M的对于用不同matlab版本生成的exe文件,MCR版本也会有不同,因此,在程序打包时,最好将相应版本的MCR一起打包.MCR环境的设置文件存放目录如 ...
- 20155230 《Java程序设计》实验五 Java网络编程及安全
20155230 <Java程序设计>实验五 Java网络编程及安全 实验内容 1.掌握Socket程序的编写: 2.掌握密码技术的使用: 3.设计安全传输系统. 实验1 两人一组结对编程 ...
- C语言学习笔记1
C语言假期学习笔记1 关于吃回车问题 第一个解决方案是使用ch=getchar(); 第二个方案是在scanf(" %c",&b):在%c加空格,将存于缓冲区的回车符读入 ...
- [SDOI2010]地精部落 DP
LG传送门 DP好题 题意很简单,就是求1-n的排列,满足一个数两边的数要么都比它大要么都比它小,求这样的排列个数对\(p\)取膜的值(为了表述简单,我们称这样的排列为波动序列). 这个题我第一眼看到 ...
- dsp6657的helloworld例程测试-第一篇
环境搭建可以参考http://blog.sina.com.cn/s/blog_ed2e19900102xi2j.html 1. 先从mcsdk导入工程,helloworld例程 2. 提示有错误,估计 ...
- linux 冒号用法
冒号在Linux中是一个空命令,可以认为与shell的内建命令true相同,它的返回值是0. 在while循环中 while : 与 while true 的作用是等效的 在 if/then 中可作为 ...
- Electron小记
一.安装 1.安装NodeJS 2.安装electronjs:npm install -g electron --unsafe-perm=true --allow-root 安装完,环境为: Node ...
- 使用Photon引擎进行unity网络游戏开发(二)——Photon常用类介绍
使用Photon引擎进行unity网络游戏开发(二)——Photon常用类介绍 Photon PUN Unity 网络游戏开发 Photon常用类介绍: IPunCallback PUNGIPunCa ...
- mysql 5.5 zip配置安装
1.解压2.创建option文件 --defaults-file=../my.ini [mysql] # 设置mysql客户端默认字符集 default-character-set=utf8 [mys ...