Java Numbers With Examples

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)

  • AtomicInteger
  • AtomicLong
  • BigDecimal
  • BigInteger
  • Byte
  • Double
  • DoubleAccumulator
  • DoubleAdder
  • Float
  • Integer
  • Long
  •  LongAccumulator
  • LongAdder
  • Short

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.

Boxing

The automatic conversion from primitive data type to its wrapper class objects is called Autoboxing.

// autoboxing
Integer intObj = 12;

Unboxing

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.

Methods of Number class:

There are several methods of Number class, and the abovementioned wrapper classes being its child, are available for their specific implementation.

byteValue() Method:

Name: bytevalue()
Signature: Object.byteValue()
Description: Returns the value of this Object as a byte after a narrowing primitive conversion.

Example:

public class MyNumbers {
   public static void main(String[] args) {
       Integer myInt = 4343645;
       System.out.println("The byteValue is " + myInt.byteValue());  
   }
}

Output:

The byteValue is 93

compareTo() Method:

Name: compareTo()
Signature: Object.compareTo(ObjectPassAsReference)
Description: It compares two objects numerically. The value 0 if this Object is equal to the argument Object. A value less than 0 if this Object is numerically less than the argument Object, and a value greater than 0 if this Object is numerically greater than the argument Object(signed comparison).

Example:

public class MyNumbers {
        public static void main(String[] args) {
            Integer myInt = 4343645;
            System.out.println("The result of compareTo is " + myInt.compareTo(5656));  
       }
}

Output:

The result of compareTo is 1

floatValue() and shortValue() Method:

Name: floatValue()
Signature: Object.floatValue()
Description: Returns the float value of this Object.
Name: shortValue()
Signature: Object.shortValue()
Description: Returns the mentioned value as short.

Example:

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
	}
}

Output:

Widening:4343645.0
Narrowing:18269

toString() Method:

Name: toString()
Signature: Object.toString()
Description: Returns a String object representing this Object's value. The value is converted to signed decimal representation and returned as a string, exactly as if the object value were given as an argument.

Example:

public class MyNumbers {
	public static void main(String[] args) {
		Integer myInt = 4343645;
		System.out.println("The string version is " + myInt.toString());
	}
}

Output:

The string version is 4343645

equals() Method:

Name: equals
Signature: Object.equals(ObjectPassAsReference)
Description: Compares this object to the specified object. The result is true if and only if the argument is not null and is the same type object that contains the same numerical value as this object.

Example:

public class MyNumbers {
	public static void main(String[] args) {
		Integer myInt = 4343645;
		System.out.println("The method equals check output: " + myInt.equals(4343645));
	}
}

Output:

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.