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对象
第二种方法:s=String.valueOf(i); //直接使用String类的静态方法,只产生一个对象

第一种方法:i=Integer.parseInt(s);//直接使用静态方法,不会产生多余的对象,但会抛出异常
第二种方法:i=Integer.valueOf(s).intValue();//Integer.valueOf(s) 相当于 new Integer(Integer.parseInt(s)),也会抛异常,但会多产生一个对象

--------------------------------------------------------------------
1如何将字串 String 转换成整数 int?

A. 有两个方法:

1). int i = Integer.parseInt([String]); 或
i = Integer.parseInt([String],[int radix]);

2). int i = Integer.valueOf(my_str).intValue();

注: 字串转成 Double, Float, Long 的方法大同小异.

2 如何将整数 int 转换成字串 String ?
A. 有叁种方法:

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中常用数据类型转换函数
虽然都能在JAVA API中找到,整理一下做个备份。

Java中int与String间的类型转换的更多相关文章

  1. JAVA中int与String类型的相互转换

    Java的int和String类型间互相转换,小功能但是经常用到,下面是几种实现的方法: 字符串类型String转换成整数int 1. int i = Integer.parseInt([String ...

  2. JAVA中int、String的类型转换

    int -> String int i=12345;String s="";第一种方法:s=i+"";第二种方法:s=String.valueOf(i); ...

  3. Java中int和String互相转换的多种方法

    1 如何将字串 String 转换成整数 int? A. 有两个方法: 1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt([ ...

  4. STL_string.【转】C++中int、string等常见类型转换

    ZC:#include <sstream> ZC:貌似还有 istringstream 和 ostringstream ... https://www.cnblogs.com/gaobw/ ...

  5. C++中int、string等常见类型转换

    1.int型与string型的互相转换 最佳实践: int型转string型 void int2str(const int &int_temp,string &string_temp) ...

  6. JAVA中int转string及String.valueOf()的使用

    日常java开放中,经常会遇到int和String的互转,一般图省事的做法就是: String length = ""+100; length的生成需要使用两个临时字符串" ...

  7. JAVA中int转String类型有三种方法

    String.valueOf(i) Integer.toString(i) i+"" i+""也就是一个int型的常量.+上个空的字符串,这里牵涉到了strin ...

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

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

  9. java中int转String 固定位数 不足补零

    转载自:http://ych0108.iteye.com/blog/2174134 String.format("%010d", 25); //25为int型 0代表前面要补的字符 ...

随机推荐

  1. BAPI_MATERIAL_SAVEDATA

    品目マスタ登録変更BAPI 概要 品目マスタを登録または変更するBAPIです.品目マスタのビューの拡張も行うことができます. BAPIでは品目マスタ登録画面(MM01)のような入力チェックがかからない ...

  2. 20155220 2016-2017-2 《java程序设计》第四周总结

    教材学习内容总结 第六章 继承与多态 继承 继承的基本原则是: 子类继承父类的所有成员变量(包括静态成员): 子类继承除父类构造方法外的所有成员方法(包括静态方法): 子类不能继承父类的构造方法,但在 ...

  3. 20155307 实验一《Java开发环境的熟悉》实验报告

    (一)命令行下Java程序开发 题目:实现Fibonacci数列功能,并进行测试 命令行下的程序截图 上传到了码云: 遇到的问题? 可以直接使用Javac,不加环境变量也行,但是文件的名字与类名必须一 ...

  4. LOJ #2585. 「APIO2018」新家

    #2585. 「APIO2018」新家 https://loj.ac/problem/2585 分析: 线段树+二分. 首先看怎样数颜色,正常的时候,离线扫一遍右端点,每次只记录最右边的点,然后查询左 ...

  5. macOS中启动Tomcat提示Cannot find ./catalina.sh

    首先查看Tomcat目录下是否存在catalina.sh,如果文件不存在,文件丢失,最好的方式是重装Tomcat Tomcat官网:http://tomcat.apache.org/ 如果文件存在,那 ...

  6. Appium+python的单元测试框架unittest(3)——discover(转)

    (原文:https://www.cnblogs.com/fancy0158/p/10047906.html) TestSuite套件可以添加很多个用例后运行,但是每个用例都需要调用addTest()函 ...

  7. 422. Length of Last Word【LintCode java】

    Description Given a string s consists of upper/lower-case alphabets and empty space characters ' ', ...

  8. centos 6.5 双网卡 上网 virtualbox nat hostonly

    虚拟机两张网卡:分别调成NAT(eth0)和host only(eht1)模式. nat的网卡不用设置,host only网卡调为(vi /etc/sysconfig/network-scripts/ ...

  9. Codeforces Round #553 (Div. 2) C

    C. Problem for Nazar time limit per test 1 second memory limit per test 256 megabytes input standard ...

  10. ICPC 沈阳 Problem C

    题意 求n的全排列中将前k个数排序后最长公共子序列>=n-1的个数 思考 我们先把最后可能产生的结果找出来,再找有多少种排列能构成这些结果 设排列为s S like 1,2,3,...,n , ...