在java中,大家肯定都会遇到int类型转String类型的情形,知其然知其所以然。总结加分析一下,int类型转String类型有下面几种方式:  

  1. a+”“
  2. String.valueOf(a)
  3. Integer.toString(a)

  以上三种方法在实际使用过程中都是没有问题的,可是效率上还是有些许区别的,所以写个小程序来对照一下他们的效率:

int a = 123456789;
long start = System.currentTimeMillis();
for (int i=0; i<100000; i++){
String m = a+"";
}
long end = System.currentTimeMillis();
Log.e("time", "a+\"\" = " + (end - start)); start = System.currentTimeMillis();
for (int i=0; i<100000; i++){
String n = String.valueOf(a);
}
end = System.currentTimeMillis();
Log.e("time", "String.valueOf(a) = " +(end-start)); start = System.currentTimeMillis();
for (int i=0; i<100000; i++){
String n = Integer.toString(a);
}
end = System.currentTimeMillis();
Log.e("time", "Integer.toString(a) = " +(end-start));

最后打印出来的运行时间:

E/time: a+"" = 257
E/time: String.valueOf(a) = 140
E/time: Integer.toString(a) = 159

能够看到在效率上除了a+”“这样的方式之外,其它两种方式的效率差点儿相同。为什么呢?看源代码!

String.valueOf(a) && Integer.toString(a)

  先看看后两种方式的源代码:

String.valueOf(a)->Integer.toString(a)->IntegralToString.intToString(a)->convertInt(null, a)

Integer.toString(a)->IntegralToString.intToString(a)->convertInt(null, a)

能够看到String.valueOf是通过调用Integer.toString实现的,也难怪他们的效率如此接近。

他们最后都会调用到convertInt函数中:

private static String convertInt(AbstractStringBuilder sb, int i) {
boolean negative = false;
String quickResult = null;
if (i < 0) {
negative = true;
i = -i;
if (i < 100) {
if (i < 0) {
// If -n is still negative, n is Integer.MIN_VALUE
quickResult = "-2147483648";
} else {
quickResult = SMALL_NEGATIVE_VALUES[i];
if (quickResult == null) {
SMALL_NEGATIVE_VALUES[i] = quickResult =
i < 10 ? stringOf('-', ONES[i]) : stringOf('-', TENS[i], ONES[i]);
}
}
}
} else {
if (i < 100) {
quickResult = SMALL_NONNEGATIVE_VALUES[i];
if (quickResult == null) {
SMALL_NONNEGATIVE_VALUES[i] = quickResult =
i < 10 ? stringOf(ONES[i]) : stringOf(TENS[i], ONES[i]);
}
}
}
if (quickResult != null) {
if (sb != null) {
sb.append0(quickResult);
return null;
}
return quickResult;
} int bufLen = 11; // Max number of chars in result
char[] buf = (sb != null) ? BUFFER.get() : new char[bufLen];
int cursor = bufLen; // Calculate digits two-at-a-time till remaining digits fit in 16 bits
while (i >= (1 << 16)) {
// Compute q = n/100 and r = n % 100 as per "Hacker's Delight" 10-8
int q = (int) ((0x51EB851FL * i) >>> 37);
int r = i - 100*q;
buf[--cursor] = ONES[r];
buf[--cursor] = TENS[r];
i = q;
} // Calculate remaining digits one-at-a-time for performance
while (i != 0) {
// Compute q = n/10 and r = n % 10 as per "Hacker's Delight" 10-8
int q = (0xCCCD * i) >>> 19;
int r = i - 10*q;
buf[--cursor] = DIGITS[r];
i = q;
} if (negative) {
buf[--cursor] = '-';
} if (sb != null) {
sb.append0(buf, cursor, bufLen - cursor);
return null;
} else {
return new String(cursor, bufLen - cursor, buf);
}
}

分析一下,这个函数的工作主要能够分为这几步:

  1. 假设a为负数。将a变成正数,假设a还小于0,直接置为Integer.MIN_VALUE;假设a小于100。则直接使用TENS和ONES数组进行高速计算得出结果。加上’-‘号。直接返回该结果。

  2. 假设a为正数而且小于100。直接使用TENS和ONES数组进行高速计算得出结果返回。

  3. 假设上面两步没有处理完,说明a是大于100的数字,无法直接使用TENS和ONES数组进行高速计算。处理方式就是2位为一步循环处理,每次将这两位使用TENS和ONES数组进行高速计算得出这两位的结果存在数组的对应位置。直到仅仅剩一位。最后剩下的一位使用DIGITS数组得出16进制的结果放在最后。返回结果。

  那么问题来了。当a>=100的时候,那两次while循环为什么会使用0x51EB851FL和0xCCCD这两个数字呢?这个问题不要问我,我也不知道,只是源代码作者凝视写的非常明确了:

