Java Primitive Data Types and Their Ranges with Examples
Introduction
Every variable in Java has a type, and the most basic types are the eight primitive data types. This tutorial explains Java's primitive data types and their ranges with examples. We will start with the difference between primitive types and reference types. We will then look at each group of primitives - the four integer types, the two floating-point types,
(Note - If you are new to variables, read Variables in JavaClick to read tutorial: Variables in Java: Declaration, Initialization and Scope first.) Primitive types vs reference types Java has two kinds of types -
Unlike in some languages, these sizes are the same on every platform - an
Integer types - byte, short, int and long
The four integer types hold whole numbers, positive or negative, and differ only in their size and range.
Note - Floating-point numbers are stored in binary, so most decimal fractions, such as
Printing the exact ranges
Each wrapper class has constants holding the range of its primitive type. Let us print them, and try out each type.
OUTPUT of the above code
Explanation of the code
OUTPUT of the above code
Explanation of the code
char and boolean - with their sizes and ranges. Next we will print the exact ranges from Java itself, and see what happens when a value goes past the end of its range. Lastly, we will look at default values and how to choose the right type.(Note - If you are new to variables, read Variables in JavaClick to read tutorial: Variables in Java: Declaration, Initialization and Scope first.) Primitive types vs reference types Java has two kinds of types -
- Primitive types: The eight basic types built into the language -
byte,short,int,long,float,double,charandboolean. A primitive variable holds its value directly. Their names are keywords, all in lower case. - Reference types: Classes, interfaces, arrays and so on - such as
String,LocalDateor your own classes. A reference variable holds a reference to an object stored elsewhere in memory, ornull.
Integer for int, called a wrapper classClick to read tutorial on Java Wrapper Classes.
The eight primitive types at a glance
| Type | Size | Range | Default | Example |
| byte | 8 bits | -128 to 127 | 0 | byte b = 100; |
| short | 16 bits | -32,768 to 32,767 | 0 | short s = 30000; |
| int | 32 bits | about -2.1 billion to 2.1 billion | 0 | int count = 42; |
| long | 64 bits | about -9.2 × 1018 to 9.2 × 1018 | 0L | long views = 8_000_000_000L; |
| float | 32 bits | about ±3.4 × 1038, 6-7 significant digits | 0.0f | float f = 3.14f; |
| double | 64 bits | about ±1.8 × 10308, 15-16 significant digits | 0.0 | double d = 3.14159; |
| char | 16 bits | '\u0000' to '' (0 to 65,535) | '\u0000' | char grade = 'A'; |
| boolean | - | true or false | false | boolean done = true; |
Unlike in some languages, these sizes are the same on every platform - an
int is always 32 bits, whether the program runs on Windows, Linux or a phone.
intis the default choice for whole numbers. A number written in code, such as42, is anint.longis for values too big for anint- such as populations, file sizes or time in milliseconds. Alongvalue needs anLat the end:8_000_000_000L.byteandshortsave memory in large arrays, andbyteis used for raw binary data such as file contents. They are rarely used for ordinary variables.
float and double hold numbers with a fractional part, such as 3.14. double is the default choice: it has about twice the precision of float, and a decimal number written in code is a double. A float value needs an f at the end: 3.14f.Note - Floating-point numbers are stored in binary, so most decimal fractions, such as
0.1, cannot be stored exactly. Small rounding errors are normal, as the example below shows. Never use float or double for money - use java.math.BigDecimal, or count in cents with a long.
char and boolean
- char: Holds a single character, such as
'A'or'7', in single quotes. Internally it is a 16-bit unsigned number - the character's Unicode code - so acharcan be used in arithmetic.'A'is 65. - boolean: Holds
trueorfalse. It is the result of comparisons such asage >= 18, and controlsifstatements and loops. Unlike in C, abooleanis not a number - you cannot use 0 and 1 in its place.
Printing the ranges of the primitive types
//PrimitiveRanges.java
public class PrimitiveRanges {
public static void main(String[] args) {
System.out.println("byte : " + Byte.MIN_VALUE + " to " + Byte.MAX_VALUE);
System.out.println("short : " + Short.MIN_VALUE + " to " + Short.MAX_VALUE);
System.out.println("int : " + Integer.MIN_VALUE + " to " + Integer.MAX_VALUE);
System.out.println("long : " + Long.MIN_VALUE + " to " + Long.MAX_VALUE);
System.out.println("float : " + Float.MIN_VALUE + " to " + Float.MAX_VALUE);
System.out.println("double : " + Double.MIN_VALUE + " to " + Double.MAX_VALUE);
System.out.println("char : " + (int) Character.MIN_VALUE + " to " + (int) Character.MAX_VALUE);
char grade = 'A';
System.out.println("grade = " + grade + ", grade + 1 = " + (grade + 1) + ", next char = " + (char) (grade + 1));
System.out.println("0.1 + 0.2 = " + (0.1 + 0.2));
System.out.println("float 1/3 = " + (1.0f / 3) + ", double 1/3 = " + (1.0 / 3));
}
}
byte : -128 to 127 short : -32768 to 32767 int : -2147483648 to 2147483647 long : -9223372036854775808 to 9223372036854775807 float : 1.4E-45 to 3.4028235E38 double : 4.9E-324 to 1.7976931348623157E308 char : 0 to 65535 grade = A, grade + 1 = 66, next char = B 0.1 + 0.2 = 0.30000000000000004 float 1/3 = 0.33333334, double 1/3 = 0.3333333333333333
Float.MIN_VALUEandDouble.MIN_VALUEare not the most negative values - they are the smallest positive values the types can hold. The most negative values are-Float.MAX_VALUEand-Double.MAX_VALUE.grade + 1is 66, because achartakes part in arithmetic as its Unicode number. Casting the result back with(char)gives the next letter,B. CastingClick to read tutorial: Type Conversion and Casting in Java with Examples is covered in its own tutorial.0.1 + 0.2prints0.30000000000000004- the floating-point rounding error mentioned above.1.0f / 3keeps about 7 digits, while1.0 / 3keeps about 16, which shows the difference in precision betweenfloatanddouble.
Integer overflow
//OverflowDemo.java
public class OverflowDemo {
public static void main(String[] args) {
int max = Integer.MAX_VALUE;
System.out.println("max = " + max);
System.out.println("max + 1 = " + (max + 1));
int millisPerYear = 1000 * 60 * 60 * 24 * 365; // overflows int
long millisPerYearOk = 1000L * 60 * 60 * 24 * 365; // calculated as long
System.out.println("int : " + millisPerYear);
System.out.println("long : " + millisPerYearOk);
System.out.println("Math.addExact: " + Math.addExact(max, 1));
}
}
max = 2147483647 max + 1 = -2147483648 int : 1471228928 long : 31536000000 Exception in thread "main" java.lang.ArithmeticException: integer overflow at java.base/java.lang.Math.addExact(Math.java:914) at OverflowDemo.main(OverflowDemo.java:13)
Integer.MAX_VALUE + 1wraps around toInteger.MIN_VALUE, a large negative number.- The number of milliseconds in a year is about 31.5 billion, more than an
intcan hold. Even though it looks harmless,1000 * 60 * 60 * 24 * 365is calculated withints and overflows. Writing the first number as1000Lmakes the whole calculation uselong, giving the correct answer. - When overflow would be a bug, use
Math.addExact(),Math.multiplyExact()and similar methods. Instead of wrapping around, they throw anArithmeticException, as the last line shows.
'\u0000' for char and false for boolean. Local variables get no default and must be initialized before use. When choosing a type -- Use
intfor whole numbers, andlongwhen the values can exceed about 2 billion. - Use
doublefor decimal numbers in calculations, andBigDecimalfor money. - Use
booleanfor yes/no values, andcharfor single characters. For text, useString, which is a class, not a primitive.
byte, short, int and long, the floating-point types float and double, and char and boolean. Next we printed the exact ranges and saw floating-point rounding in action. We then saw how integer overflow silently wraps around, and how long and Math.addExact() help. Lastly, we went through how to choose the right type. In the next tutorial, we will look at literalsClick to read tutorial: Java Literals: Integer, Floating-Point, char and boolean - the way values of these types are written in code.Java Fundamentals Tutorials
Variables in Java: Declaration, Initialization and ScopeClick to read tutorial: Variables in Java: Declaration, Initialization and Scope Java Literals: Integer, Floating-Point, char and booleanClick to read tutorial: Java Literals: Integer, Floating-Point, char and boolean Java Syntax Basics: Statements, Identifiers, KeywordsClick to read tutorial: Java Syntax Basics: Statements, Identifiers, Keywords Type Conversion and Casting in Java with ExamplesClick to read tutorial: Type Conversion and Casting in Java with Examples Java var Keyword: Local Variable Type InferenceClick to read tutorial: Java var Keyword: Local Variable Type Inference Java Arithmetic, Relational and Logical OperatorsClick to read tutorial: Java Arithmetic, Relational and Logical Operators Java Bitwise and Shift Operators with ExamplesClick to read tutorial: Java Bitwise and Shift Operators with Examples
Variables in Java: Declaration, Initialization and ScopeClick to read tutorial: Variables in Java: Declaration, Initialization and Scope Java Literals: Integer, Floating-Point, char and booleanClick to read tutorial: Java Literals: Integer, Floating-Point, char and boolean Java Syntax Basics: Statements, Identifiers, KeywordsClick to read tutorial: Java Syntax Basics: Statements, Identifiers, Keywords Type Conversion and Casting in Java with ExamplesClick to read tutorial: Type Conversion and Casting in Java with Examples Java var Keyword: Local Variable Type InferenceClick to read tutorial: Java var Keyword: Local Variable Type Inference Java Arithmetic, Relational and Logical OperatorsClick to read tutorial: Java Arithmetic, Relational and Logical Operators Java Bitwise and Shift Operators with ExamplesClick to read tutorial: Java Bitwise and Shift Operators with Examples