What is the difference between parseInt and valueOf in Java?

Asked 2 years ago, Updated 2 years ago, 122 views

What is the difference between parseInt and valueOf? I think parseFloat(), parseDouble(), parseLong() are the same... What's the difference between this and Long.valueOf(string)?

Which one is more commonly used?

java language-feature

2022-09-22 22:28

1 Answers

If you look at the API... Integer.valueOf(String) is interpreted exactly as String did Integer.parseInt(String). However, valueOf(String) returns the object to newInteger() and parseInt(String) returns the int default datatype.

If you want to write some efficient code with Integer.valueOf(int), you need to write an eye-catching code like below. Integer k = Integer.valueOf(Integer.parseInt("123"))

In conclusion, when converting a string, you can use valueOf(String) if you want to receive it as an object other than the basic data type. If not, use parseInt(String).


2022-09-22 22:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.