Last updated February 1, 2021 by
The class number in Java is an abstract class that is a superclass of the classes Byte, Short, Integer, Long, Float, and Double.
The Byte, Short, Integer, Long, Float, and Double classes are the envelope classes most commonly used to represent numeric values.
All enclosures of numeric type are inherited from the abstract class Number.
The number of the Java class and its subclasses are defined in the java.lang file. It was added in Java version 1.0.
Declaration of class number
The abstract class number extends the class Object and implements the interface Serializable. The general statement of the number class is as follows:
The public abstract class Number
extends object
and implements the serializable
Classes such as AtomicInteger, AtomicLong, BigDecimal, BigInteger, DoubleAccumulator, DoubleAdder, LongAccumulator, and LongAdder also implement the Number class.
A hierarchical diagram of the Number class in Java is shown in the following figure.
Number class methods in Java
The Number class defines some useful methods that return an object value in each of several number formats. These methods are as follows:
byteValue() : This method returns the value of the object as a byte, which may result in rounding or truncation. In other words: It converts the calling object to a byte value. The called object can be an object of the class Byte, Short, Integer, Long, Floating or Double.
2. shortValue() : This method returns the value of the object as a reduction, which may include rounding or truncation. Simply put: It converts the calling object to a short value.
3. abstract intValue() : It returns the value of the object as short, which may be due to rounding or truncation. That is, it converts the calling object into an int. value.
4. abstract longValue() : Returns the value of the object as long as it can be due to rounding or truncation. It is used to convert the calling object to a long value.
5. abstract floatValue() : This method returns the value of the object as a floating point, which may be due to rounding.
6. Abstract function doubleValue() : This method returns the value of the object to the extent required for rounding.
All these methods provided by the abstract number class in Java are available for the classes Byte, Short, Integer, Long, Float, and Double.
Example of a program based on the number class in Java
1. Let’s take an example program where we learn interval constants for integers, interval constants for floating point, and some other useful interval constants. Look at the source code.
Program source code 1 :
public class Test
[
public static void main(String[] args)
[
// Integer interval constants :
System.out.println(byte range: +byte.MIN_VALUE+ to +byte.MAX_VALUE);
System.out.println(short range: +short.MIN_VALUE+ to +short.MAX_VALUE);System.out.println(integer range: +Integer.MIN_VALUE+ to +Integer.MAX_VALUE);
System.out.println(long range: +Long.MIN_VALUE+ to +Long.MAX_VALUE) ;
// Floating-comma range constants.
System.printout.(Float: +MIN.value.to.+.MAX.value);
System.printout.(Float: +Double.MIN.value.to.+.Double.MAX.value);
// Other useful constants.
System.out.println(Math.PI: +Math.PI);
System.out.println(Math.E: +Math.E);
}
}
Go away:
Byte range : -128 to 127Short range : -32768 to32767Full range : -2147483648 to2147483647Long range : -9223372036854775808 до9223372036854775807Диапазон плавающих : -9223372036854775807Диапазон плавающих: -2147483648 to2147483647 : 1.4E-45 to 3.4028235E38
Doubling the range : 4.9E-324 to 1.7976931348623157E308
Math.PI : 3.141592653589793
Mathematics.E : 2.718281828459045
2. Let’s take an example of a program where we get the value of a Double object after converting to Byte, Short, Int, Long, Float and Double.
Program source code 2 :
public class Test
[
public static void main(String[] args)
[
// Create an object of the class Double with the value 25,10
Double d = new Double(25,10);// Convert this object Double(Number) into different values of the primitive data type.
byte b = d.byteValue();
short s = d.shortValue();
int i = d.intValue();
long l = d.longValue();
float f = d.floatValue();
double d1 = d.doubleValue() ;
Print.system(valeurobyte: + b);Print.system(short.value: + s);Print.system(long.value: + i);Print.system(long.value: + l);Print.system(floating.value: + f);Print.system(double.value: + d1);}}}
Output:
byteValue: 25
shortValue: 25
intValue: 25
longValue: 25
floatValue: 25,1
doubleValue : 25.1
We hope this lesson has covered some important topics related to the Java Number class with sample programs. I hope you understand this simple topic in Java.
If you like this tutorial, please share it on social media.
Thanks for reading.
frequently asked questions
What is a Java number?
The abstract class Number is a superclass of platform classes that represent and convert numeric values to the primitive types byte, double, float, int, long and short.
Is the number a Java method?
Therefore, in a Java application, the easiest way to determine whether a string is a number or not is to use the isNumber() method, which checks whether a string is a valid number in Java. … However, numbers beginning with 0. are considered decimal numbers. The return of the empty and null/empty string is false.
What is a class method in Java?
Class methods are methods that are called on the class itself, not on a particular instance of an object. … Many built-in standard Java classes (e.g. Math) have static methods (e.g. Math. abs(int value)) that are used in many Java programs.
Related Tags:
java number class example, number wrapper class in java, integer class java, wrapper class methods in java, java numbers class methods, autoboxing in java, java string to number, java long