1

I enjoy CodeFights at the moment and at the end of my last fight i found something interesting. The code in those two cases (mine and the opponent) was said to be correct. Is there a difference between this source code:

return Integer.toString(Character.getNumericValue(ch1) + Character.getNumericValue(ch2));

and this one:

return new Integer(Character.getNumericValue(ch1)+ Character.getNumericValue(ch2)).toString();

What is the key that i am missing?

Burdzi0
  • 80
  • 1
  • 9

2 Answers2

1

From javadoc https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html

String toString() Returns a String object representing this Integer's value.

static String toString(int i) Returns a String object representing the specified integer.

Xiongbing Jin
  • 11,779
  • 3
  • 47
  • 41
1

Integer's toString method is implemented as Integer.toString(value), so the second answer merely has a redundant instantiation.

@Override
public String toString() {
    return Integer.toString(value);
}
Allen G
  • 1,160
  • 6
  • 8