top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is the fastest way to convert a string into an integer in Java?

+2 votes
1,083 views

I want to convert a string say "98989" into an integer without using any library functions in java. Give fastest way to do it and explain why your method is best.

posted Jan 31, 2014 by Muskan

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

0 votes
String str = "1234";
int num = Integer.parseInt(str);

To convert a number into a string, use:

int num = 1234;   
String str = String.valueOf(num);
answer Feb 1, 2014 by Amit Kumar Pandey
0 votes

Integer.valueOf is fastest because it uses cached instance. So if there already exists a Integer in pool with value 98989,
that will be returned instead of creating new Integer instance.

Check this for details.
http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#valueOf(int)

answer Sep 10, 2015 by Rahul Jha
...