Java Numbers Class

Published by Admin on

Java Numbers Class - intechnologies.in

When we use Java Numbers Class in our java program, we use primitive data types such as byte, int, long, double, etc.

Example

int i = 1000;
float f = 13.65;
double d = 0xaf;

Wrapper Classes

Integer, Long, Double, Byte, Float, Short are sub-classes of the abstract class Number.

Boxing :- It is converting primitive datatype into object is called boxing.

Unboxing :- It is converting an object of a wrapper type into its corresponding primitive value is called unboxing.

Example of boxing and unboxing

public class Test {
   public static void main(String args[]) {
      Integer k = 7; // boxes int to an Integer object
      k =  k + 10;   // unboxes the Integer to a int
      System.out.println(k); 
   }
}
//
Output
17

Number Methods

Sr.No.MethodDescription
1.xxxValue()It is converts the value of this Number object to the xxx data type and returns it.
2.compareTo()This method compares this Number object to the argument.
3.equals()This method checks whether this Number object is equal to the argument.
4.valueOf()This method returns an Integer object holding the value of the specified primitive.
5.toString()This method 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 that contains numeric value.
7.abs()Get the absolute value of the argument.
8.ceil()Get the smallest integer that is greater than or equal to the argument. Its returned as a double.
9.floor()Get the largest integer that is less than or equal to the argument. Its returned as a double.
10.rint()Get the integer that is closest in value to the argument. Its returned as a double.
11.round()Get the closest long or int, as indicated by the method’s return type to the argument.
12.min()Get the smaller of the two arguments.
13.max()Get the larger of the two arguments.
14.exp()Get the base of the natural logarithms, e, to the power of the argument.
15.log()Get the natural logarithm of the argument.
16.pow()Get the value of the first argument raised to the power of the second argument.
17.sqrt()Get the square root of the argument.
18.sin()Get the sine of the specified double value.
19.cos()Get the cosine of the specified double value.
20.tan()Get the tangent of the specified double value.
21.asin()Get the arcsine of the specified double value.
22.acos()Get the arccosine of the specified double value.
23.atan()Get the arctangent of the specified double value.
24.atan2()Its converts rectangular coordinates (x, y) to polar coordinate (r, theta) and returns theta.
25.toDegrees()Its converts the argument to degrees.
26.toRadians()Its converts the argument to radians.
27.random()Get a random number.

Question :-

  • What is wrapper classes in Java?
  • What is Java Numbers Class?
  • How to use random() in Java?


0 Comments

Leave a Reply

Facebook
Twitter
Pinterest
LinkedIn
Instagram
Follow by Email
YouTube
Telegram
RSS
WhatsApp