1. Java Numbers Class
•What is a Number class?
•What is wrapper class?
•What are the methods in wrapper class.
4/10/2019 1Jamsher Bhanbhro(F16CS11)
2. Number class
• Using objects instead of primitive data types is known
as wrapper class.
• Converting Primitive data type to object data is known
as Boxing.
• There are six wrapper classes which are (int, byte, float,
short, long, double).
• These wrapper classes are subclasses of an abstract
class Number.
• Boxing is very important to use so it will be done by
using compilers.
• We have to pass value of the parameters to the
constructor of wrapper class.
4/10/2019 Jamsher Bhanbhro(F16CS11) 2
3. How to do Boxing and UnBoxing
public class Boxex{
public static void main(String args[]){
Integer x=11;
// here the value of x is boxed in Integer object
x = x+1;
// here the value of x is unboxed
System.out.println(“”);
}
}
4/10/2019 Jamsher Bhanbhro(F16CS11) 3
4. dTValue()
• This method is used convert the value of Number
Object to the primitive data type.
• These are intValue(), shortValue(),
longValue(),floatValue, doubleValue(),byteValue()
Integer a=11;
S.O.P(a.byteValue());
S.O.P(a.intValue());
S.O.P(a.doubleValue());
etc
4/10/2019 Jamsher Bhanbhro(F16CS11) 4
5. compareTo and equals method
• compareTo this method used to compare the object value with given
parameters of method
int a=11; SOP(a.compareTo(12));
• If the integer arg is equal to object return 0.
• If greater than return 1.
• If less than return -1.
• The equals() method determines whether the Number object that
invokes the method is equal to the object that is passed as an
argument.
Integer x=10;
Integer y=15;
SOP(x.equals(y));
• If equals than answer will be true otherwise answer will be false.
4/10/2019 Jamsher Bhanbhro(F16CS11) 5
6. valueOf(dt obj)
• The valueOf method 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. The method
can take two arguments, where one is a String
and the other is a radix.
• valueOf(int i), valueOf(String s),
valueOf(String s, int radix)
4/10/2019 Jamsher Bhanbhro(F16CS11) 6
7. Other Methods
• parseInt() :This method is used to get the primitive data type of a certain
String. parseXxx() is a static method and can have one argument or two.
• abs() Method : float abs(float f)
• ceil() :double ceil(double d):The method ceil gives the smallest integer that
is greater than or equal to the argument.
• floor(): The method floor gives the largest integer that is less than or equal
to the argument.
• double floor(double d), double floor(float f)
• rint(): The method rint returns the integer that is closest in value to the
argument. double rint(double d)
• round(): The method round returns the closest long or int, as given by the
methods return type. long round(double d)
• exp(): The method returns the base of the natural logarithms, e, to the
power of the argument. double exp(double d)
4/10/2019 Jamsher Bhanbhro(F16CS11) 7
8. Methods
• log(): The method returns the natural logarithm of the argument.
double log(double d)
• pow() : The method returns the value of the first argument raised to
the power of the second argument. double pow(double base, double
exponent)
• sqrt() : The method returns the square root of the argument.
for example: double sqrt(double d).
• sin(), cos() , tan() syntax: double tan(double d)
• asin() , acos() , atan() syntax: double atan(double d)
• toDegrees() : The method converts the argument value to degrees.
double toDegrees(double d)
• toRadians():
4/10/2019 Jamsher Bhanbhro(F16CS11) 8
9. Tasks
• Use all above number class methods in your
program.
• Make a scientific calculator
4/10/2019 Jamsher Bhanbhro(F16CS11) 9