8

For instance, when to use

GetterUtil.getBoolean()

and when

ParamUtil.getBoolean()?

Are both same, or is it expected to be used differently according to a parameter, a variable, etc? Can you give some examples for both?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Whimusical
  • 6,401
  • 11
  • 62
  • 105

2 Answers2

8

Both are util methods to avoid Null-Pointer Exceptions.

GetterUtil internally returns the default type and does the casting too. So in case where someone has passed a null value, it will return default value of the type.

Example:
Assume you have a String value "true", and you are expecting it will always be of type boolean. So you use GetterUtil.getBoolean("true") which will internally do the casting to boolen and return the value as boolean-true. Incase someone passes rubbish characters like "tr", it will be converted to boolean-false.

As mentioned ParamUtil does the same treatment with request parameters. ParamUtil internally uses the GetterUtil to have the above behaviour. It first retrieves the parameter (which always would be a string) and then passes it to GetterUtil.getType() method and in turn returns the proper type.

Community
  • 1
  • 1
Sandeep Nair
  • 3,630
  • 3
  • 26
  • 38
2

GetterUtil and ParmUtil both are different classes.

GetterUtil is to get the default values for basic Java data types.

ParamUtil is to retrive the values(of primitive data types) from the HttpReqeust.

Check the source code here for these two classes here

For GetterUtil http://docs.liferay.com/portal/6.0/javadocs/src-html/com/liferay/portal/kernel/util/GetterUtil.html

For ParamUtil http://docs.liferay.com/portal/5.1/javadocs/portal-kernel/com/liferay/portal/kernel/util/ParamUtil.java.html

ravikuwi
  • 120
  • 9
  • GetterUtil.getBoolean will return the default value of the boolean variable which is false. ParamUtil.getBoolean will take the HttpRequest and Parameter name and will return the corresponding boolean value. – ravikuwi Aug 16 '12 at 18:00