I am sure you can use numbers now. At least you know about int, long, double etc. So these are primitive types. Take a few minutes to review Data Types.

**Primitive Types and Wrapper Types**

Primitive types can be declared like this:

int score = 90; float value = 384.03f; char initial = 'K'; long total = 700834L;

Java also provides us with Wrapper Classes. Although these are also numbers, they are actually objects. They include:

- Byte
- Short
- Integer
- Long
- Float
- Double

These are wrapper classes and they derive from the Number class. This is part of java.lang package. Therefore, you can create objects from these classes. Object of these classes contains the equivalent primitive type. So it kind of ‘wraps’ the corresponding primitive. Hence the name wrapper class.

You can convert primitive type to wrapper type (object). This is called boxing. Similarly, you can convert wrapper object back to the corresponding primitive type. This is called unboxing.

Let’s take an example of boxing. We also look at unboxing as well

int a = 10; //a is a primitive type, int Integer b = 10; //b is a wrapper object, Integer (boxing) b = b + 20; //this causes be to become a primitive type int (unboxing)

From the code above, the can deduce that:

- Boxing creates a wrapper object from a primitive type
- Unboxing creates a primitive type from a wrapper object

Oftentimes, you will only need to use primitive types. However, there are few occasion you probably will require wrapper objects.

**Method of the Number Classs**

We have some methods in the Number class that you need to know. So, let’s consider some of them.

SN. | Method and Description |
---|---|

1 | intValue(), shortValue(), floatValue(), byteValue()
Converts the value of Number object to the primitive equivalent |

2 | compareTo()
Used to compare the Number object to another number given as argument. |

3 | equals()
Used to check whether the number object is equal to the given argument. |

4 | valueOf()
Used to create an Integer object holding the value of the specified primitive. |

5 | toString()
Used to create a String object representing the value of a specified int or Integer. |

6 | parseInt()
This method is used to get the primitive data type of a certain String. |

7 | abs()
Used to return the absolute value of the argument. |

8 | ceil()
Used to return the smallest integer that is greater than or equal to the argument. Returned a double. |

9 | floor()
Used to return the largest integer that is less than or equal to the argument. Returns a double. |

10 | rint()
Used to return the integer that is closest in value to the argument. Returns a double. |

11 | round()
Rounds a number to the closest long or int, as indicated by the method’s return type to the argument. |

12 | min()
Takes two arguments and return the smaller of the two. |

13 | max()
Takes two arguments and return the larger of the two. |

14 | exp()
Used to return the base of the natural logarithms, e, to the power of the argument. |

15 | log()
Used to return the natural logarithm of the given argument. |

16 | pow()
Takes two arguments and the value of the first argument raised to the power of the second argument. |

17 | sqrt()
Used to return the square root of the given argument. |

18 | sin()
Used to return the sine of the given double value. |

19 | cos()
Used to return the cosine of the given double value. |

20 | tan()
Used to return the tangent of the given double value. |

21 | asin()
Used to return the arcsine of the given double value. |

22 | acos()
Used to return the arccosine of the given double value. |

23 | atan()
Used to return the arctangent of the given double value. |

24 | atan2()
Used to convert the rectangular coordinates (x, y) to equivalent polar coordinate (r, theta) and returns theta. |

25 | toDegrees()
Used to convert an given argument to degrees. |

26 | toRadians()
Converts the argument to radians. |

27 | random()
Used to return a single random number. |

Table 1.0: List of Methods of the Number class

I would therefore recommend you try some of these methods to see how they work. I have provided the code below the demonstrates some of them.

//Testing the Methods of the Number Wrapper class public class Tester { public static void main(String args[]) { Integer x = 10; Integer y = 20; Integer z = 30; Float w = 40.45f; int a = Integer.parseInt("4"); //creates in int value int x1 = x.intValue(); //unboxes x(Integer) to an int x1 int y1 = y.intValue(); //unboxed y(Integer) to an int y1 String strx = x.toString(); //Creates a string equivalent of x int c = Integer.min(4,5); //returns the smaller of the two numbers int d = Integer.max(4, 5); //returns the larger of the two numbers } }