×

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

Reentrant Lock in Java

The Lock interface is implemented by the ReentrantLock class, which also provides methods for synchronising access to shared resources. The code that controls the resource has calls to the lock...

4 minutes read.

FizzBuzz Program in Java

FizzBuzz is a well-known children's game. This game helps children learn division. The FizzBuzz game is becoming a popular programming question, appearing frequently in Core Java interviews. This section will...

3 minutes read.

Generic queue in Java

Before understanding how to implement a generic queue in java, one must know about generics and queue in java. Generics in Java Generics are parameterized types. The goal is to enable type...

6 minutes read.

Compare time in java

Introduction: This article discusses how to compare time in java. Maximum of the time we need to examine the date and datetime items. Date comparisons are vital if you want to...

3 minutes read.

Banking Application in Java

JDBC (Java Database Connectivity), which provides an API to connect to, execute, and fetch data from any databases, can be used to handle transactions in Java. There are several factors...

7 minutes read.

Tilt operator in Java | Tilde operator in Java Example

The symbol designates it as a unary operator (pronounced as the tilde). It gives back the bit's complement or inverse. Every 0 turns into a 1, and every 1 back...

3 minutes read.

Java Integer shortValue() method

The shortValue() method of Java Integer class returns a short value for this Integer after a narrowing primitive conversion. Syntax public short shortValue () Parameters NA Overrrides: The shortValue () overrides : shortValue in class Number  Return Value This...

1 minute read.

Java Math expm1() Method

The expm1() method of Math class returns ex-1 where e represents Euler’s number. Syntax: public static double expm1(double x) Parameters: The parameter ‘x’ represents the exponent to raise e in the calculation of ex-1. Return...

2 minutes read.

Java 9 Try With Resources

Java 9 provides the improvement in the try statement. It allows us to declare a try statement with duly declared resources. Whenever the user does not require the functionality with...

3 minutes read.

Special Operators in Java

An operator in Java is a special symbol. It is used to perform operations on two or more variables.  We all know basic operations in Java. There are 8 types...

5 minutes read.

Get year from date in Java

The getYear() method of the Java date class returns a number calculated by deducting 1900 from the year that contains or starts with the instant in time represented by this...

4 minutes read.

How to Develop Programming Logic in Java?

Introduction In the world of software development, Java programming language is one of the most powerful programming languages that is used to create a wide range of applications. It includes desktop,...

18 minutes read.

Hourglass problem in Java

In this section, we will discuss the hourglass problem in Java.The aim is to find the largest sum of an hour glass given a 2D matrix. An hour glass is made...

2 minutes read.

Difference between print() and println() in Java

In this tutorial, we will discuss the differences between print and println in Java language. There are mainly two methods to display the text on the console printprintln The above two methods are...

4 minutes read.

Java String replace() method

Java String replace() method returns new String by replacing old characters with new characters or old CharSequence to new CharSequence. Syntax: public String replace(char oldChar, char newChar) public String replace(CharSequence target, CharSequence replacement) Parameters: oldChar...

2 minutes read.

Equilibrium Index of an Array in Java

An array's equilibrium index is the condition where the sum of items with lower and higher indices equals. For example, an array A=[-4,5 2,6,-5] where: a[0]=-4, a[1]=5, a[2]=2, a[3]=6, a[4]=-5 Now according...

4 minutes read.

File Operation in Java

In Java, a file is an Abstract data type. These are used to store the data which is related. Files are named storage locations. With a file we can perform...

7 minutes read.

Checked vs Unchecked Exceptions in Java

In this tutorial, we will discuss Java's checked and unchecked Exceptions. Exception An exception is an undesirable event that disrupts the normal flow of the program. An exception is thrown at runtime. It...

4 minutes read.

Anonymous Function in Java

A function defined as being unbound from an identifier is called an anonymous function. Because they permit access to variables within the scope of the contained function, these are a...

4 minutes read.

Check whether Java is installed or not

As we know that there are various operating systems, to check whether Java is installed or not in Windows and Mac we use the following ways.           Windows Operating System: There are several...

2 minutes read.