Numbers in Java
Remembering the Java Number data types again. There are six Java number data types. Four of which are Integers, two are floating point numbers
Let us discover the properties of each and every data type and what can we do with them!
Some Common Properties of Integer Numbers
All the 4 data types mentioned under Integer Numbers are signed, i.e. all the data types can store both positive and negative values in them.
All of them have a particular width assigned, that it can utilize in memory.
Based on the size assigned, each data type can store a particular range of data in memory.
byte
Smallest in term of width assigned to it, which is : 8 bit
Storage limit: 128 to 127
Keyword: byte
· Usage : Useful while handling file or network data or raw file formats
Example:
public static void main(String[] args)
{
byte b = 76; // This is correct as byte stores from range 128 to 127
byte c = 23;// This is correct as byte stores from range 128 to 127
byte d = 129; // This is not correct as byte stores from range 128 to 127
byte e = 456;// This is not correct as byte stores from range 128 to 127
}
short
· short is assigned a width of 16 bits
· Storage limit: 32,768 to 32,767
· Keyword: short
· Probably the least used data type in Java Numbers
Example:
public static void main(String[] args)
{
short a = 30000; // This is correct as short stores from range 128 to 127
short b = 24587; // This is correct as short stores from range 128 to 127
short c = 32890; // this is not correct as short stores from range 128 to 127
short d = 87547; // This is not correct as short stores from range 128 to 127
}
int
· short is assigned a width of 32 bits
· Storage limit: –2,147,483,648 to 2,147,483,647.
· Keyword: int
· most used data type in Java Numbers
· Whenever any calculation is made on short or byte, first it is converted to int before any operation. This is Type Promotion. Thus, we can say that choosing int in a program is a wise decision.
· Example:
public static void main(String[] args)
{
int a = 30000;
int b = 24587
int c = 464758976076654; // This is not correct as it is larger than the int range
int d = 45743642532626437; // This is not correct as it is smaller than the int range
}
long
· short is assigned a width of 64 bits
· Storage limit: 18446744073709551616 to 18446744073709551615
· Keyword: long
· Need to use suffix L while assigning a value to the variable. Because, default data type for numbers is int.
· Use: When int is not enough to store a data, in those cases, long is used as data types.
Example:
public static void main(String[] args)
{
int a = 21474836487686574L; // error, because, it is out of the range for int
long b = 21474836487686574L;
}
Some Common Properties of Floating Point Numbers
· Both float and double are signed data types, i.e. all the data types can store both positive and negative values in them.
· All of them have a particular width assigned, that it can utilize in memory.
· Based on the size assigned, each data type can store a particular range of data in memory.
· Whenever there is a need to fractional or decimal numbers to use in the program, we use double or float.
· Also called real numbers.
float
· short is assigned a width of 32 bits
· Storage limit: 1.4e–045 to 3.4e+038
· Keyword: float
· Need to use suffix f while assigning a value to the variable. Because, default data type for floating point numbers is double.
· Use: When fraction or decimal must be used, precision is not that big, we use float.
Example:
public static void main(String[] args)
{
float d = 876.9896596f;
}
double
· short is assigned a width of 64 bits
· Storage limit: 4.9e–324 to 1.8e+308
· Keyword: double
· Use: When need to maintain accuracy in big calculation, we should use double.
· double is faster than float.
Example:
public static void main(String[] args)
{
double d = 876.9896596f;
}
In a Nutshell:
ata Types

Width

Range

Keyword

Suffix

byte

8

128 to 127

byte



short

16

32,768 to 32,767

short



int

32

–2,147,483,648 to 2,147,483,647

int



long

64

18446744073709551616 to 18446744073709551615

long

L

float

32

1.4e–045 to 3.4e+038

float

f

double

64

4.9e–324 to 1.8e+308

double



Some widely used Java Number Methods
Method

Description

Result Data type

Example

compareTo

Compares one variable to another variable of same data type

true or false

Double b = 0.09;
b.compareTo(34.87);
returns false

equals

Compares one variable to another. Returns true if both are same, else, return false.

true or false

Integer i = 3;
System.out.println(i.equals("Test"));
System.out.println(i.equals(3));
Prints :
False
true

toString

Convert the variable in signed decimal representation and then returned as a String exactly as the parameter provided to it.

String

Long l = 34L;
System.out.println(l.toString());
Prints:
34

valueOf(int i)

Returns Integer instance of the parameter provided

Integer

