CodeGym /Courses /JAVA 25 SELF /Conversion between data types

Conversion between data types

JAVA 25 SELF
Level 2 , Lesson 3
Available

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.

Important! You cannot perform arithmetic operations with the String type — even if the string consists entirely of digits.

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);
Converting a string to a number

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.

1
Task
JAVA 25 SELF, level 2, lesson 3
Locked
Access Code 🔑
Access Code 🔑
1
Task
JAVA 25 SELF, level 2, lesson 3
Locked
Flight number ✈️
Flight number ✈️
1
Task
JAVA 25 SELF, level 2, lesson 3
Locked
Film release year 🎞️
Film release year 🎞️
1
Task
JAVA 25 SELF, level 2, lesson 3
Locked
Summing game scores 🕹️
Summing game scores 🕹️
Comments (7)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Landon Keith Level 4, United States of America, United States
3 May 2026
I shan't pass.
Anonymous #11788082 Level 2, Jalesar, India
12 April 2026
this is not right cannot do the tasks without premium subscription
coolJavaPlayer Level 3, java land, Canada
26 March 2026
justise for pc
Hoist Level 38, San Diego, United States
25 November 2025
Output: Flight 987 to Minsk
nathan eshetu Level 2, Addis Ababa, Ethiopia
28 September 2025
how i now where is the task
Gold Stone Level 3, -, Nigeria
17 October 2025
How do we practice ?
Anonymous #11744457 Level 8, Seattle, United States
3 January 2026
you need to buy premium