In real life, we are surrounded by numeric information. In order to serve our purpose while programming, we need a range of data type which are capable of storing and can be processed easily.
As discussed earlier, we are aware of the primitive data types such as byte, short, int, long, float, double. Usually, we use these primitive data types for basic computations.
There are several examples of it.
byte myByte = 12;
short myShort = 121;
int myInt = 1212;
float myFloat = 12.12f;
But primitive data types are not objects, so Java provides classes alternative to these primitive data types which can be used to avoid primitive data type usage and making the code strictly object-oriented. These classes are called wrapper class.
The abstract class Number is the parent class of the wrapper classes representing numeric values. There are several directly known child classes like (stated in alphabetical order)
Among all, we are currently limiting our discussion to those wrapper classes, which are used more frequently. The primitive data type and its corresponding wrapper class is listed below:
Primitive Data Type | Wrapper Class |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
boolean | Boolean |
An important thing to note is that, these classes are convertible to their respective primitive types i.e byte, short, int, long, float, and double and vice versa.
The automatic conversion from primitive data type to its wrapper class objects is called Autoboxing.
// autoboxing
Integer intObj = 12;
The automatic conversion from wrapper class objects to its primitive data type is called Unboxing.
// Unboxing
Integer intObject = 12;
int primitiveInt = intObject;
Among these wrapper classes, the conversion is similar to narrowing and widening which is possible while converting between primitive data types. Hence, the conversion might even result in loss/ tampering(while narrowing) of data.
Let us understand the concept of narrowing and widening.
Widening: Water from a 4-litre container can be transferred easily into an 8-litre container. We are transferring out water from smaller to bigger capacity. So transferring data from lower size data type to its compatible higher size data type, it is called widening.
Narrowing: When water is transferred from an 8-litre container to a 4-litre container, we will see that only 4 litres of water was transferred and the rest of it is lost. We are transferring out water from bigger to smaller capacity. So transferring data from higher size data type to its compatible lower size data type, it is called narrowing. This might result in data loss as well.
There are several methods of Number class, and the abovementioned wrapper classes being its child, are available for their specific implementation.
public class MyNumbers {
public static void main(String[] args) {
Integer myInt = 4343645;
System.out.println("The byteValue is " + myInt.byteValue());
}
}
The byteValue is 93
public class MyNumbers {
public static void main(String[] args) {
Integer myInt = 4343645;
System.out.println("The result of compareTo is " + myInt.compareTo(5656));
}
}
The result of compareTo is 1
public class MyNumbers {
public static void main(String[] args) {
Integer myInt = 4343645;
System.out.println("Widening:" + myInt.floatValue()); //Widening
System.out.println("Narrowing:" + myInt.shortValue()); //Narrowing
}
}
Widening:4343645.0
Narrowing:18269
public class MyNumbers {
public static void main(String[] args) {
Integer myInt = 4343645;
System.out.println("The string version is " + myInt.toString());
}
}
The string version is 4343645
public class MyNumbers {
public static void main(String[] args) {
Integer myInt = 4343645;
System.out.println("The method equals check output: " + myInt.equals(4343645));
}
}
The method equals check output: true
These are just a few examples to give you a glimpse of so many things that is possible with Java Numbers. In the next class, we will understand about Java Character.