I have a class that starts with:
abstract class CalcValuesGeneric<E> {
E will be Integer, int, Float or float. (For now, but always a Number.)
What is the best way to determine which of the two is lower?
I have a class that starts with:
abstract class CalcValuesGeneric<E> {
E will be Integer, int, Float or float. (For now, but always a Number.)
What is the best way to determine which of the two is lower?
On 7/29/2020 11:19 AM, Cecil Westerhof wrote:
I have a class that starts with:
abstract class CalcValuesGeneric<E> {
E will be Integer, int, Float or float. (For now, but always a Number.)
What is the best way to determine which of the two is lower?
T extends Number?
Example:
public class NumFun {
public static class Demo<T extends Number> {
public boolean gt(T a, T b) {
return a.doubleValue() > b.doubleValue();
}
}
public static void main(String[] args) {
Demo<Double> dd = new Demo<>();
System.out.println(dd.gt(1.0, 0.0));
System.out.println(dd.gt(1.0, 1.0));
System.out.println(dd.gt(0.0, 1.0));
Demo<Integer> di = new Demo<>();
System.out.println(di.gt(1, 0));
System.out.println(di.gt(1, 1));
System.out.println(di.gt(0, 1));
}
}
Eric Sosman <esosman@comcast-dot-net.invalid> writes:
On 7/29/2020 11:19 AM, Cecil Westerhof wrote:
I have a class that starts with:
abstract class CalcValuesGeneric<E> {
E will be Integer, int, Float or float. (For now, but always a Number.)
What is the best way to determine which of the two is lower?
Aside: How can you get `<E>' to be `<int>' or `<float>'? Is that
something new? I'm still on Java 8, but the JLS for 14 says "Seq<int>
is illegal, as primitive types cannot be type arguments." (4.5)
You are completely right, I was not thinking straight. :'-(
I have a class that starts with:
abstract class CalcValuesGeneric<E> {
E will be Integer, int, Float or float. (For now, but always a Number.)
What is the best way to determine which of the two is lower?
On 7/29/2020 11:19 AM, Cecil Westerhof wrote:
I have a class that starts with:
abstract class CalcValuesGeneric<E> {
E will be Integer, int, Float or float. (For now, but always a Number.)
What is the best way to determine which of the two is lower?
Aside: How can you get `<E>' to be `<int>' or `<float>'? Is that something new? I'm still on Java 8, but the JLS for 14 says "Seq<int>
is illegal, as primitive types cannot be type arguments." (4.5)
On 7/29/2020 2:42 PM, Cecil Westerhof wrote:
Eric Sosman <esosman@comcast-dot-net.invalid> writes:
On 7/29/2020 11:19 AM, Cecil Westerhof wrote:
I have a class that starts with:
abstract class CalcValuesGeneric<E> {
E will be Integer, int, Float or float. (For now, but always a Number.) >>>> What is the best way to determine which of the two is lower?
Aside: How can you get `<E>' to be `<int>' or `<float>'? Is that
something new? I'm still on Java 8, but the JLS for 14 says "Seq<int>
is illegal, as primitive types cannot be type arguments." (4.5)
You are completely right, I was not thinking straight. :'-(
Probably a common miswording.
class CalcValuesGeneric<E> {
void something(E v)
...
cvg.something(x);
E can be Integer or Float but not int or float.
But x can be int and float as well due to auto boxing.
Arne Vajhøj <arne@vajhoej.dk> writes:
On 7/29/2020 2:42 PM, Cecil Westerhof wrote:
Eric Sosman <esosman@comcast-dot-net.invalid> writes:
On 7/29/2020 11:19 AM, Cecil Westerhof wrote:
I have a class that starts with:
abstract class CalcValuesGeneric<E> {
E will be Integer, int, Float or float. (For now, but always a Number.) >>>>> What is the best way to determine which of the two is lower?
Aside: How can you get `<E>' to be `<int>' or `<float>'? Is that >>>> something new? I'm still on Java 8, but the JLS for 14 says "Seq<int> >>>> is illegal, as primitive types cannot be type arguments." (4.5)
You are completely right, I was not thinking straight. :'-(
Probably a common miswording.
class CalcValuesGeneric<E> {
void something(E v)
...
cvg.something(x);
E can be Integer or Float but not int or float.
But x can be int and float as well due to auto boxing.
But I do not want auto boxing. The particular case I am working with
shows that int can take 40% less CPU and memory when using Integer.
I will find another way. Thanks.
Arne Vajhøj <arne@vajhoej.dk> writes:
On 7/29/2020 2:42 PM, Cecil Westerhof wrote:
Eric Sosman <esosman@comcast-dot-net.invalid> writes:
On 7/29/2020 11:19 AM, Cecil Westerhof wrote:
I have a class that starts with:
abstract class CalcValuesGeneric<E> {
E will be Integer, int, Float or float. (For now, but always a Number.) >>>>> What is the best way to determine which of the two is lower?
Aside: How can you get `<E>' to be `<int>' or `<float>'? Is that >>>> something new? I'm still on Java 8, but the JLS for 14 says "Seq<int> >>>> is illegal, as primitive types cannot be type arguments." (4.5)
You are completely right, I was not thinking straight. :'-(
Probably a common miswording.
class CalcValuesGeneric<E> {
void something(E v)
...
cvg.something(x);
E can be Integer or Float but not int or float.
But x can be int and float as well due to auto boxing.
But I do not want auto boxing. The particular case I am working with
shows that int can take 40% less CPU and memory when using Integer.
I will find another way. Thanks.
But if this is performance critical then why not just drop the
generic stuff and write specific code for the types?
You could try Scala. It has type specialization that allows you to
prevent boxing overhead when using generics with primitive type
parameters.
Arne Vajhøj <arne@vajhoej.dk> writes:
But if this is performance critical then why not just drop the
generic stuff and write specific code for the types?
Because I want to show the differences between using
Float/float/Integer/int and different implementations thereof.
Arne Vajhøj <arne@vajhoej.dk> writes:
But if this is performance critical then why not just drop the
generic stuff and write specific code for the types?
Because I want to show the differences between using
Float/float/Integer/int and different implementations thereof.
On 7/30/2020 5:33 AM, Cecil Westerhof wrote:
Arne Vajhøj <arne@vajhoej.dk> writes:
But if this is performance critical then why not just drop the
generic stuff and write specific code for the types?
Because I want to show the differences between using
Float/float/Integer/int and different implementations thereof.
Is this a "practical problem," or a "teaching exercise?" If the
former, I'd be interested to know how it arises -- needing to compare
one arbitrary Number to another (BigDecimal vs. AtomicLong?) seems not
out of the question, but surely unusual.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 296 |
Nodes: | 16 (2 / 14) |
Uptime: | 33:12:56 |
Calls: | 6,648 |
Calls today: | 3 |
Files: | 12,193 |
Messages: | 5,328,709 |