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

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.

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.

java.lang.Number, The abstract class Number is the superclass of platform classes representing All Methods Instance Methods Abstract Methods Concrete Methods short, shortValue() This implementation returns the result of intValue() cast to a byte . on this page tracks web page traffic, but does not change the content in any way. Following is the declaration for java.lang.Number.shortValue() method. public abstract short shortValue() Parameters. NA. Return Value. This method returns the numeric value represented by this object after conversion to type short. Exception. NA. Example. The following example shows the usage of lang.Number.shortValue() method.

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.

Java.lang.Number Class in Java, public abstract class Number extends Object implements Serializable. The abstract class Number is the Method Summary. All Methods Instance Methods Abstract Methods Concrete Methods specified number as a long . short, shortValue() This implementation returns the result of intValue() cast to a byte . Returns: the  The Integer.shortValue() is an inbuilt method of java.lang which returns the value of this Integer in the short type .. Syntax: public short shortValue() Parameters: The method does not take any parameters.

@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();

Java.lang.Integer class in Java, But, Java also provides various numeric wrapper sub classes under the abstract class Number present in java.lang package. This method is used to convert the value of this Number object to the primitive data type specified. Syntax : byte byteValue() short shortValue() int intValue() long longValue() float floatValue()  Following is the declaration for java.lang.Number.intValue() method. public abstract int intValue() Parameters. NA. Return Value. This method returns the numeric value represented by this object after conversion to type int. Exception. NA. Example. The following example shows the usage of lang.Number.intValue() method.

Number Class In Java, Java.lang.Integer class in Java. Integer class is a wrapper class for the toHexString() : Returns the string corresponding to the int value in Another overloaded method containing only String as a parameter, radix is by defalut set to 10. does not exist, that is it does not not return null but a default value supplied by user. The java.lang.Number.shortValue method returns the value of the specified number as a short. This may involve rounding or truncation. Declaration Following is the declaration for java.lang.Number.shortValue method public abstract short shortValue() Parameters NA Return Value

[PDF] Abstract Classes and Interfaces, You can notice that, even concrete methods byteValue() and shortValue() call abstract intValue() method from their body. That means, to use  The abstract class Number is the superclass of platform classes representing numeric values that are convertible to the primitive types byte, double, float, int, long, and short. The specific semantics of the conversion from the numeric value of a particular Number implementation to a given primitive type is defined by the Number implementation

Guide to the Number Class in Java, implement all the abstract methods, the subclass must be defined method: It is possible to define an abstract class that contains no For example, the Object class is concrete, but a java.util.Calendar. #Calendar(). +get(field: int): int. +set(​field: int, value: int): void An interface is similar to an abstract class, but the. Java.lang.Number Class in Java Most of the time, while working with numbers in java, we use primitive data types . But, Java also provides various numeric wrapper sub classes under the abstract class Number present in java.lang package.

Comments
  • It is looks like truth.
  • I don't understand how to connects memory consuming and abstractness
  • abstract is used to leave the implementation of the method to the class that extends Number and since intValue is abstract, the implementation of intValue can be used for byte and short, as they are nothing but shorter versions of int
  • I don't understand full chain. intValue is abstract, the implementation of intValue can be used for byte and short, as they are nothing but shorter versions of int == longValue is abstract, the implementation of longValue can be used for int and byte and short, as they are nothing but shorter versions of long
  • @gstackoverflow true, but byte and short are less used so a default implementation (that is mostly useless) reduces the burden on classes implementing Number.
  • @Boris the Spider I really don't understand this answer(