简体   繁体   English

Integer.toString()如何在内部工作?

[英]How does Integer.toString() works internally?

I found that a similar question has been asked before here : how does Float.toString() and Integer.toString() works? 我发现之前已经问过类似的问题: Float.toString()和Integer.toString()是如何工作的?

But this doesn't speak about how that function internally works. 但这并没有谈到该功能如何在内部运作。 When I opened the internally source code of Integer.toString() , it is not understandable for normal junior java programmer. 当我打开Integer.toString()的内部源代码时,普通的初级java程序员是不可理解的。

Can somebody please explain what happens internally in short description ? 有人可以解释一下内部的简短说明吗?


NOTE : This was one of the interview questions that I was asked recently. 注意:这是我最近被问到的面试问题之一。 I had no idea about how to answer such question ! 我不知道如何回答这个问题!

The no arg call of integer.toString() simply calls the static method Integer.toString(int i) (using the integer variables own primitive value), which is implemented as below; integer.toString()的无参数调用只是调用静态方法Integer.toString(int i) (使用integer变量自身的原始值),其实现如下;

  public static String toString(int i) {
       if (i == Integer.MIN_VALUE)
           return "-2147483648";
       int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
       char[] buf = new char[size];
       getChars(i, size, buf);
       return new String(0, size, buf);
   }

First it checks whether it's value is == the lowest possible integer, and returns that if it is equal. 首先,它检查它的值是否==最低可能的整数,并返回它是否相等。 If not, then it checks what size the String needs to be using the stringSize() method of Integer to use as the size of an array of characters. 如果没有,那么它使用IntegerstringSize()方法检查String需要的大小,以用作字符数组的大小。

stringSize() implementation below; stringSize()实现如下;

  static int stringSize(int x) {
       for (int i=0; ; i++)
           if (x <= sizeTable[i])
               return i+1;
   }

Once it has a char[] of the correct size, it then populates that array using the getChars() method, implemented below; 一旦它具有正确大小的char[] ,它就会使用getChars()方法填充该数组,如下所示;

  static void getChars(int i, int index, char[] buf) {
       int q, r;
       int charPos = index;
       char sign = 0;

       if (i < 0) {
           sign = '-';
           i = -i;
       }

       // Generate two digits per iteration
       while (i >= 65536) {
           q = i / 100;
       // really: r = i - (q * 100);
           r = i - ((q << 6) + (q << 5) + (q << 2));
           i = q;
           buf [--charPos] = DigitOnes[r];
           buf [--charPos] = DigitTens[r];
       }

       // Fall thru to fast mode for smaller numbers
       // assert(i <= 65536, i);
       for (;;) {
           q = (i * 52429) >>> (16+3);
           r = i - ((q << 3) + (q << 1));  // r = i-(q*10) ...
           buf [--charPos] = digits [r];
           i = q;
           if (i == 0) break;
       }
       if (sign != 0) {
           buf [--charPos] = sign;
       }
   }

Explaining each individual step would take far too long for for a stackoverflow answer. 对于stackoverflow答案,解释每个单独的步骤将花费太长时间。 The most pertinent section however (as pointed out in the comments) is the getChars() method which, complicated bit shifting aside, is essentially process of elimination for finding each character. 然而,最相关的部分(正如评论中所指出的那样)是getChars()方法,除了复杂的位移之外,它基本上是用于查找每个字符的消除过程。 I am afraid I can't go into any greater detail than that without going beyond my own understanding. 如果不超出我的理解,我恐怕不能再深入细节了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM