Sitemap
Sithila Sihan Somaratne

Sithila Sihan Somaratne

I'm a student. I love programming and I would like to become a Senior Software Engineer.

Java — Numbers

3 min readJun 27, 2024

--

Numbers

Primitive number types are divided into two groups:

Integer types stores whole numbers, positive or negative (such as 123 or -456), without decimals. Valid types are byte, short, int and long. Which type you should use, depends on the numeric value.

Floating point types represents numbers with a fractional part, containing one or more decimals. There are two types: float and double.

Note: Even though there are many numeric types in Java, the most used for numbers are int (for whole numbers) and double (for floating point numbers). However, we will describe them all as you continue to read.

Integer Types

Byte

The byte data type is one of the primitive data types. It is an 8-bit signed two’s complement integer. Its value-range lies between -128 to 127 (inclusive). Its minimum value is -128 and maximum value is 127. Its default value is 0.

The bytedata type is used to save memory in large arrays where the memory savings is most required. It saves space because a byte is 4 times smaller than an integer (int). It can also be used in place of “int” data type.

byte b = 10;
byte y = -20;
System.out.println(b);
System.out.println(y);

Short

The short data type is a 16-bit signed two’s complement integer. Its value-range lies between -32,768 to 32,767 (inclusive). Its minimum value is -32,768 and maximum value is 32,767. Its default value is 0.

The shortdata type can also be used to save memory just like byte data type. A short data type is 2 times smaller than an integer.

short s = 10000;
short h = -5000;
System.out.println(s);
System.out.println(h);

Integer(Int)

The int data type is a 32-bit signed two’s complement integer. Its value-range lies between — 2,147,483,648 (-2³¹) to 2,147,483,647 (2³¹ -1) (inclusive). Its minimum value is — 2,147,483,648and maximum value is 2,147,483,647. Its default value is 0.

Get Sithila Sihan Somaratne’s stories in your inbox

Join Medium for free to get updates from this writer.

The intdata type is generally used as a default data type for integral values unless if there is no problem about memory.

int i = 100000;
int n = -200000;
System.out.println(i);
System.out.println(n);

Long

The long data type is a 64-bit two’s complement integer. Its value-range lies between -9,223,372,036,854,775,808(-2⁶³) to 9,223,372,036,854,775,807(2⁶³ -1)(inclusive). Its minimum value is — 9,223,372,036,854,775,808and maximum value is 9,223,372,036,854,775,807. Its default value is 0. The long data type is used when you need a range of values more than those provided by int.

Note: You should end the value with an “L”.

long l = 100000L;
long g = -200000L;
System.out.println(l);
System.out.println(g);

Floating Point Types

You should use a floating point type whenever you need a number with a decimal, such as 99.99 or 1.1419634.

We’ve got 2 data types can store fractional numbers: float and double.

Float

The float data type is a single-precision 32-bit IEEE 754 floating point.Its value range is unlimited. It is recommended to use a float (instead of double) if you need to save memory in large arrays of floating point numbers. The float data type should never be used for precise values, such as currency. Its default value is 0.0F.

Note: in certain cases you should end the value with an “f” for floats to not confuse with doubles.

float f = 234.5f;
System.out.println(f);

/* If you do:
float f = 234.5
Java will take it as a double as shown in this error:
error: incompatible types: possible lossy conversion from double to float
*/

Double

The double data type is a double-precision 64-bit IEEE 754 floating point. Its value range is unlimited. The double data type is generally used for decimal values just like float. The double data type also should never be used for precise values, such as currency. Its default value is 0.0d.

Note: in certain cases you should end the value with an “d” for doubles to not confuse with integers.

double d = 300.0;
System.out.println(d);

/* If you do:
double d= 300;
It's ok, but it may be confusing...
so, it's better to add an "d" at the end like this:
double d= 300d;
*/

Use float or double?

The precision of a floating point value indicates how many digits the value can have after the decimal point. The precision of float is only six or seven decimal digits, while double variables have a precision of about 15 digits. Therefore it is safer to use double for most calculations.

You can use both float and doublefor scientific numbers:

float f1 = 35e3f;
double d1 = 12E4d;
System.out.println(f1);
System.out.println(d1);

/*
OUTPUT:
35000.0
120000.0
*/

--

--

Sithila Sihan Somaratne
Sithila Sihan Somaratne

I'm a student. I love programming and I would like to become a Senior Software Engineer.