How does Integer.parseInt(string) actually work?

后端 未结 7 1878
南笙
南笙 2020-12-02 20:43

Was asked this question recently and did not know the answer. From a high level can someone explain how Java takes a character / String and convert it into an int.

M

7条回答
  •  心在旅途
    2020-12-02 21:13

    • Find the length of the String (s) (say maxSize )
    • Initialize result = 0
    • begin loop ( int j=maxSize, i =0 ; j > 0; j--, i++)
    • int digit = Character.digit(s.charAt(i))
    • result= result + digit * (10 power j-1)
    • end loop
    • return result

提交回复
热议问题