Data Types in Java
Data types in Java are divided into 2 categories:
• Primitive Data Types
• Non-Primitive Data Types
Primitive Data Types
Primitive data types specify the size and type of variable values.
They are the building blocks of data manipulation and cannot be
further divided into simpler data types.
Boolean type – Boolean
A boolean data type can store either True or False. They can be
used to check whether two values are equal or not
The default boolean value is False.
Example-
class BooleanDataTypes
{
public static void main(String args[]) {
boolean var1 = true;
if (var1 == true) //checks if the value is true or false
{
[Link]("Boolean value is True");
}
else
{
[Link]("Boolean value is False");
}
}
}
Output-
Character type – char
The char data type stores a single character. It stores lowercase
and uppercase characters, which must be enclosed in single
quotes. It takes memory space of 16 bits or 2 bytes.
Example:-
class CharDataType {
public static void main(String[] args) {
char var1 = 'A';
char var2 = 'd';
[Link](var1);
[Link](var2);
}
}
Output:
Integer type –
An integer type stores an integer number with no fractional or
decimal places. Java has four integer types – byte, short, int, and
long.
Byte
The byte is the smallest data type among all the integer data
types. It is an 8-bit signed two’s complement integer. It stores
whole numbers ranging from -128 to 127.
Syntax:
byte byteVariable;
Short
Short is a 16-bit signed two’s complement integer. It stores whole
numbers with values ranging from -32768 to 32767. Its default
value is 0.
Syntax:
short shortVariable;
Int
Int is a 32-bit signed two’s complement integer that stores integral
values ranging from 2147483648 (-2^31) to 2147483647 (2^31 -1).
Its default value is 0.
Syntax:
int intVariable;
Long
long is a 64-bit signed two’s complement integer that stores
values ranging from -9223372036854775808(-2^63) to
9223372036854775807(2^63 -1). It is used when we need a range
of values more than those provided by int. Its default value is 0L.
This data type ends with ‘L’ or ‘l’.
Syntax:
long longVariable;
Example:
class IntegerDataTypes
{
public static void main(String args[]) {
int a = 10;
short s = 2;
byte b = 6;
long l = 125362133223l;
[Link]("The integer variable is " + a + '\n');
[Link]("The short variable is " + s + '\n');
[Link]("The byte variable is " + b + '\n');
[Link]("The long variable is " + l);
}
}
Output:
Float type –
Floating-point is used for expressions involving fractional
precision. It has two types: float and double.
Float
It is a floating-point data type that stores the values, including
their decimal precision.
A Float value:
• is a single-precision 32-bit or 4 bytes
• can have a 7-digit decimal precision
• ends with an ‘f’ or ‘F’
• default value = 0.0f
•stores fractional numbers ranging from 3.4e-038 to
3.4e+038
Syntax:
float floatVariable;
Double
The double data type is similar to float. The difference between
the two is that is double twice the float in the case of decimal
precision. It is used for decimal values just like float and should
not be used for precise values.
A double value:
• is a double-precision 64-bit or 8 bytes
• can have a 15-digit decimal precision
• default value = 0.0d
• stores fractional numbers ranging from 1.7e-308 to
1.7e+308
Syntax:
double doubleVariable;
Example:
class FloatDataTypes
{
public static void main(String args[]) {
float f = 65.20298f;
double d = 876.765d;
[Link]("The float variable is " + f);
[Link]("The double variable is " + d);
}
}
Output:
Primitive Data Types Table – Default Value, Size, and Range
Data Default
Default size Range
Type Value
1 byte or 8
Byte 0 -128 to 127
bits
2 bytes or 16
short 0 -32,768 to 32,767
bits
4 bytes or 32
Int 0 2,147,483,648 to 2,147,483,647
bits
8 bytes or 64 9,223,372,036,854,775,808 to
Long 0
bits 9,223,372,036,854,775,807
4 bytes or 32
float 0.0f 1.4e-045 to 3.4e+038
bits
8 bytes or 64
double 0.0d 4.9e-324 to 1.8e+308
bits
2 bytes or 16
Char ‘\u0000’ 0 to 65536
bits
1 byte or 2
boolean FALSE 0 or 1
bytes
Non-Primitive Data Types
Non-primitive data types or reference data types refer to
instances or objects. They cannot store the value of a variable
directly in memory.
Non-primitive data types are user-defined.
Array
An Array is a collection of elements of the same data type stored
in contiguous memory locations.
Example:
int Array_Name = new int[7];
String
The String data type stores a sequence of characters.
String literals are enclosed in double quotes.
class Main {
public static void main(String[] args) {
// create strings
String S1 = "Java String Data type";
// print strings
[Link](S1);
}
}
Class
A class is a user-defined data type from which objects are
created.
It describes the set of properties or methods common to all
objects of the same type.
It contains fields and methods that represent the behaviour of an
object.
A class gets invoked by the creation of the respective object.
There are two types of classes: a blueprint and a template. For
instance, the architectural diagram of a building is a class, and
the building itself is an object created using the architectural
diagram.
Example:
Interface
An interface is declared like a class. The key difference is that the
interface contains abstract methods by default; they have
nobody.
Example:
interface printable {
void print();
}
class A1 implements printable {
public void print()
{
[Link]("Hello");
}
public static void main(String args[]) {
A1 obj = new A1();
[Link]();
}
}
Enum
An enum, similar to a class, has attributes and methods.
However, unlike classes, enum constants are public, static, and
final (unchangeable – cannot be overridden). Developers cannot
use an enum to create objects, and it cannot extend other
classes. But, the enum can implement interfaces.
//declaration of an enum
enum Level {
LOW,
MEDIUM,
HIGH
}