When switching int to String in Java, which is better, Integer.toString(i) or new Integer(i).toString()?

Asked 2 years ago, Updated 2 years ago, 70 views

Someone asked me a question, and when I initialized a large amount of int,

What's the difference if you have?

string java int

2022-09-22 22:30

1 Answers

In the Integer class, Integer.toString is a static method. So you don't have to create an Integer instance.

If you want to print out the int, it'll be lighter, faster, and more memory-saving if you write it in the former way. If you want to put the Integer value in the Collection and represent it as an object, you can create each object like the latter. Then it creates objects, so it's going to provide more functionality than just int.


2022-09-22 22:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.