


What is the difference between the efficiency of converting int to string and converting string to int in java?
Comparison of the efficiency of converting int to string and converting string to int
Converting string to int, two methods
1 2 |
|
The second method is to look at the source code and implement the first method.
The comment probably means this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
The parseInt() method is implemented in valueOf(). The time comparison of the second kind is much faster than the first kind.
1 2 |
|
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
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().
1 2 3 |
|
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:
1 2 3 4 5 6 7 8 |
|
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
1 2 3 4 5 6 7 8 9 10 11 |
|
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
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)的值肯定不会超出整数的范围,我们现在的程序是以正数的方式处理,如果反过来已负数的方式处理,问题不就解决了吗?修改代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
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的定义:
1 2 3 4 5 6 7 8 9 10 11 |
|
原码、反码、补码
我们知道,在计算机中,数据都是以二进制的形式存储的,比如,数字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进行求补运算,那么对于任意的范围内的数,可以得到:
1 |
|
即
1 |
|
而对于一个正数来说,对其进行求补运算其实得到的就是它的相反数的补码(负数的补码符号位保持不变,其他为全部取反再+1,因为正数和负数的符号位本来就不一样,所以对一个正数进行求补其实得到的就是它的相反数的补码)。
那么对于一个负数来说呢?对其进行求补运算是否能够得到其对应的正数的补码呢?
假设n>0,根据上面可知:
1 |
|
对f(n)进行求补运算,有:
1 |
|
其中,1 00000000 - n表示n对应负数的补码,对其进行求补运算得到的就是n,正数的补码就是其原码。
由上可知:如果数值以补码的形式保存,对一个数进行求补运算,可以得到其相反值,即:f(n) = -n
2、方便减法运算
如果数值以补码的方式存储,可以将减法变为加法,省去了减法器,通过上面的推导,如果数据以补码的方式存储,以f(n)表示对n进行求补运算,可以得到:
1 |
|
那么现在我们需要计算m - n,应该要怎么计算呢?如果以补码的方式存储,那么就有:
1 |
|
也就是说,减去一个数只需要加上对其进行求补运算后得到的值即可。
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.
