SObject Numeric field - Integer.valueOf() vs casting

380    Asked by himanshusingh in Data Science , Asked on Apr 15, 2021

When working in Apex it is not uncommon for us to work with a numeric field that has a 0 scale. To use that with some built in functions we need to use an Integer. To provide an example, let's say we have a field on Account TrainingDays__c. This field always holds a whole number. To use this field with Date.addDays() we need to convert the Decimal value that acct.TrainingDays__c is represented in to an Integer. For this task we have two options: Integer.valueOf(acct.TrainingDays__c) or (Integer)acct.TrainingDays__cI know that there are some differences when working in other languages, but for the given example where the source value is a numeric SObject field: Is there ever a time when one of the methods would throw an exception and the other would not? Is one more performant than the other?Other than code consistency is there any reason to standardize on one approach?

Answered by Ethan Clark

For this specific example, there is this third way:

    Integer i = acct.TrainingDays__c != null ? acct.TrainingDays__c.intValue() : null;

or:

    Integer i = acct.TrainingDays__c != null ? acct.TrainingDays__c.round() : null;

The benefit of using these methods is that they more clearly express what is being done and provide options in how the operation is done (e.g. rounding mode).

Methods like Integer.valueOf make sense when you don't know the type of the argument. As for using casting for this particular conversion, I'm a little surprised it even works. That observation leads to a reason you haven't included in your list: the most important reason to choose is clarity for someone reading the code (e.g. someone other than you changing the code in 2 years’ time). Whether the operation takes 1 microsecond or 10 microseconds is irrelevant if a 5-millisecond query comes soon after. See e.g. we should forget about small efficiencies, say about 97% of the time. 1 for sfdcfox's measuring to get the performance figures rather than speculating. Integer.parseInt(): While operating upon strings, there are times when we need to convert a number represented as a string into an integer type. The method generally used to convert String to Integer in Java is parseInt(). This method belongs to Integer class in java.lang package. It takes a valid string as a parameter and parses it into primitive data type int. It only accepts String as a parameter and on passing values of any other data type, it produces an error due to incompatible types. Integer.valueOf(): This method is a static method belonging to the java.lang package which returns the relevant Integer Object holding the value of the argument passed. This method can take an integer or a String as a parameter. But when the given String is invalid, it provides an error. This method can also take in a character as a parameter but the output will be its corresponding Unicode value. This method will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range.

Integer.parseInt()
Integer.valueOf()

  • It can only take a String as a parameter. It can take a String as well as an integer as parameter. It returns a primitive int value. It returns an Integer object.
  • When an integer is passed as parameter, it produces an error due to incompatible types
  • When an integer is passed as parameter, it returns an Integer object corresponding to the given parameter.
  • This method produces an error(incompatible types) when a character is passed as parameter.
  • This method can take a character as parameter and will return the corresponding unicode.
  • This lags behind in terms of performance since parsing a string takes a lot of time when compared to generating one.
  • This method is likely to yield significantly better space and time performance by caching frequently requested values.
  • If we need the primitive int datatype then Integer.parseInt() method is to be used.
  • If Wrapper Integer object is needed then valueOf() method is to be used



Your Answer

Interviews

Parent Categories