I have read source code of java.lang.Number
and I wondered why
intValue()
longValue()
floatValue()
doubleValue()
are abstract but
shortValue()
byteValue()
a concrete.
source code:
public abstract class Number implements java.io.Serializable {
public abstract int intValue();
public abstract long longValue();
public abstract float floatValue();
public abstract double doubleValue();
public byte byteValue() {
return (byte)intValue();
}
public short shortValue() {
return (short)intValue();
}
private static final long serialVersionUID = -8742448824652078965L;
}
Why java founders have made it so?
I don't see big differencies between these method. Its seem as related.
P.S.
from Long class:
public byte byteValue() {
return (byte)value;
}
public short shortValue() {
return (short)value;
}
public int intValue() {
return (int)value;
}
public long longValue() {
return (long)value;
}
public float floatValue() {
return (float)value;
}
public double doubleValue() {
return (double)value;
}
from Integer class
public byte byteValue() {
return (byte)value;
}
public short shortValue() {
return (short)value;
}
public int intValue() {
return value;
}
public long longValue() {
return (long)value;
}
public float floatValue() {
return (float)value;
}
public double doubleValue() {
return (double)value;
}
Hence we are see a lot of same code. I think copy paste
development is bad but I think that Java founders have reasons for this.
byte
andshort
are the lesser memory consuming versions, and sinceintValue
isabstract
, the implementation ofintValue
can be used for the byte and short. I think that's why they should have done it like that.@Anirudh sir, Small confirmation as you said that byte and short method are consuming the lesser memory to store the value, but it is not the right way to perform an operations by taking the same variable.. Because the byte variable will return you the negative value.
//As per (step-1)Integer is the wrapper class object which is stored with 100000 value above range of byte. But the memory usage is less compare to int memory consumstion or range. because the int range is up to 600000 or smthng. remaining is waste of memory(apart from 100000), so you said that the int value(100000) if we convert to byte like(step-2) the more usage of memory will get reduced. But the value get stored inside the "b" variable is negative value. So here we cannot perform any operation with the original value(100000). Please suggest me if i am in wrong thought.
step-1:- Integer io=new Integer(100000);
step-2:- byte b=io.byteValue();
According to the documentation of the Number class, the methods
byteValue
andshortValue
were added first in JDK1.1. This is unlike the other "Value" methods which were already available in the very first JDK release. My assumption is that those two methods were made concrete in order to keep compatibility with existing (also non-standard) subclasses ofNumber
, which would otherwise have been broken by the addition of new abstract methods in the superclass.