Java Tut – Numbers

Normally when we work with numbers, we use primitive data types such as byte,int,long,double, etc

e.g:
int i=400;
float gpa=13.65;
byte mask=0xaf;
In develoment we need to use objects instead of primitive data types.So java provides wrapper classes

All the weapper classes are the subclasses of the abstract class Number.
Number Subclasses
The object of the wrapper class contains or wraps it respective primitive data type.converting primitive data types into object called boxing, and this is taken care by the compiler.so while passing the wrapper class you just need to pass the value of the primitive data type to the constructor of the wrapper class.

And the wrapper object will be converted back to a primitive data type and this process is called un boxing.The Number class is part of the java.lang package.

example:

output:
15
When x is assigned integer value the compiler boxes the integer because x is integer object.Later, x is unboxed so that they can be added as integer.
Number Methods:
Here is the list of the instance methods that all the subclasses of teh Number class implement:

xxxxValue() method:
Syntax:
here is a separate method for each primitive data type:

example:

Output:

compareTo() method:
Two different types cannot be compared, both the argument and the Number object invoking(ডাকা) should be the same type:
Syntax:

Parameters:
referenceName – this could be a Byte,Double,Integer,Float,Long or Short
Return Value:
– If the integer in referenceName is equal to argument object then 0 is returned.
– If the Integer is less than the argument than 1 is returned.
– If the Integer is greater than the argument than -1 is returned

Example:

Output:

equals() Method:
Syntax:
public boolean equals(Object o)
Parameters:
Here is the detail of parameters:
o — is any object
Return Value:
The method returns True or false if the arguments 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.
Example:

Output:

 

Java Max() method:
The method gives the maximum of the two arguments.The arguments can be int,float,long,double
Syntax:
This method has following variants:

Parameters: This method accepts any primitive data type as parameter
Return value: This method returns the maximum of the two arguments
Example:

Output:

exp():
Syntax:

double exp(double d)
example:

out:

toRadians()
syntax:

 

Parameter: d  a double data type
Return value: This method returns a double value
example:

output:


floor() method/ceil method:

This method floor gives the largest integer that is less than or equal to the argument.

This method ceil gives the smallest integer that is greater than or equal to the argument.
Syntax:

Parameter:A double or flaot primitive data type
Return Value: This method returns the largest integer that is less than or equal to the argument.Returned as double.
example:

output:

toString() method:

This method is used to get a string object representing the value of the number object.

If the method takes a primitive data type as an argument, then the String object representing the primitive data type value is returned.

If the method takes two arguments,then a String representation of the first argument in the radix specified by the second argument will be returned.

i — An int for which string representation would be returned

valueOf():

output:

parseInt() or parseXXXX():

This method is used to get the primitive data type of a certain String.parseXXX() is a static method can have one argument or two.

Parameters:
s — This is a string representation of deciaml
radix – This would be used to convert String s into integer

Return Value:
parseInt(String s): This returns an integer(decimal only)
parseInt(int i): This returns an Integer, given a string representation of decimal,binary,octal or hexadecimal(radix equals 10,2,8 or 16 respectively) numbers as input.