Yogesh Mali
Simple Java Tips

Simple Java Tips

Difference between valueOf and parse methods

Yogesh Mali's photo
Yogesh Mali
·Sep 3, 2020·

1 min read

Subscribe to my newsletter and never miss my upcoming articles

In Java, you will see methods like valueOf and parseInt or parseBoolean.

Boolean var = Boolean.valueOf("true");

Boolean var = Boolean.parseBoolean("true");

Why we have these kind of duplicate methods?

There is a difference in these methods.

Boolean.valueOf returns a boxed object and not the primitive data type variable.

Boolean.parseBoolean returns a primitive data type and avoids creating unnecessary object.

If you have a string value of the variable and need to return a primitive data type, then use parse method. Avoid using valueOf. This will not create unnecessary objects.

Share this