-2

I am developing a Java program and I'm meeting cases where I get undecided whether to use the casting a string to integer method, or to use the integer.parseInt method. Is there any clear benefit for either of the two methods?

With 'casting to string method', I mean:

String.valueOf(integer);
Joeytje50
  • 18,636
  • 15
  • 63
  • 95
Ford
  • 1
  • 2
  • 2
    Please clarify _to cast a string to integer method_. – Sotirios Delimanolis May 20 '14 at 18:04
  • (Don't add code to comments. Edit your question.) Did you try running that code? – Sotirios Delimanolis May 20 '14 at 18:16
  • `String.valueOf(integer)` is not casting. It is calling a method. Looking at the source code for it would be enlightening. – David Conrad May 20 '14 at 18:31
  • @ David,i happen to have been examined in a java-based interview with this question which proved difficult:Explain the difference between casting a string to an integer and using the Integer.parseInt.please clarify. method. – Ford May 20 '14 at 18:51
  • possible duplicate of [Integer.valueOf() vs. Integer.parseInt()](http://stackoverflow.com/questions/7355024/integer-valueof-vs-integer-parseint) – femtoRgon May 20 '14 at 19:57

1 Answers1

1

As far as I know, it's not possible to cast from a String to an int, so using Integer.parseInt seems like the best option here.

Looking at your edits about using valueOf, perhaps this link may help: Integer.valueOf() vs. Integer.parseInt()

Community
  • 1
  • 1
helencrump
  • 1,351
  • 1
  • 18
  • 27