// Compute q = n/100 and r = n % 100 as per "Hacker's Delight" 10-8

// Compute q = n/10 and r = n % 10 as per "Hacker's Delight" 10-8

去看《Hacker’s Delight》的10-8章。

  接着另一个问题是TENS和ONES数组,直接看代码。一目了然:

/** TENS[i] contains the tens digit of the number i, 0 <= i <= 99. */
private static final char[] TENS = {
'0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
'1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
'2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
'3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
'4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
'5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
'6', '6', '6', '6', '6', '6', '6', '6', '6', '6',
'7', '7', '7', '7', '7', '7', '7', '7', '7', '7',
'8', '8', '8', '8', '8', '8', '8', '8', '8', '8',
'9', '9', '9', '9', '9', '9', '9', '9', '9', '9'
}; /** Ones [i] contains the tens digit of the number i, 0 <= i <= 99. */
private static final char[] ONES = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
};

每一个数组都是100的长度,都是用来处理0~99这100个数字,个位和十位的处理方式也非常清楚。

  从代码角度来看,这个算法在数字小于100的和大于100的处理方式是不一样的,小于100的高速计算法运行时间会远远短于大于100的方式。验证一下。将a变量改动为10:

E/time: i+"" = 199
E/time: String.valueOf() = 7
E/time: Integer.toString() = 6

确实短了非常多。!!

a+”“

  再来看看a+”“的方式,我承认这样的方式我用的最多了,由于太简单了,java源代码对’+’运算符进行了重载。源代码我找不到啊,只是从网上找一些资料:

The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method. String conversions are implemented through the method toString, defined by Object and inherited by all classes in Java. For additional information on string concatenation and conversion, see Gosling, Joy, and Steele, The Java Language Specification.

地址:http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

能够看到,’+’运算符的主要方式是使用StringBuilder或者StringBuffer来实现的。相似于:

StringBuilder sb = new StringBuilder();
sb.append("");
sb.append(i);
String strI = sb.toString();

再来看看append的源代码:

StringBuffer.append->IntegralToString.appendInt(this, a)->convertInt(sb, i)

能够看到’+’运算符最后也是调用到了同一个函数。仅仅只是第一个參数的sb不为null而已。所以已经非常清楚了,’+’运算符的运行效率不高的原因应该就在之前的new StringBuilder等操作和之后的StringBuilder.toString等操作,反编译class文件也能够得出一样的结论:

http://stackoverflow.com/a/4105406

  所以a+”“的方式以后就少用一点了,效率不高,也显得不太专业。

扩展

  String 扩展的相关知识:

常量池的内存分配在 JDK6、7、8中有不同的实现:

1. JDK6及之前版本号中,常量池的内存在永久代PermGen进行分配。所以常量池会受到PermGen内存大小的限制。

2. JDK7中,常量池的内存在Java堆上进行分配。意味着常量池不受固定大小的限制了。

3. JDK8中,虚拟机团队移除了永久代PermGen。

关于永久代移除:http://www.infoq.com/cn/articles/Java-PERMGEN-Removed

样例1:

public class StringTest {
public static void main(String[] args) {
String a = "java";
String b = "java";
String c = "ja" + "va";
}
}

变量 a、b 和 c 都指向常量池的 “java” 字符串,表达式 “ja” + “va” 在编译期间会把结果值”java”直接赋值给c。所以终于的结果 a==c 为 true。

样例2:

public class StringTest {
public static void main(String[] args) {
String a = "hello ";
String b = "world";
String c = a + b;
String d = "hello world";
}
}

我们依据上面知道在 java 中 “+” 运算符实际上是使用 StringBuilder.append 去实现的。所以此时会在 Java 堆上新建一个 String 对象,这个 String 对象终于指向常量池的 “hello world”。所以说此时 c==d 为 false。

