Home  >  Article  >  Java  >  What is the difference between the efficiency of converting int to string and converting string to int in java?

What is the difference between the efficiency of converting int to string and converting string to int in java?

王林
王林forward
2023-05-12 08:46:16991browse

    Comparison of the efficiency of converting int to string and converting string to int

    Converting string to int, two methods

    Interger.parseInt(String)
    Interger.valueOf(String).intValue()

    The second method is to look at the source code and implement the first method.

    What is the difference between the efficiency of converting int to string and converting string to int in java?

    The comment probably means this

    /**
          *返回一个包含整数的对象
          *指定的{@ String String}的值。 这个说法是
          *被解释为表示一个有符号的十进制整数
          *就好像这个论据是给予{@link的
          * #parseInt(java.lang.String)}方法。 结果是一个
          表示整数值的整数对象
          *由字符串指定。
         *
          换句话说,这个方法返回一个{@code Integer}
          *对象等于以下值:
         *
          * <blockquote>
          * {@code new Integer(Integer.parseInt(s))}
          * </ blockquote>
         *
          * @param是要解析的字符串。
          * @返回一个保存值的{整数}对象
          *由字符串参数表示。
          * @exception NumberFormatException如果字符串不能被解析
          *作为一个整数。
         */

    The parseInt() method is implemented in valueOf(). The time comparison of the second kind is much faster than the first kind.

     Integer.parseInt(str) : 21
     Integer.valueOf(str).intValue() : 14

    int There are generally three methods for converting strings

    • The first one: number ""

    • The second one: string.valueOf()

    • The third type: .toString()

    • Let’s talk about the first type first, which is simple and crude.

    • The second method: the bottom layer still uses the .toString() method

    • The third method is toString()

    Upload the code.

    int num = 888888;
     
            //(1)num + ""
            long start = System.currentTimeMillis();//得到开始运行时系统时间
            for(int i=0; i<100000; i++){
                String str = num + "";
            }
            long end = System.currentTimeMillis();//得到结束运行时系统时间
            System.out.println("num + \"\" : " + (end - start));
     
            //(2)String.valueOf(num)
            start = System.currentTimeMillis();
            for(int i=0; i<100000; i++){
                String str = String.valueOf(num);
            }
            end = System.currentTimeMillis();
            System.out.println("String.valueOf(num) : " + (end - start));
     
            //(3)Integer.toString(num)
            start = System.currentTimeMillis();
            for(int i=0; i<100000; i++){
                String str = Integer.toString(num);
            }
            end = System.currentTimeMillis();
            System.out.println("Integer.toString(num) : " + (end - start));

    The result is

    num "" : 82
    String.valueOf(num) : 32
    Integer.toString(num) : 9

    After many repeated tests, toString() is the fastest and num "" is the slowest. The source code is like this when using String.valueOf().

    public static String valueOf(Object obj) {
        return (obj == null) ? "null" : obj.toString();
    }

    That is to say, when using it, there is no need to judge whether the passed object is null, but pay special attention to that if the passed object is empty, a null string will be returned instead of a null value. , this place needs to be kept in mind.

    Analysis of string to int problem

    I believe that many students have encountered such a problem during interviews, asking to encapsulate a function to convert the String type to the int type. This seemingly simple problem actually hides many details, and it is not easy to truly encapsulate this function. What the interviewer wants to examine is not actually the difficulty of the algorithm itself. The algorithm of this question is actually not difficult at all. What the interviewer wants to examine is how carefully the programmer writes the code and considers whether the problem is comprehensive. In other words, we must try our best to to make the code robust. Below we analyze the details hidden in this problem step by step.

    Analysis

    First of all, we do not consider any exception handling. Assuming that the data passed in by the caller of the function is correct, it is easy to write the following code:

        public int strToInt(String str) {
            int number = 0;
            for (int i=0; i<str.length(); i++) {
                number *= 10;
                number += (str.charAt(i) - &#39;0&#39;);
            }
            return number;
        }

    The above code will traverse each character of the string, convert it into the corresponding integer, and then integrate it into the integer data number one by one.

    If you submit such a code to the interviewer, the result will definitely not be satisfactory. Because you have not considered the robustness of the program. The functions we encapsulate are equivalent to API interfaces and are provided to all developers. It is inevitable that other developers will not pass in some strange parameters, and this code has no response to abnormal parameters. If any processing is done, once the exception parameter is passed in, the program will crash directly. Below we will improve this function step by step and improve its robustness.

    1. For the incoming string is an empty object or the string is an empty string
        public int strToInt(String str) throws NumberFormatException{
            if (str == null || str.contentEquals("")) { // 如果传入的字符串为空对象或者传入的字符串为空字符串,则抛出异常
                throw new NumberFormatException("null or empty string"); // 这里直接利用java封装好的异常类,当然我们也可以自己封装异常类,面试官要考察的不是对异常类的封装,而是你要知道要处理异常情况
            }
            int number = 0;
            for (int i=0; i<str.length(); i++) {
                number *= 10;
                number += (str.charAt(i) - &#39;0&#39;);
            }
            return number;
        }

    First of all, whether the string is empty or whether it is an empty string, if so, Then throw an exception directly. Here we use the Java-encapsulated exception class NumberFormatException. Of course, we can also encapsulate the exception class ourselves. What the interviewer wants to examine is not the encapsulation of the exception class, but that you need to know how to handle exceptions.

    2. Processing of the sign bit

    We'd better ask the interviewer in advance whether it is possible that the incoming number is a negative number. When it is a positive number, whether the sign bit is allowed? If so, we have to deal with the sign bit. The first character of a negative number is "-". We only need to determine whether the first character is "-" to know whether the incoming number is a negative number. If it is a positive number The signed bit is allowed. The first character there may be " ". We also need to handle it accordingly:

        public int strToInt(String str) throws NumberFormatException{
            if (str == null || str.contentEquals("")) { // 如果传入的字符串为空对象或者传入的字符串为空字符串,则抛出异常
                throw new NumberFormatException("null or empty string"); // 这里直接利用java封装好的异常类,当然我们也可以自己封装异常类,面试官要考察的不是对异常类的封装,而是你要知道要处理异常情况
            }
            boolean negative = false; // negative为true表示是负数,反之为正数
            int pos = 0;
            if (str.charAt(0) == &#39;-&#39;) { // 如果为负数
                negative = true;
                pos++; // 调过第一位符号位
            } else if (str.charAt(0) == &#39;+&#39;) {
                pos++; // 调过第一位符号位
            }
            int number = 0;
            while (pos < str.length()) {
                number *= 10;
                number += (str.charAt(pos) - &#39;0&#39;);
                pos++;
            } 
            return negative ? -number : number; // 如果为负数则返回对应的负数
        }
    3. Processing of wrong characters

    The caller of the function may A messy string will be passed in, such as "abc23123". In this case, we have to deal with it accordingly. We should throw an exception to the caller to inform the caller that the string passed in is an illegal string:

        public int strToInt(String str) throws NumberFormatException{
            if (str == null || str.contentEquals("")) { // 如果传入的字符串为空对象或者传入的字符串为空字符串,则抛出异常
                throw new NumberFormatException("null or empty string"); // 这里直接利用java封装好的异常类,当然我们也可以自己封装异常类,面试官要考察的不是对异常类的封装,而是你要知道要处理异常情况
            }
            boolean negative = false; // negative为true表示是负数,反之为正数
            int pos = 0;
            if (str.charAt(0) == &#39;-&#39;) { // 如果为负数
                negative = true;
                pos++; // 调过第一位符号位
            } else if (str.charAt(0) == &#39;+&#39;) {
                pos++; // 调过第一位符号位
            }
            int number = 0;
            while (pos < str.length()) {
                if (str.charAt(pos) >= &#39;0&#39; && str.charAt(pos) <= &#39;9&#39;) { // 只有字符在&#39;0&#39;到&#39;9&#39;的范围内,才算正确的字符
                    number *= 10;
                    number += (str.charAt(pos) - &#39;0&#39;);
                    pos++; 
                } else {
                    throw new NumberFormatException("invalid string"); // 当字符是其他字符时,抛出异常告知调用者传入的字符串错误
                }
            } 
            return negative ? -number : number; // 如果为负数则返回对应的负数
        }
    4. Processing of integer data out of range

    The string passed in by the caller may be a very long string, and the conversion to an integer may exceed the storage range of the integer, such as "12345678674324334" , in this case, we need to throw an exception to inform the caller that the incoming string exceeds the range of the integer:

        public int strToInt(String str) throws NumberFormatException{
            if (str == null || str.contentEquals("")) { // 如果传入的字符串为空对象或者传入的字符串为空字符串,则抛出异常
                throw new NumberFormatException("null or empty string"); // 这里直接利用java封装好的异常类,当然我们也可以自己封装异常类,面试官要考察的不是对异常类的封装,而是你要知道要处理异常情况
            }
            boolean negative = false; // negative为true表示是负数,反之为正数
            int pos = 0;
            if (str.charAt(0) == &#39;-&#39;) { // 如果为负数
                negative = true;
                pos++; // 调过第一位符号位
            } else if (str.charAt(0) == &#39;+&#39;) {
                pos++; // 调过第一位符号位
            }
            int limit = negative ? (-Integer.MIN_VALUE) : Integer.MAX_VALUE;
            int mult = limit / 10; // 记录最大数/10,让number和这个数比较,如果大于它,则number * 10肯定也就大于最大数
            int number = 0;
            while (pos < str.length()) {
                if (str.charAt(pos) >= &#39;0&#39; && str.charAt(pos) <= &#39;9&#39;) { // 只有字符在&#39;0&#39;到&#39;9&#39;的范围内,才算正确的字符
                    if (number > mult) {// 让number和mult比较,如果大于它,则number * 10肯定也就大于最大数
                        throw new NumberFormatException("input string beyond int size");
                    }
                    number *= 10;
                    int digit = str.charAt(pos) - &#39;0&#39;;
                    if (number > limit - digit) { // 这里不能用number + digit > limit来判断,因为number + digit可能超出整数的存储范围,相加后的数可能是一个负数,但是limit - digit肯定不会超出
                        throw new NumberFormatException("input string beyond int size");
                    } else {
                        number += digit;
                    }
                    pos++;
                } else {
                    throw new NumberFormatException("invalid string"); // 当字符是其他字符时,抛出异常告知调用者传入的字符串错误
                }
            } 
            return negative ? -number : number; // 如果为负数则返回对应的负数
        }

    In the above code, when we judge whether number will exceed the maximum integer, the first is Let's compare the value of (maximum integer/10) first, instead of multiplying it by 10 and comparing it with the maximum integer. This is because if number * 10 exceeds the integer range, it will cause data overflow, and the resulting value may be one Negative numbers, and the value of (maximum integer/10) will not cause data overflow. This is also a small detail. Maybe you think this function is perfect, but now I want to tell you that the above writing is wrong.

    为什么呢?这要从整数的范围说起,整数的取值范围是(-2^31)至(2^31 - 1),从绝对值的角度看,最小负数相比于最大正数大1。所以上面代码中(-Integer.MIN_VALUE)会超出整形的范围,造成数据溢出,也就是说上面的代码对负数最小范围的限制的处理是错误的。那么怎么解决这个问题呢?

    我们换个角度思考,最小负数的绝对值比最大正数的绝对值大1,那(-Integer.MAX_VALUE)的值肯定不会超出整数的范围,我们现在的程序是以正数的方式处理,如果反过来已负数的方式处理,问题不就解决了吗?修改代码如下:

        public int strToInt(String str) throws NumberFormatException{
            if (str == null || str.contentEquals("")) { // 如果传入的字符串为空对象或者传入的字符串为空字符串,则抛出异常
                throw new NumberFormatException("null or empty string"); // 这里直接利用java封装好的异常类,当然我们也可以自己封装异常类,面试官要考察的不是对异常类的封装,而是你要知道要处理异常情况
            }
            boolean negative = false; // negative为true表示是负数,反之为正数
            int pos = 0;
            if (str.charAt(0) == &#39;-&#39;) { // 如果为负数
                negative = true;
                pos++; // 调过第一位符号位
            } else if (str.charAt(0) == &#39;+&#39;) {
                pos++; // 调过第一位符号位
            }
            int limit = negative ? Integer.MIN_VALUE : (-Integer.MAX_VALUE);
            int mult = limit / 10;
            int number = 0;
            while (pos < str.length()) {
                if (str.charAt(pos) >= &#39;0&#39; && str.charAt(pos) <= &#39;9&#39;) { // 只有字符在&#39;0&#39;到&#39;9&#39;的范围内,才算正确的字符
                    if (number < mult) {
                        throw new NumberFormatException("input string beyond int size");
                    }
                    number *= 10;
                    int digit = str.charAt(pos) - &#39;0&#39;;
                    if (number < limit + digit) {
                        throw new NumberFormatException("input string beyond int size");
                    } else {
                        number -= digit;
                    }
                    pos++;
                } else {
                    throw new NumberFormatException("invalid string"); // 当字符是其他字符时,抛出异常告知调用者传入的字符串错误
                }
            } 
            return negative ? number : -number;
        }

    OK,现在我们把能够想到的异常情况处理了。再来考虑一个问题,为什么整形数据的范围是(-2^31)至(2^31 - 1),最小负数的绝对值比最大正数的绝对值要大1呢?

    5、int数据范围的讨论

    我们知道,一个int类型占四个字节,也就是32位,其中第一位是符号位,符号位为0表示正数,为1表示负数,其余31位表示数值。正常来说int类型的数据范围应该是(-2^31-1)到(2^31-1),为什么负数会多一位呢?

    我们首先看一下Java代码中对Integer.MAX_VALUE和Integer.MIN_VALUE的定义:

        /**
         * A constant holding the minimum value an {@code int} can
         * have, -2<sup>31</sup>.
         */
        public static final int   MIN_VALUE = 0x80000000;
     
        /**
         * A constant holding the maximum value an {@code int} can
         * have, 2<sup>31</sup>-1.
         */
        public static final int   MAX_VALUE = 0x7fffffff;
    原码、反码、补码

    我们知道,在计算机中,数据都是以二进制的形式存储的,比如,数字10,其二进制形式就是1010。

    一个字节有8位,每位可以存储一个01字符,byte类型占1个字节,也就是8位,其中,最高位是符号位,用来表示数值是正数还是负数,符号位为0表示正数,符号位为1表示负数。我们先来看一下原码、反码、补码的定义:

    • 原码:符号位加上真值的绝对值, 即用第一位表示符号, 其余位表示值。

    • 反码:正数的反码是其本身;负数的反码是在其原码的基础上, 符号位不变,其余各个位取反。

    • 补码:补码的表示方法是:正数的补码就是其本身;负数的补码是在其原码的基础上, 符号位不变, 其余各位取反, 最后+1。 (即在反码的基础上+1)

    正数的原码、反码、补码都是其本身;负数的反码是在其原码的基础上,符号位不变,其余个位取反,负数的补码是其反码的基础上+1。

    举例说明(下面都以byte类型进行举例):

    数据 原码 反码 补码
    10 00001010 00001010 00001010
    -10 10001010 11110101 11110110

    计算机中,数据都是以补码的形式存储的。为什么要以补码的形式存储呢?有两个原因:

    1、如果数值以补码的形式保存,对一个数进行求补运算,可以得到其相反值

    求补运算:将一个数(包括正数和负数)所有二进制位(包括符号位和数值位)取反,然后在最低位加上1。

    为什么对一个数进行求补运算,可以得到其相反值呢?我们先来分析一下求补运算的定义,现将所有的二进制取反,然后+1,首先一个数和它所有位取反得到的数相加,其结果肯定是11111111,这是因为它们每一位都不一样,然后将结果+1,即11111111 + 1,结果是1 00000000,最高位的1已经溢出,换种方式说,如果以f(n)表示对n进行求补运算,那么对于任意的范围内的数,可以得到:

    n + f(n) = 1 00000000

    f(n) = 1 00000000 - n

    而对于一个正数来说,对其进行求补运算其实得到的就是它的相反数的补码(负数的补码符号位保持不变,其他为全部取反再+1,因为正数和负数的符号位本来就不一样,所以对一个正数进行求补其实得到的就是它的相反数的补码)。

    那么对于一个负数来说呢?对其进行求补运算是否能够得到其对应的正数的补码呢?

    假设n>0,根据上面可知:

    f(n) = 1 00000000 - n

    对f(n)进行求补运算,有:

    f(f(n)) = f(1 00000000 - n) = 1 00000000 - (1 00000000 - n) = n

    其中,1 00000000 - n表示n对应负数的补码,对其进行求补运算得到的就是n,正数的补码就是其原码。

    由上可知:如果数值以补码的形式保存,对一个数进行求补运算,可以得到其相反值,即:f(n) = -n

    2、方便减法运算

    如果数值以补码的方式存储,可以将减法变为加法,省去了减法器,通过上面的推导,如果数据以补码的方式存储,以f(n)表示对n进行求补运算,可以得到:

    f(n) = -n

    那么现在我们需要计算m - n,应该要怎么计算呢?如果以补码的方式存储,那么就有:

    m - n = m + (-n) = m + f(n)

    也就是说,减去一个数只需要加上对其进行求补运算后得到的值即可。

    3、使用补码存储数据,可以对任意的两个数直接进行相加运算,不用考虑符号位

    4、通过补码形式存储,规定10000000对应的负数的最小值,也就是-128。

    由上面可知,如果是byte类型,数据范围应该为[-128,127],最小负数要比最大正数多一位。

    The above is the detailed content of What is the difference between the efficiency of converting int to string and converting string to int in java?. For more information, please follow other related articles on the PHP Chinese website!

    Statement:
    This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete