It is often necessary to handle data in our programs, and it is not in the format that we want. With Java, you can do conversions of objects, such as a string to int, and vice-versa. In this article, we will show you several conversions methods in Java.
Make the conversion of a String to an integer using the parseInt() method of Integer class, or by using the methods valueOf() and intValue() of the Integer class combined, as shown in the following code.
str = "25"; int i = Integer.valueOf (str).intValue(); //or int i = Integer.parseInt(str);
Convert a String to float through the combination of the methods valueOf () and floatValue () of Float class.
float f = Float.valueOf(str).floatValue();
You can make the conversion of a String to long using the parseLong() method of Long class, or using the combination of the methods valueOf() and longValue(), also from Long class.
long l = Long.valueOf(str).longValue(); //or long l = Long.parseLong(str);
Similarly, going in the other direction, we can:
See in the code below two ways to do the conversion of an integer to a string:
int i = 42; String str = Integer.toString(i); //or String str = "" + i;
You can do the conversion from long to String using the toString method of the class.
long l = 42; String str = Long.toString(l);
If you want to explore more about Java, visit our videos section! Below are some examples:
You can also follow some of the broadcasters who program in Java, the below:
Another cool way to find out interesting things about jQuery is to access our project page!
In 2024 we're witnessing a critical point in democratic technology: the integration of blockchain and…
We’re thrilled to announce an exciting opportunity for you to win not one but two…
Acquiring practical skills is crucial for career advancement and personal growth. Education Ecosystem stands out…
Artificial Intelligence (AI) has been making significant strides in various industries, and the software development…
Another week to bring you the top yield platforms for three of the most prominent…
If you hold a large volume of LEDU tokens above 1 million units and wish…
View Comments
I thought of doing it another way, but this one seems simpler.