Categories: Uncategorized

Learn Java – How to Convert String to Int?

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.

Converting String to integer

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);

 

Converting a String to a float

Convert a String to float through the combination of the methods valueOf () and floatValue () of Float class.

float f = Float.valueOf(str).floatValue();

 

Converting a String to a long

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:

Converting an integer to a String

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;

 

Converting a long to a String

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:

Aikar

jennifer

Another cool way to find out interesting things about jQuery is to access our project page!

Dr. Michael J. Garbade

I, Dr. Michael J. Garbade is the co-founder of the Education Ecosystem (aka LiveEdu), ex-Amazon, GE, Rebate Networks, Y-combinator. Python, Django, and DevOps Engineer. Serial Entrepreneur. Experienced in raising venture funding. I speak English and German as mother tongues. I have a Masters in Business Administration and Physics, and a Ph.D. in Venture Capital Financing. Currently, I am the Project Lead on the community project -Nationalcoronalvirus Hotline I write subject matter expert technical and business articles in leading blogs like Opensource.com, Dzone.com, Cybrary, Businessinsider, Entrepreneur.com, TechinAsia, Coindesk, and Cointelegraph. I am a frequent speaker and panelist at tech and blockchain conferences around the globe. I serve as a start-up mentor at Axel Springer Accelerator, NY Edtech Accelerator, Seedstars, and Learnlaunch Accelerator. I love hackathons and often serve as a technical judge on hackathon panels.

View Comments

Recent Posts

Highest Stable Coin Yields – (W16 – 2024)

Another week to bring you the top yield platforms for three of the most prominent…

2 weeks ago

LEDU Token OTC Trading

If you hold a large volume of LEDU tokens above 1 million units and wish…

1 month ago

Highest Stable Coin Yields – (W12 – 2024)

It’s another week and like always we have to explore the top yield platforms for…

1 month ago

Binance Auto Invest – the Best Innovation in Crypto since Sliced Bread

At a time where we’re constantly seeking tools and strategies to simplify our crypto investments,…

1 month ago

Highest Stable Coin Yields – March 2024

As we kick off another week, it's time to explore the top yield platforms for…

2 months ago

Education Ecosystem Featured on Business Insider

We're excited to share that Education Ecosystem was recently featured in an article on Business…

2 months ago