SoFunction
Updated on 2025-04-12

Convert String and int in Android

1. How to convert a string String into an integer int?

Method 1:

int i = ([String]);or i = ([String],[int radix]);

Method 2

int i = (my_str).intValue();

What is the difference between these two methods? Is the function the same? Can it be interchanged under any circumstances?

Method 1:

// Use static methods directly, no unnecessary objects will be generated, but exceptions will be throwni = (s);

Method 2:

// (s) is equivalent to new Integer((s)), and will also throw exceptions, but will generate one more objecti = (s).intValue(); 

Note: The method of converting a string to Double, Float, Long is similar.

2. How to convert an integer int into a string String?

Method 1:

String s = (i);

Method 2:

String s = (i);

Method 3:

String s = "" + i;

What is the difference between these two methods? Is the function the same? Can it be interchanged under any circumstances?

Method 1:

// Two String objects will be generateds = i + "";

Method 2:

// Use the static method of the String class directly to generate only one objects = (i); 

Note: Double, Float, Long The methods of converting to strings are similar.

This is the end of this article about the conversion of String and int in Android. For more related content on Android String and int conversion, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!