Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why shortValue() method are concrete but intValue() is abstract into java.lang.Number?

Tags:

java

oop

I have read source code of java.lang.Number and I wondered why

  1. intValue()
  2. longValue()
  3. floatValue()
  4. doubleValue()

are abstract but

  1. shortValue()
  2. 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.

like image 300
gstackoverflow Avatar asked Mar 20 '14 09:03

gstackoverflow


People also ask

What is num in Java?

Number class is the superclass of classes BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, and Short. The Subclasses of Number must provide methods to convert the represented numeric value to byte, double, float, int, long, and short.

What is the parent class for integer byte and short Java lang?

The abstract class Number is the superclass of classes BigDecimal , BigInteger , Byte , Double , Float , Integer , Long , and Short .

What is the immediate superclass of integer class in Java?

Hence, the superclass of Integer is Number, the superclass of Float is Number, and the superclass of Number is Object.


2 Answers

According to the documentation of the Number class, the methods byteValue and shortValue 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 of Number, which would otherwise have been broken by the addition of new abstract methods in the superclass.

like image 106
GOTO 0 Avatar answered Oct 01 '22 08:10

GOTO 0


byte and short are the lesser memory consuming versions, and since intValue is abstract, the implementation of intValue can be used for the byte and short. I think that's why they should have done it like that.

like image 22
anirudh Avatar answered Oct 01 '22 09:10

anirudh