Get the App
SLTechnology News&Howtos  ›  Development  › 

How to convert String and int in Android

Shulou Source: shulou.com Published: 2022-06-02 06:46:55 11月12日 Update

Editor to share with you how to convert String and int in Android. I hope you will get something after reading this article. Let's discuss it together.

First, how to convert the string String to integer int?

Method 1:

Int I = Integer.parseInt ([String]); or I = Integer.parseInt ([String], [int radix])

Method 2:

Int I = Integer.valueOf (my_str). IntValue (); what's the difference between the two methods? Is the effect the same? Is it interchangeable under any circumstances?

Method 1:

/ / using static methods directly will not result in redundant objects, but will throw an exception I = Integer.parseInt (s)

Method 2:

/ / Integer.valueOf (s) is equivalent to new Integer (Integer.parseInt (s)) and will also throw an exception, but it will produce an extra object I = Integer.valueOf (s) .intValue ()

Note: the methods of converting strings to Double, Float and Long are more or less the same.

Second, how to convert integer int to string String?

Method 1:

String s = String.valueOf (I)

Method 2:

String s = Integer.toString (I)

Method 3:

String s = "" + I; what's the difference between the two methods? Is the effect the same? Is it interchangeable under any circumstances?

Method 1:

/ / two String objects will be generated, s = I + ""

Method 2:

/ / directly use the static method of the String class and produce only one object s = String.valueOf (I)

Note: the methods of converting Double, Float and Long into strings are more or less the same.

After reading this article, I believe you have a certain understanding of "how to convert String and int in Android". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Tags: Methods objects more or less the same functions integers articles static Datong two finished more knowledge industry information channels channels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information vpn MariaDB MySQL macOS