---
title: "java中int和string的相互转换"
author: "sdylag"
date: "2011-02-27T18:15:27Z"
tags: ["Java"]
categories: ["编程语言"]
slug: "java-int-string"
draft: false
lastmod: "2011-02-27T18:17:01Z"
id: 932590
url: "https://sdylag.iteye.com/blog/932590"
---

**int -&gt; String**

 

int i=12345;  
String s="";

第一种方法：s=i+""; //会产生**两个String对象 效率低**

第二种方法：s=String.valueOf(i); //直接使用String类的静态方法，只产生一个对象  效率中等

 

第三种方法：s=Integer.toString(i); //也是调用的静态方法，不是的是 此方法**效率高**

\--------------------------------------------------------

 

**String -&gt; int**

s="12345";  
int i;

第一种方法：i=Integer.parseInt(s); //直接使用静态方法，不会产生多余的对象，但会抛出异常

第二种方法：i=Integer.valueOf(s).intValue();

//Integer.valueOf(s) 相当于 new Integer(Integer.parseInt(s))，也会抛异常，但会多产生一个对象

 

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