只是有种特殊情况。当final修饰的变量发生连接动作时,编译器会进行优化,将表达式结果直接赋值给目标变量:

public class StringTest {
public static void main(String[] args) {
final String a = "hello ";
final String b = "world";
String c = a + b;
String d = "hello world";
}
}

所以此时 c==d 为 true。

引用

http://www.importnew.com/21711.html

http://www.importnew.com/21720.html

java int转String全部方式的效率对照与深入解析的更多相关文章

  1. java int and string convert

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

  2. java中多种写文件方式的效率对比实验

    一.实验背景 最近在考虑一个问题:“如果快速地向文件中写入数据”,java提供了多种文件写入的方式,效率上各有异同,基本上可以分为如下三大类:字节流输出.字符流输出.内存文件映射输出.前两种又可以分为 ...

  3. Java int与String互相转化大全

    int -> String //int 转化才 string int num = 123456; //方法一 会产生两个String对象 String s1 = num+"" ...

  4. Java -- int与String相互转换

    int转换为String 使用Integer的静态方法 Integer.toString(int num); 空值会抛出NullPointerException异常 使用String的静态方法 Str ...

  5. java int和String类型之间的相互转换

    String --> int 第一种方法:int i = Integer.parseInt(s); 第二种方法:int i = Integer.valueOf(s).intValue(); 两种 ...

  6. java Int 和 String 之间的转换

    String 转换成 int Integer.parseInt(formParams.get("id")) int 转换成 string Integer.toString(id);

  7. Java int转string 长度不足左补0

    最近项目中用到这个需求,我试了两个方法都可以实现 方法一:(推荐) String s=String.format("%010d", 123)//123为int类型,0代表前面要补的 ...

  8. Java int to String互转

    Integer.toString Integer.parseInt(lAyaNums);

  9. C#的StringBuilder 以及string字符串拼接的效率对照

    今天公司一个做Unity3d的人在说字符串拼接的一个效率问题,他觉得string拼接会产生新的一个内存空间,假设不及时回收会产生大量的碎片,特别是在Unity3d这样一个Updata环境下,由于每一帧 ...

随机推荐

  1. JavaScript中继承机制的模仿实现

    首先,我们用一个经典例子来简单阐述一下ECMAScript中的继承机制. 在几何学上,实质上几何形状只有两种,即椭圆形(是圆形的)和多边形(具有一定数量的边).圆是椭圆的一种,它只有一个焦点.三角形. ...

  2. Java面试题之类加载器有哪些?什么是双亲委派模型

    类加载器有哪些: 1.启动类加载器(Bootstrap ClassLoader):这个类加载器负责将存放在<JAVA_HOME>\lib目录中的,或被-Xbootclasspath参数所指 ...

  3. js字符串使用占位符拼接

    由于几个老项目中经常用到jquery拼接字符串,各种引号很disgusting 所以写了一个占位符拼接的的方法 String.prototype.signMix= function() { if(ar ...

  4. 去除TB二合一页面弹窗

    AdBlock插件 自定义AdBlock ###J_MMREDBOX_MASK 保存

  5. 写一个system.data.entity的simpledatarepo,实现crudq这些功能,不需要引入entityframework,直接可以使用,用到objectset

    note:you can delete reference of entityframework when using this classes.it`s just a simple repohelp ...

  6. mysql 取消命令行继续编辑

    mysql> create database mingongge defa\c#回车 置空mysql>  加一个\c  cancel 编辑命令 回车

  7. poj 1151(离散化+矩形面积并)

    题目链接:http://poj.org/problem?id=1151 关于离散化,这篇博客讲的很好:http://www.cppblog.com/MiYu/archive/2010/10/15/12 ...

  8. Educational Codeforces Round 33 (Rated for Div. 2) A. Chess For Three【模拟/逻辑推理】

    A. Chess For Three time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  9. Python与数据库[0] -> 数据库概述

    数据库概述 / Database Overview 1 关于SQL / About SQL 构化查询语言(Structured Query Language)简称SQL,是一种特殊目的的编程语言,是一 ...

  10. zzc种田

    题目背景 可能以后 zzc就去种田了. 题目描述 田地是一个巨大的矩形,然而zzc 每次只能种一个正方形,而每种一个正方形时zzc所花的体力值是正方形的周长,种过的田不可以再种,zzc很懒还要节约体力 ...