I have confusion regarding the following parameters for a numeric method
int abs(int) long abs(long) double abs(double)
Abs number (number)
which approach is best and what is the difference between the two approaches?
If we use the parent class reference, no overload is needed. Is there a problem using the second style.
When should we go for the first approach and when for the second?
The main difference: your option 1 works with primitive types.
While option 2 works for reference types (aka objects).
, - abs(Number). : , !
abs(Number)
: 1 - , int, long, float, double. , , , .
2, , , , ... Number ( ).
№ 2 , :
Number
intValue()
longValue()
doubleValue()
, , , generics, ( downcasting).
<N extends Number> N abs(N number) { // choose an actual primitive type to use, e.g.: number.doubleValue() // TODO: create a new instance of N if number.doubleValue() is negative }
, , , .
Zen ,
, . .
It also avoids the compiler creating objects, so I assume it is faster
Source: https://habr.com/ru/post/1663149/More articles:Confirm child login components to send using Vee-Validate and vue js 2 - javascriptvee-validate error object - _vm.errors undefined - vue.jsSVG image not showing - htmlUsing Google Cloud Machine Learning to Predict Using the Retrained Inception Model - tensorflowHow to ask user to change default applications - androidhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1663150/how-to-feed-multiple-files-to-a-background-process-using-makefile&usg=ALkJrhjJUEipJvGSDdqFweZLNR-pyqZcQACrap Wrap Function in Java Callable - javaquery with similar offer in scalikejdbc - scalikejdbcWhat task is returning and why is the status of the task RanToCompletion - multithreadingMicro Service vs Nano Service? - web-servicesAll Articles