Java data type conversion among int, Integer and String

This blog presents how to convert data types among int, Integer and String.

1. int, Integer and String

1.1 int

Refer to the Java™ Tutorials: Primitive Data Types )

By default, the int data type is a 32-bit signed two’s complement integer, which has a minimum value of -231 and a maximum value of 231-1. In Java SE 8 and later, you can use the int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 232-1. Use the Integer class to use int data type as an unsigned integer. See the section The Number Classes for more information. Static methods like compareUnsigned, divideUnsigned etc have been added to the Integer class to support the arithmetic operations for unsigned integers.

1.2 Integer

Refer to here.

//java.lang.Object <-- java.lang.Number <--java.lang.Integer
//All Implemented Interfaces: Serializable, Comparable<Integer>

public final class Integer extends Number implements Comparable<Integer>

The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int.

Note that the ‘int’ type is a primitive , whereas the ‘Integer’ type is an object.

1.3 String

Refer to here.

//java.lang.Object <-- java.lang.String
//All Implemented Interfaces: Serializable, CharSequence, Comparable<String>

public final class String extends Object implements Serializable, Comparable<String>, CharSequence

//for example
String str = "abc";
char str[] = {'a', 'b', 'c'};
String str = new String(data);

The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class. Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared.

2. int <–> String

2.1 int to String

There are two ways to convert an int to a String.

//Class Integer
static String toString(int i) //Returns a String object representing the specified integer.
//Class String
public static String valueOf(int i) //Returns the string representation of the int argument.

//for example
int i = 1234;
String str;
str = Integer.toString(i);
str = String.valueOf(i);

2.2 String to int

public static int parseInt(String s, int radix) throws NumberFormatException

//String can be null. To avoid 'java.lang.NumberFormatException: null',
int i = str != null ? Integer.parseInt(str) : -1;

3. String <–> Integer

3.1 String to Integer

static Integer valueOf(String s) //Returns an Integer object holding the value of the specified String.

// for example
String str = "1234";
Integer integer = Integer.valueOf(str) //OR
Integer integer = new Integer(str)

3.2 Integer to String

String toString() //Returns a String object representing this Integer's value.

//for example
Integer integer = new Integer("1234")
String str = integer.toString()

4. int <–> Integer

4.1 int to Integer

static Integer valueOf(int i) //Returns an Integer instance representing the specified int value.

//for example
int i = 1234;
Integer integer = Integer.valueOf(i);

4.2 Integer to int

int intValue() //Returns the value of this Integer as an int.

//Noted that an Integer can be null. The better way to convert an Integer to an int is:
Integer integer = new Integer("1234");
int i = integer != null ? integer.intValue() : -1;
赞赏

微信赞赏支付宝赞赏

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注