The Best Technical Blog Ever

twitterrss

Numbers Class in java

For using numbers, most of the time we use primitive data types like int, float, etc. In some cases, we need to use objects instead of primitive data types. For this, Java provides wrapper classes for each primitive data type. 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.


Wrapping concept is done by compiler whenever it is needed and the process is called boxing. If we use primitives instead of the expected object, the compiler boxes the primitive in its wrapper class for us(Autoboxing). In the same way, if you use a number object for the expected primitive, the compiler unboxes the object. Number is part of the java.lang package.We use Number object rather than a primitive in the following cases:


  • for a method that expects an object as an argument.

  • for using constants like MIN_VALUE and MAX_VALUE, that provide the upper and lower bounds of the data type.

  • for using class methods to convert values to and from other primitive types.

number class


Example:
number class

Number Methods:

Sl.No Methods Description
1 int intValue()
double doubleValue()
Java Converts the value of this Number object to primitive data type returned.
2 int compareTo() Compares this Number object to the argument.
3 boolean equals(Object obj) Determines whether this number object is equal to the argument.
4 parseInt() Returns an integer (decimal only).
5 toString() Returns a String object representing the value of this Integer.
6 valueOf() Returns an Integer object holding the value of the specified primitive.
7 boolean equals(Object obj) Determines whether this number object is equal to the argument.
8 random() Returns a randomly selected number between 0.0 and 1.0.


Basic Math Methods:

Sl.No Math Methods Description
1 abs() Returns the absolute value of the argument.
2 double ceil(double d) Returns the smallest integer that is greater than or equal to the argument. Returned as a double.
3 double floor(double d) Returns the largest integer that is less than or equal to the argument. Returned as a double.
4 double rint(double d) Returns the integer that is closest in value to the argument. Returned as a double.
5 int round(float f) Returns the closest int, as indicated by the method's return type, to the argument.
6 int min(int arg1, int arg2) Returns the smaller of the two arguments.
7 float max(float arg1, float arg2) Returns the larger of the two arguments.


Exponential and Logarithmic Methods:

Sl.No Exponential and Logarithmic Methods Description
1 double exp(double d) Returns the base of the natural logarithms, e, to the power of the argument.
2 double log(double d) Returns the natural logarithm of the argument.
3 double pow(double base, double exponent) Returns the value of the first argument raised to the power of the second argument.
4 double sqrt(double d) Returns the square root of the argument.


Trigonometric Methods:

Sl.No Trigonometric Methods Description
1 double sin(double d) Returns the sine of the specified double value.
2 double cos(double d) Returns the cosine of the specified double value.
3 double tan(double d) Returns the tangent of the specified double value.
4 double atan2(double y, double x) Converts rectangular coordinates (x, y) to polar coordinate (r, theta) and returns theta.
5 double toDegrees(double d) Converts the argument to degrees.
6 double toRadians(double d) Converts the argument to radians.
Keypoints to remember in java

1. Java is a high level, robust, secured , platform independent and object oriented programming.... more info

Back to Basics:- Java Syntax

In this session, you are going to refresh some basic set of rules(java syntax), defining how a ... more info

Total Visits
Traffic feed status