Java Primitive Data Types. Size, Range and Default Value of Basic Data Types

Java Eight Primitive Data Types

Java primitive data types are the basic data types that are built-in to Java language. A data type is a classification mechanism whereby it can be identified that what kind of data is stored inside the variable, and what operations it supports.

Java provides a richer set of primitive or basic or built-in data types than other languages like C and C++. There are eight built-in types supported by Java to support integer, floating-point, character, and boolean values. All primitive or basic data types hold numeric data that is directly understood by system.

The following table lists all Java primitive data types, their storage requirements in bytes and the numeric range they support.

Table 1: List of Java's primitive data types
Type Size in Bytes Range
byte 1 byte -128 to 127
short 2 bytes -32,768 to 32,767
int 4 bytes -2,147,483,648 to 2,147,483, 647
long 8 bytes -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
float 4 bytes approximately ±3.40282347E+38F
(6-7 significant decimal digits)
Java implements IEEE 754 standard
double 8 bytes approximately ±1.79769313486231570E+308
(15 significant decimal digits)
char 2 byte 0 to 65,536 (unsigned)
boolean not precisely defined* true or false

*boolean represents one bit of information, but its "size" isn't something that's precisely defined. [Source: Sun's data type tutorial]

It is important to note that Java does not support unsigned types. All int variants are signed. But char is an exception; it is unsigned and consumes 2 bytes in memory. It stores 16-bit Unicode UTF-16 character. Type char to be unsigned seems logical because there are no negative characters.

Default Values of Java's Primitive Types

Java primitive data types are initialized to some default values when they are declared as class members. While programming in Java you declare and use variables at two places. First, inside a function those are local to that function. And second, as a class member. When a variable is declared local to a function it must be initialized or assigned before its first use otherwise compiler reports an error "variable <variable name> might not have been initialized". But when a variable is declared as a class member or field, it is not always essential to assign a value to the member. In that case class members are initialized to some default values by the compiler. However, leaving class members uninitialized is not considered a good practice.

The following table lists the default values for Java's primitive data types shown in Table 1.

Table 2: Default values of primitive data types in Java
Type Default Value
byte 0
short 0
int 0
long 0
float 0.0f
double 0.0d
char '\u0000'
boolean false
String or other object null

Last Word

In this tutorial we discussed Java's primitive or basic data types their default values and range. Hope you have enjoyed reading this tutorial. Please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!

References



Share this page on WhatsApp

Get Free Tutorials by Email

About the Author

is the founder and main contributor for cs-fundamentals.com. He is a software professional (post graduated from BITS-Pilani) and loves writing technical articles on programming and data structures.