Understanding Data Types in Java With Examples

Written by Coursera Staff • Updated on

Discover the key data types in Java. Learn about the primitive and non-primitive data types in Java, their differences, and their subcategories.

[Featured Image] A data analyst works with data types in Java on their computer while colleagues have a meeting around a table in the background.

Data types are critical in almost every computer programming language. Knowledge of the type of every bit of data that undergoes processing is a must for programmers. In Java, variables store data, and problems can arise when as a programmer you are unaware of the kind of data the variable is storing. 

In this article, we'll explain the data types in Java, which are categorised as either primitive or non-primitive. This knowledge should help you find it easier to write codes when you have a clearer understanding of the data types in Java.

Which data types can be used in Java?

Data type indicates how data is interpreted by a compiler—or interpreter. Some examples of general data types are integer, strings, boolean, and short. Java data types can be classified into two distinct groups according to their characteristics and behaviour:

  • Primitive data types: These data types are pre-defined by the programming language and are the basic building blocks for programming. The type and size of variable values are also specified. These standard math operators can represent numbers, characters, or other fundamental data types.

  • Non-primitive (or reference) data types: A programmer creates non-primitive data types and defines their reference variables or object references. These data types can be created to perform desired operations. 

Java data types can be broken down into several smaller subcategories to make them easier to classify. The following sections will help you understand the specific data types in Java.

Primitive data types in Java

Java's primitive data types fall into four main types based on size and type of variable values. Those can then be broken down into eight primitive data types. Here is the complete list for your knowledge:

Main primitive data typePrimitive data type
Boolean data type1. boolean
Character data type2. char
Integer data type3. byte 4. short 5. int 6. long
Float data type7. float 8. double

1. boolean 

The boolean data type represents the state of only one bit of information. This can only represent true or false values, but may be used as a flag (set/reset), and other boolean types. Because it isn't implicitly convertible to other types, you must use conversion code to use boolean values in your programs.

Size: The size of a virtual machine depends on the amount of memory and storage resources available. Its precise size cannot be defined.

Syntax: The syntax includes the boolean and the booleanVar;

Values: true and false 

Default value: false

Example: boolean flag=true;

2. char

The char data type is suitable for storing single-value characters, also called Unicode characters.

Size: The size provided is 2 bytes (16 bits).

Syntax: The syntax used under such are the char and charVar.

Values: \u0000 (0) to \uffff (65535)

Default value: \u0000

Example: char a='D';

3. byte 

This is another space-efficient data type in Java. The real data type is a signed 8-bit two's complement integer, which can be used as a replacement for the “int” data type. You can use byte data to save memory in large arrays. 

Size: The size is defined as 1 byte (8 bits)

Syntax: The syntax is denoted by byte and byteVar;

Values: The values of byte are -128 to 127

Default Value: The default value for byte is considered as 0

Example: This can be explained by the most common example, i.e. byte a = 10;

4. short

The type is a 16-bit signed two-component integer representing a positive or negative number. Whenever memory savings are your prime concern, you can use this data type to save memory in large arrays. 

Size: The available size is 2 bytes (16 bits)

Syntax: The syntax comprises: short and the shortVar;

Values: -32,768 to 32,767 (inclusive)

Default Value: 0

Example: short a= 54;

5. Int

This is a 32-bit signed data type that stores numbers. It can store integers greater than or equal to -231 (-2,147,483,648) and less than 231-1 (2,147,483,647).

Size: The size under this category is 4 bytes (32 bits)

Syntax: The syntax comprises the int and the intVar;

Values: -2,147,483,648 to 2,147,483,647 (inclusive)

Default value: 0

Example: int a= 56;

Note: Java SE 8 onwards, you can use int to represent an unsigned 32-bit integer with its value ranging in [0, 232-1].

6. Long

This data type stores enormous numeric data. It is 64-bit in size and a two’s component integer ranging from -263 to 263-1. This helps complement the integer type and is suitable for large data types, including the actual desired value of the int value.

