Eight Data Types of Java

Data Type : Type of data that can be stored in a variable. Sometimes, Java is called a “strongly typed language” because when you declare a variable, you must specify the variable’s type.

Two kinds of data types:

  1. Primitive types are the data types defined by the language itself.
  2. Reference types are types defined by classes in the Java application programming interface (API) or by classes you create rather than by the language itself.

Java defines eight primitive types:

Type Explanation
int A 32-bit (4-byte) integer value
short A 16-bit (2-byte) integer value
long A 64-bit (8-byte) integer value
byte An 8-bit (1-byte) integer value
float A 32-bit (4-byte) floating-point value
double A 64-bit (8-byte) floating-point value
char A 16-bit character using the Unicode encoding scheme
boolean A true or false value (1Bit)

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.

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

Leave a Reply

Your email address will not be published. Required fields are marked *