Converting String to int in Java?

Report
Question

Please briefly explain why you feel this question should be reported .

Report
Cancel

Integer x = Integer.valueOf(str);
// or
int y = Integer.parseInt(str);

There is a slight difference between these methods:

  • valueOf returns a new or cached instance of java.lang.Integer
  • parseInt returns primitive int.

To convert a string into an int, use:

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

To convert a number into a string, use:
int num = 1234;
String str = String.valueOf(num);

0
Manimaran Samuthirapandi 10 months 2021-08-05T18:41:47+02:00 0 Answers 9 views 0

Leave an answer

Captcha Click on image to update the captcha .