Java Data Types

In Java, every variable must have a specified data type. This determines the kind of values the variable can hold.

Tutorials dojo strip

Example:

JAVA




Types of Data Types

Data types in Java are divided into two main categories:

  1. Primitive Data Types
  2. Non-Primitive Data Types




Primitive Data Types

Primitive data types specify the type of a variable and the kind of values it can hold. Java has eight primitive data types:

Data TypeDescription
byteStores whole numbers from -128 to 127
shortStores whole numbers from -32,768 to 32,767
intStores whole numbers from -2,147,483,648 to 2,147,483,647
longStores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
floatStores fractional numbers, sufficient for storing 6 to 7 decimal digits
doubleStores fractional numbers, sufficient for storing 15 to 16 decimal digits
booleanStores true or false values
charStores a single character/letter or ASCII values




Non-Primitive Data Types

Non-primitive data types include classes, arrays, and interfaces. For now, we’ll focus on one common non-primitive type: String.

Example:

JAVA




Examples of Using Data Types

In this example, we declare different types of variables and print their values using the println() method.

HTML

Tutorials dojo strip