System.out.println(Integer.valueOf(5));
Prints: 5

valueOf(String s)

Returns Integer instance of the String provided as a parameter. If the String is not in right format, it will throw exception.

Integer

System.out.println(Integer.valueOf("8"));
Prints: 8

Value
For example :
intValue() Method

Convert the input to int (by casting).
Same for floatValue(), doubleValue() etc.

mentioned in the method name

Double b = 1.09;
System.out.println(b.intValue());
Prints: 1

parseInt parseFloat parseDouble parseLong

Takes input as a String and convert the string to the data type required. If not the proper format is provided, it throws error

mentioned in the method name

System.out.println(Float.parseFloat("56.98f"));
System.out.println(Integer.parseInt("200"));
Prints:
56.98
200

isNan

Check if the parameter is a number or not. Can be applied only on float and double

true or false

Double d = 5.0;
System.out.println(d.isNaN());
Float e = 234.0f;
System.out.println(e.isNaN());
Prints:
false
false

Math.abs

Returns the absolute value of the variable provided as parameter

All Java Number data types

Double d = 5.34465768;
System.out.println(Math.abs(d));
Float e = 5.344f;
System.out.println(Math.abs(e));
Integer i = 5;
System.out.println(Math.abs(i));
Prints:
5.34465768
5.344
5

Math.cos

Calculates the cosine value of the angle (provided as parameter) trigonometrically.

Double
(if the argument is 0 or NaN, the result is NaN)

Double d = 5.34465768;
System.out.println(Math.cos(d));
Prints:
0.5909764119042038

Math.sin

Calculates the sine value of the angle (provided as parameter) trigonometrically.

Double
(if the argument is 0 or NaN, the result is NaN)

Double d = 5.34465768;
System.out.println(Math.sin(d));
Prints:
0.8066888375159488

Math.tan

Calculates the tangent value of the angle (provided as parameter) trigonometrically.

Double
(if the argument is 0 or NaN, the result is NaN)

Double d = 5.34465768;
System.out.println(Math.tan(d));
Prints:
1.3650102123648067

Math.ceil

Calculates the smallest (closest to negative infinity) floatingpoint value that is greater than or equal to the argument and is equal to a mathematical integer

Double (If the argument is NaN or an infinity or positive zero or negative zero, then the result is the same as the argument)

Double d = 5.34465768;
System.out.println(Math.ceil(d));
Integer i = 6;
System.out.println(Math.ceil(i));
Prints:
5.0
6.0

Math.cbrt

Calculates cube root of the

Double

Integer i = 6;
System.out.println(Math.cbrt(i));
Prints:
1.8171205928321397

Math.sqrt

Calculates cube root of the

Double

Integer i = 6;
System.out.println(Math.sqrt(i));
Prints:
2.449489742783178

Math.exp

Calculate the value e^(parameter), where e is the base of the natural logarithms

Double(If the argument is NaN, the result is NaN.)

Integer i = 6;
System.out.println(Math.exp(i));
Prints:
403.4287934927351

Math.floor

Calculates the largest (closest to positive infinity) floatingpoint value that less than or equal to the argument and is equal to a mathematical integer.

Double

Double i = 67.986;
System.out.println(Math.floor(i));
Prints:
67.0

Math.max

Compares two parameters of same data types and returns the largest one. Can be applied to only long, float, double or int.

Long/ int/ float/double

Double i = 67.986;
System.out.println(Math.max(23, 567));
Prints:
567

Math.min

Compares two parameters of same data types and returns the smallest one. Can be applied to only long, float, double or int.

Long/ int/ float/double

Double i = 67.986;
System.out.println(Math.min(23, 567));
Prints:
23

Math.pow

Calculates the value a^b where a is the first parameter and b is the second.

Double

Double i = 67.986;
Double a = 2.0;
System.out.println(Math.pow(a, i));
Prints:
2.9229762406033716E20

Math.random

Returns a pseudorandom double greater than or equal to 0.0 and less than 1.0.

Double

System.out.println(Math.random());
Prints:
0.050148991557008915

Math.toDegrees

Calculates the measurement of the angle angrad in degrees.

Double

Double i = 67.986;
System.out.println(Math.toDegrees(i));
Prints:
3895.3108659764152

Math.toRadians

Calculates the measurement of the angle in radians

Double

Double i = 67.986;
System.out.println(Math.toRadians(i));
Prints:
1.186579545260865

Next >
< Prev