Java BeanUtils BeanUtils and ConvertUtils - Java Bean Utils

What is Java BeanUtils BeanUtils and ConvertUtils?

Description

The BeanUtils is defined as a utility method for populating JavaBeans properties and ConvertUtils method converts string scalar values to objects, string arrays to arrays of the specified class.

BeanUtils

The BeanUtils accepts string values by the use of the setter methods and automatically converts them to suitable property types for Java primitives and uses the getter techniques for reverse conversion. The populate() method accepts set of property values from java.util.HashMap and uses the right setters whenever bean comprise the property with the same name.

Example

The below instance shows usage of BeanUtils properties:

Now we will create another class called User.java as shown below:

OutPut

  • Let's carry out the following steps to see how above code works:
  • Save the above first code as Test.java.

Now execute the code using Run option or Ctrl+f11 and output as below gets displayed.

Java BeanUtils - BeanUtils and ConvertUtils


ConvertUtils

The Apache Commons BeanUtils is a library that comes with a number of converters to convert to and from different data sorts and also contain ConvertUtils utility class which uses these converters.

Example

The below example shows the conversion of string array to a double array using ConvertUtils utility:

OutPut

  • Save the above first code as ConvertStringArrayToDoubleArray.java.
  • Now execute the code using Run option or Ctrl+f11 and output as below gets displayed.

Java BeanUtils - BeanUtils and ConvertUtils

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Java Bean Utils Topics