Integer.toString()如何在内部工作? [英] How does Integer.toString() works internally?

查看:140
本文介绍了Integer.toString()如何在内部工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此之前发现了一个类似的问题:如何Float.toString()和Integer.toString()是否有效?



但这并没有谈到该函数内部如何工作。当我打开 Integer.toString()的内部源代码时,对于普通的初级java程序员来说这是不可理解的。



<请问有人可以在简短说明中解释内部发生的事情吗?






注意:这是我面试的问题之一有人问道。我不知道如何回答这个问题!

解决方案

整数.toString的无参数调用()只需调用静态方法 Integer.toString(int i)(使用整数变量拥有原始值),实现如下;

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

首先检查它的值是否为 == 可能的最小整数,如果相等则返回。如果没有,那么它检查 String 的大小需要使用 stringSize()方法 Integer 用作字符数组的大小。



stringSize()下面的实现;

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

一旦有 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;
}

//每次迭代产生两位数
而(i> = 65536){
q = i / 100;
//真的:r = i - (q * 100);
r = i - ((q << 6)+(q << 5)+(q << 2));
i = q;
buf [--charPos] = DigitOnes [r];
buf [--charPos] = DigitTens [r];
}

//通过快速模式下降到较小的数字
//断言(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;
}
}

解释每个步骤需要花费太长时间才能完成stackoverflow答案。然而,最相关的部分(正如评论中所指出的)是 getChars()方法,除了复杂的位移之外,它基本上是用于查找每个字符的消除过程。如果不超出我自己的理解,我恐怕不能再深入细节了。


I found that a similar question has been asked before here : how does Float.toString() and Integer.toString() works?

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.

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;

  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.

stringSize() implementation below;

  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;

  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. 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. I am afraid I can't go into any greater detail than that without going beyond my own understanding.

这篇关于Integer.toString()如何在内部工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