1. Why do you need type conversion?
Java is a statically typed language. If the computer has a “box” for a number, then trying to shove text into it is about the same as trying to put a fork into a USB port. The compiler won’t forgive you for that! 😡
However, in real life we often work with different data types at the same time. For example, you may have a string that contains a number. That’s where conversion (or transformation) of types is needed.
Let’s look at an example. Suppose you want to store the number of users:
int a = 1;
int b = "2";
int c = "three";
The compiler will immediately say: “Who do you take me for? I can’t automatically convert a string to a number without your help. You need to convert the string to a number explicitly.”
In Java there are several ways to perform conversion, each with its pros, cons, and typical pitfalls.
2. Converting int to String
In our work it’s often necessary to get the string representation of a number: for example, to print it, save it to a file, send it over the network, combine it with text, etc. In Java there are several ways to do this, each convenient in different situations.
Method String.valueOf()
This is the primary and most common way:
int number = 42;
String str = String.valueOf(number); // str == "42"
String.valueOf() converts a number into a string that corresponds to the type of the object passed to it.
Concatenation with an empty string
An old but working approach:
int number = 42;
String str = "" + number;
This approach is quick for simple cases, but it is less explicit for someone reading the code.
3. Implicit conversion to a string
As mentioned above, the designers of Java made it so that absolutely all variables, objects, and expressions in Java can be converted to type String.
Moreover, this happens automatically when we add a String to some other type. Examples:
int a = 5;
String name = "Anya" + a; // name contains the string Anya5
int a = 5;
String city = a + "New York" + a; // city contains the string 5New York5
int number = 10;
String code = "Yo";
String message = "Hello! " + number + code; // message contains the string Hello! 10Yo
In all three examples we freely added variables of types int and String, and the result was always of type String.
Examples:
int a = 5;
String name = "1" + a; // name contains the string 15
int a = 5;
String city = a + "9" + a; // city contains the string 595
int number = 10;
String code = "10";
String message = "" + number + code; // message contains the string 1010
The addition operation is evaluated left to right, so the result can be somewhat surprising. Example:
int a = 5;
String name = a + a + "1" + a; // name contains the string 1015
Order of evaluation: ((a + a) + "1") + a
4. Converting a string to a number
To convert a number to a string in Java, it is enough to add it to an empty string:
String str = "" + number;
But what if you need to convert a string to a number? Well, you can’t convert just any string to a number. But if the string consists only of digits, then you can. For this, Java has a special function.
It looks like this:
int x = Integer.parseInt(string);
Where int x is the declaration of the integer variable x, and string is a number provided as a string (a string consisting of digits).
Examples:
String str = "123";
int number1 = Integer.parseInt(str); // number1 contains the number 123;
int number2 = Integer.parseInt("321"); // number2 contains the number 321
int number3 = Integer.parseInt("321" + 0); // number3 contains the number 3210
int number4 = "321"; // Will not compile: variable is of type int, but the value is of type String
This also works for negative values: the Integer.parseInt() method is quite smart.
GO TO FULL VERSION