Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
196 views
in Technique[技术] by (71.8m points)

java - 如何从int转换为String?(How do I convert from int to String?)

I'm working on a project where all conversions from int to String are done like this:

(我正在一个项目中,所有从intString转换都是这样完成的:)

int i = 5;
String strI = "" + i;

I'm not familiar with Java.

(我对Java不熟悉。)

Is this usual practice or is something wrong, as I suppose?

(我想这是通常的做法还是出了什么问题?)

  ask by Denis Palnitsky translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Normal ways would be Integer.toString(i) or String.valueOf(i) .

(正常的方式是Integer.toString(i)String.valueOf(i) 。)

The concatenation will work, but it is unconventional and could be a bad smell as it suggests the author doesn't know about the two methods above (what else might they not know?).

(串联可以工作,但它是非常规的,可能会产生难闻的气味,因为这表明作者不了解上述两种方法(他们可能还不知道什么?)。)

Java has special support for the + operator when used with strings (see the documentation ) which translates the code you posted into:

(当与字符串一起使用时,Java对+运算符有特殊的支持(请参阅文档 ),它将您发布的代码转换为:)

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

at compile-time.

(在编译时。)

It's slightly less efficient ( sb.append() ends up calling Integer.getChars() , which is what Integer.toString() would've done anyway), but it works.

(它的效率稍微低一些( sb.append()最终调用Integer.getChars() ,无论如何Integer.toString()都会这样做),但是它可以工作。)

To answer Grodriguez's comment: ** No, the compiler doesn't optimise out the empty string in this case - look:

(要回答Grodriguez的评论:**不,在这种情况下,编译器不会优化空字符串-请看:)

simon@lucifer:~$ cat TestClass.java
public class TestClass {
  public static void main(String[] args) {
    int i = 5;
    String strI = "" + i;
  }
}
simon@lucifer:~$ javac TestClass.java && javap -c TestClass
Compiled from "TestClass.java"
public class TestClass extends java.lang.Object{
public TestClass();
  Code:
   0:    aload_0
   1:    invokespecial    #1; //Method java/lang/Object."<init>":()V
   4:    return

public static void main(java.lang.String[]);
  Code:
   0:    iconst_5
   1:    istore_1

Initialise the StringBuilder:

(初始化StringBuilder:)

   2:    new    #2; //class java/lang/StringBuilder
   5:    dup
   6:    invokespecial    #3; //Method java/lang/StringBuilder."<init>":()V

Append the empty string:

(追加空字符串:)

   9:    ldc    #4; //String
   11:    invokevirtual    #5; //Method java/lang/StringBuilder.append:
(Ljava/lang/String;)Ljava/lang/StringBuilder;

Append the integer:

(追加整数:)

   14:    iload_1
   15:    invokevirtual    #6; //Method java/lang/StringBuilder.append:
(I)Ljava/lang/StringBuilder;

Extract the final string:

(提取最终字符串:)

   18:    invokevirtual    #7; //Method java/lang/StringBuilder.toString:
()Ljava/lang/String;
   21:    astore_2
   22:    return
}

There's a proposal and ongoing work to change this behaviour, targetted for JDK 9.

(针对JDK 9,有一项建议和正在进行的工作来改变这种行为。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...