Java Numbers

Java Tutorial

Java Numbers

Numbers in Java are stored using numeric data types. You can use numbers to perform calculations, store values, and work with mathematical expressions.

Java supports two main categories of number types:

️1. Integer Types (Whole numbers)
️2. Floating-Point Types (Decimal numbers)

 Integer Number Types

These store whole numbers (no decimals).

TypeSizeRange Example
byte1 byte-128 to 127
short2 bytes-32,768 to 32,767
int4 bytes-2,147,483,648 to 2,147,483,647
long8 bytesVery large values (use L)

 Example:


 


 Floating-Point (Decimal) Types

These store decimal values.

TypeSizePrecision
float4 bytes6–7 decimal digits (use f)
double8 bytes15 decimal digits (default for decimals)

 Example:


 


Numeric Operations

Java supports arithmetic with numbers:

OperatorMeaningExample
+Additionx + y
-Subtractionx - y
*Multiplicationx * y
/Divisionx / y
%Modulus (Remainder)x % y

Example:


 


Number Formatting (Math Methods)

Java includes built-in math functions:


 Generate Random Number (Useful Trick)


 Summary

Type CategoryTypes
Integer Numbersbyte, short, int, long
Decimal Numbersfloat, double
  • Use int for most whole numbers.

  • Use double for most decimal calculations.

  • Use suffix L for long and f for float.

You may also like...