Summary: In this tutorial, you will learn about different data types in Java with the help of examples.
Data Types in Java
In Java, a data type is a classification of values that determines the kind of operations that can be performed on those values and the amount of memory needed to store them.
When declaring a variable in Java, you must specify the type of the variable, like this:
int age; // declares an integer variable
double price; // declares a double variable
String name; // declares a string variable
Java has two categories of data types: primitive types and reference types.
Primitive Type
Primitive types are the basic types of data in Java, and they include the following:
boolean
A type that can only hold the values true
or false
. This type is often used to represent a logical condition or to control the flow of a program.
boolean isTrue = true;
boolean isFalse = false;
char
A type that represents a single Unicode character. This type is often used to store individual characters or to represent a small set of characters (such as a digit or a letter).
char letterA = 'A';
char letterB = 'B';
char newLine = '\n';
byte
A type that represents an 8-bit integer value. This type is often used to represent small integer values that do not require a large range of values.
byte b1 = -128;
byte b2 = 127;
short
A type that represents a 16-bit integer value. This type is similar to the byte
type, but it allows for a larger range of values.
short s1 = -32768;
short s2 = 32767;
int
A type that represents a 32-bit integer value. This is the most commonly used integer type in Java, and it can represent values in the range of -2147483648
to 2147483647
.
int i1 = -2147483648;
int i2 = 2147483647;
long
A type that represents a 64-bit integer value. This type is used for larger integer values that do not fit in the int
type.
long l1 = -9223372036854775808L;
long l2 = 9223372036854775807L;
float
A type that represents a single-precision floating-point value. This type is used to represent decimal values with a limited precision (up to about 7 decimal digits).
float f1 = -3.4E38f;
float f2 = 3.4E38f;
double
A type that represents a double-precision floating-point value. This type is used for decimal values with a higher precision (up to about 15 decimal digits).
double d1 = -1.7E308;
double d2 = 1.7E308;
Reference Type
Reference types are used to represent more complex data structures, such as objects and arrays. Some examples of reference types in Java are:
String
A type that represents a sequence of characters. Strings are often used to store and manipulate text in Java. They are immutable, which means that once a string is created, it cannot be modified.
String s1 = "Hello, World!";
String s2 = "Welcome to Java";
Object
A type that represents a general-purpose object. Objects in Java are instances of classes, which are templates that define the characteristics and behavior of an object.
Objects can contain data (called “fields”) and behavior (called “methods”) and can interact with one another through well-defined interfaces. We will study this in more detail in later post.
Object obj = new Object();
Array
A type that represents an ordered collection of elements. Arrays in Java are objects that store a fixed number of values of a specific type. They can be used to store a list of values or to represent a multi-dimensional data structure.
int[] numbers = new int[5];
String[] names = new String[3];