JAVA BEGINNERS TUTORIAL – Dealing With Numbers 1

From time to time I need to have a peek on the basics of any language I know for that reason I am wrting these toturial blogs. any programming language must have to Work with numbers. in java is easy as we use primitive data types such as byte, int, long, double, etc. 


In development, There are reasons to use objects in place of primitives, and the Java platform provides wrapper classes for each of the primitive data types. These classes “wrap” the primitive in an object. All of the numeric wrapper classes (Integer, Long, Byte, Double, Float, Short)  are subclasses of the abstract class Number:

Java Classes

This wrapping is taken care of by the compiler, the process is called boxing. So when a primitive is used when an object is required, the compiler boxes the primitive type in its wrapper class. Similarly, the compiler unboxes the object to a primitive as well. The Number is part of the java.lang package.

Here is an example of boxing and unboxing:

When x is assigned integer values, the compiler boxes the integer because x is integer objects. Later, x is unboxed so that they can be added as integers.

Number Methods:

Here is the list of the instance methods that all the subclasses of the Number class implement:


The method converts the value of the Number Object that invokes the method to the primitive data type that is returned from the method.

Here is a separate method for each primitive data type:



The method compares the Number object that invoked the method to the argument. It is possible to compare Byte, Long, Integer, etc. 

However, two different types cannot be compared, both the argument and the Number object invoking the method should be of same type.

  • referenceName — This could be a Byte, Double, Integer, Float, Long or Short.
  • If the Integer is equal to the argument then 0 is returned.
  • If the Integer is less than the argument then -1 is returned.
  • If the Integer is greater than the argument then 1 is returned.



The method determines whether the Number Object that invokes the method is equal to the argument.

  • The methods returns True if the argument is not null and is an object of the same type and with the same numeric value. There are some extra requirements for Double and Float objects that are described in the Java API documentation.



Returns the relevant Number Object holding the value of the argument passed. The argument can be a primitive data type, String, etc. This method is a static method.

  • i — An int for which Integer representation would be returned. 


    • valueOf(int i): This returns an Integer object holding the value of the specified primitive.
  • s — A String for which Integer representation would be returned. 


    • valueOf(String s): This returns an Integer object holding the value of the specified string representation.
  • radix — This would be used to decide the value of returned Integer based on passed String. 


    • valueOf(String s, int radix): This returns an Integer object holding the integer value of the specified string representation, parsed with the value of radix.


More Methods in thenext blog post