Size: The size that is available under this is 8 bytes (64 bits)

Syntax: The syntax here comprises the long and the longVar;

Values: -9,223,372,036,854,775, 808 to 9,223,372,036,854,775,807 (-263 to 263-1) (inclusive)

Default value: 0L

Example: long a= 1273762;

7. Float

This data type is a single-precision 32-bit (IEEE 754 notation) suitable for storing decimal values. When working with large arrays of floating-point numbers, you should use a float data type instead of double to conserve memory. The range of the float type is infinite and should not be used for precise values like currency. 

Size: The size provided under this context is 4 bytes (32 bits)

Syntax: The syntax here comprises the float and the floatVar;

Values: Up to seven decimal digits

Default value: 0.0f

Example: float a=98.7f;

8. Double

Double can typically be your default choice for decimal values. Double-precision 64-bit (IEEE 754 notation) is a numerical representation system that allows numbers to be stored in 64 bits, providing higher precision than other floating point formats. It can represent arbitrarily large or small numbers and, like its counterpart float, is not recommended for precise data values.

Size: The size under this context is available at 8 bytes or 64 bits

Syntax: The syntax provided under this context are the double and the doubleVar;

Values: Up to around 15 decimal digits

Default value: 0.0d

Example: double DataFlair=99.987d;

Non-primitive data types in Java

Apart from primitive data types, Java also has reference data types referred to as non-primitive ones. They contain a memory address of variable values that are not stored directly but instead store the value at that address.

All variables outside of the eight primitive data types are non-primitive (or reference) types. Examples of  four non-primitive data types in Java are:

  • Strings

  • Classes

  • Interfaces

  • Arrays

Here is more insight into each of the example non-primitive data types. 

1. Strings

In Java programming, a string is an object representing a sequence of characters (text). A string in Java differs from a character array because a string can contain a character sequence in a single variable. In contrast, a character array represents a separate char-type entities’ collection. 

Strings are dynamically allocated data structures that can be iterated to get a series of characters. They are also often used to store user input, user-defined variables, and data storage.

The string syntax specifies the kind of string variable you want to create, enclosed in double quotes. An example is String greetString = "Welcome";

2. Classes

A class serves as a blueprint or prototype that includes all your data. It comprises fields (variables) and methods to define an object’s behaviour.

You invoke a class when you create the respective object. Typically, classes are templates out of which objects are created. For example, a class is like an architectural diagram of a building, and the building itself is an object. You will require an architectural diagram to construct a building.

3. Interfaces

Interfaces are quite similar to classes. The two main differences between a pure interface and a classes are that the pure interface cannot use any methods that are not in the interface itself, as it is abstract by default and it does not have a body outside of default and static methods.

Interfaces also act as blueprints for a class. If a class implements an interface, it adds extra details to every interface function. If you don't declare an interface abstract, then you must declare all its functions public and let other classes use them in the future.

4. Arrays

In Java, you declare an array using brackets and can access its values using a pointer. The array's variable is set when the program is compiled and cannot be altered. Because arrays are homogeneous data structures, it's important to acknowledge that arrays in Java differ from those in C/C++. When dealing with Java, the size of an array needs to be established as an integer value before it is created in the system's memory. This size cannot be altered post-creation.

Java arrays store one or more values of a specified data type, allowing indexed access to each value.

Learn more with Coursera

Without data types, it becomes impossible to classify various varieties of inputs and variables, which could lead to errors in classification. The primitive data types in Java comprise eight different types, while non-primitive data types contain four. Thus, knowledge of data types in Java is necessary for seamless programming. 

To learn more about Java programming, grasp fundamentals by taking the Java Programming and Software Engineering Fundamentals Specialisation on Coursera.

Updated on
Written by:

Editorial Team

Coursera’s editorial team is comprised of highly experienced professional editors, writers, and fact...

This content has been made available for informational purposes only. Learners are advised to conduct additional research to ensure that courses and other credentials pursued meet their personal, professional, and financial goals.