×

Java Numbers

We use primitive data types like byte, int, long, double, etc to work with the numbers, When we need objects, we use wrapper class like Integer, Double, Long, Byte, etc.

All the wrapper classes (Integer, Long, Byte, Double, Float, Short) are subclasses of the abstract class Number.

The wrapper class has different wraps which convert the respective primitive data types to its corresponding object types. It is done by the compiler.

We pass the primitive type and compiler convert it into there object type, this is called boxing. The conversion of the wrapper class object to the primitive is called unboxing.

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

Output:

20

Methods of Number class:

xxxValue():- It Converts the value of this Number object to the xxx data type and returns it.

CompareTo():- It Compares this Number object to the argument.

Equals():- It Determines whether this number object is equal to the argument.

ValueOf():- It  Returns an Integer object holding the value of the specified primitive.

ToString():- It  Returns a String object representing the value of a specified int or Integer.

ParseInt():- This method is used to get the primitive data type of a certain String.

Abs():-  It Returns the absolute value of the argument.

Ceil():-  It Returns the smallest integer that is greater than or equal to the argument. Returned as a double.

Floor():- It  Returns the largest integer that is less than or equal to the argument. Returned as a double.

Rint():- It  Returns the integer that is closest in value to the argument. Returned as a double.

Round():-  It Returns the closest long or int, as indicated by the method's return type to the argument.

Min():-  It Returns the smaller of the two arguments.

Max():-  It Returns the larger of the two arguments.

Exp():- It  Returns the base of the natural logarithms, e, to the power of the argument.

Log():-  It Returns the natural logarithm of the argument.

Pow():-  It Returns the value of the first argument raised to the power of the second argument.

Sqrt():- It  Returns the square root of the argument.

Sin():-  It Returns the sine of the specified double value.

Cos():-  It Returns the cosine of the specified double value.

Tan():-  It Returns the tangent of the specified double value.

Asin():-  It Returns the arcsine of the specified double value.

Acos():-  It Returns the arccosine of the specified double value.

Atan():- It  Returns the arctangent of the specified double value.

Atan2():- It  Converts rectangular coordinates (x, y) to polar coordinate (r, theta) and returns theta.

ToDegrees():- It  Converts the argument to degrees.

ToRadians():-  It Converts the argument to radians.

Random():-  It Returns a random number.


Related Topics

Java String replaceAll() method

Java String replaceAll() method returns a String replacing all the sequence of characters matching regular expression i.e regex and replacement string. Syntax: public String replaceAll(String regex, String replacement) Parameters: regex : regular expression replacement :...

1 minute read.

How to check if date is valid in Java?

In this article, you will acknowledge about how to verify if a date is valid or not. For this you will learn the approach, you will be able to write...

3 minutes read.

The final Keyword in Java

The final keyword is employed in several instances. Firstly, the non-access modifier final only applies to variables, methods, and classes. The final can be used in the following situations. Final Variables When...

6 minutes read.

Topological Sort In Java

Topological Sort in Java Topological sort is mainly used in the linear ordering of vertices in a Directed Acyclic Graph (DAG). Topological sort in Java illustrates how to do the linear ordering of...

1 minute read.

Java Math floorMod() Method

The floorMod() method of Math class returns the floor modulus of the specified arguments. It firstly divides the dividend and divisor and then returns an integer that is equal to...

1 minute read.

Multithreading in Java

Multithreading is a specialized form of multitasking. It is responsible for executing more than one task at a time of a single program, and each task is a separate thread. A program...

8 minutes read.

Java Math copySign() Method

The copySign() method of Math class returns the first floating-point argument with the sign of the second argument. Syntax: public static float copySign(float magnitude, float sign)public static double copySign(double magnitude, double sign) Parameters: The...

1 minute read.

Java Enumeration

In a computer language, enumerations express a set of named constants. For instance, the four suits in a deck of playing cards could be represented by the enumerators Club, Diamond,...

4 minutes read.

Application of Array in Java

In this article we are going to acknowledge about what the array is, types of arrays and their applications. What is an array? An array is often a set of interrelated elements...

4 minutes read.

How to Run Applet Program in Java

An applet is a new type of program which is put in a webpage. By inserting applet into a webpage dynamic content can be created. Characteristics of an Applet The applet is...

11 minutes read.

Java Editors

A straightforward text editor may be used to create Java applications. However, a Java integrated programming environment (IDE) enables the software developer to create programs more quickly. An IDE offers...

4 minutes read.

Java Pass-by-Reference

In Java, passing parameters can be done using one of two fundamental methods. Pass-by-value is used for the first and pass-by-reference is used for the second. One thing to keep...

2 minutes read.

Selection Sort in Java

Selection Sort in Java Selection sort is also a simple sorting algorithm that works by repeatedly finding the minimum element from the unsorted portion of the input array, and placing it...

5 minutes read.

Minimum Window Subsequence in Java

In this article, you will be very well acknowledged about the minimum window subsequence, what is the approach and how it is implemented. The example program is also executed and...

4 minutes read.

Java Error Stack Trace

The stack trace in Java is an array of stacks.The stack trace reveals the console's location of an exception or error by gathering data from all program methods. The JVM...

3 minutes read.

Thread Scheduler in java

Scheduling: it is defined as the execution of multiple threads on a single CPU in some order is called scheduling. Preemptive-priority scheduling: This algorithm schedules threads based on their priority relative to other...

11 minutes read.

Zebra Puzzle Problem in Java

Complex puzzles like the zebra puzzle demand a lot of work and mental training to complete. Because it was created by renowned German scientist Albert Einstein, it is also sometimes...

10 minutes read.

Permutation Coefficient in Java

In this tutorial, we will get familiar with the permutation coefficient in Java.  We will understand it through examples and see different approaches to solving the problem. A permutation is a...

5 minutes read.

Zygodromes in Java

Zygodrome is a positive number created by the same digits running non-trivially. A number is called a zygodrome if identical digits constantly occur together (in pairs). The Greek word "zyg"...

4 minutes read.

Resultset in java

Resultset: A result set is an interface that is present in the package java.sql and the resultset is used to store the data that are returned from the database table after...

5 minutes read.