Datatypes
Datatypes
Explained
Shiksha Online
Updated on Dec 28, 2023 18:06 IST
In Java, data types play a crucial role in defining the kind of information that can be
stored and manipulated. Primitive data types, such as integers, floating-point
numbers, characters, and booleans, provide the foundation for representing simple
values. On the other hand, non-primitive data types, like arrays, classes, and
interfaces, enable the creation of more complex structures and allow for the
organization and manipulation of larger sets of data. Understanding and effectively
utilizing these data types is essential for developing robust and efficient Java
programs. Let’s understand!!
A data type is a classification of data. It tells the compiler or interpreter how the
programmer aims to use the variables or method. Data types are a crucial factor in
all computer programming languages. The task of a programmer is to develop a
workable program by assigning the right types of data to the right variables. Data
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 29 -Dec-20 23.
types represent the type, nature, and set of operations for the value which they
store. There are two data types of categories in Java: Primitive and Non-Primitive.
Data types in Java specify how memory stores the values of the variable. Each
variable has a data type that decides the value the variable will hold. Moreover,
Primitive Data Types are also used with functions to define their return type.
Unlock the secrets of Java with our career prospects and comprehensive guide; discover
information on top colleges, specialised programmes and online courses to excel in the world
of Java!
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 29 -Dec-20 23.
Source: Crunchify
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.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 29 -Dec-20 23.
Source: Crunchify
There are 8 Primitive data types in Java – Boolean, char, byte, int, short, long, float,
and double.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 29 -Dec-20 23.
Boolean t ype – Boolean
A boolean data type can store either T rue or False . They can be used to check
whether two values are equal or not (basically in conditional statements to return
True or False). Typically, programmers use it as a flag variable to track true or false
conditions.
The default boolean value is False . Moreover, the boolean type’s size depends on
the Java Virtual Machine . Therefore, it fluctuates on different platforms.
Example-
Copy code
Output-
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 29 -Dec-20 23.
Charact er t ype – char
The char data type stores a single character . It stores lowercase and uppercase
characters, which must be enclosed in single quotes. The char data type in Java
supports Unicode characters and provides provision to multiple languages like
English, French, German, etc. It takes memory space of 16 bits or 2 bytes. The values
stored range between 0 to 65536.
Example:-
Copy code
Output:
An integer type stores an integer number with no fractional or decimal places. Java
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 29 -Dec-20 23.
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:
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 29 -Dec-20 23.
Copy code
}
}
Output:
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 29 -Dec-20 23.
Float t ype –
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. It is not used for precise data such as currency or research data.
A Float value:
Syntax:
f loat f loatVariable;
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:
Syntax:
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 29 -Dec-20 23.
double doubleVariable;
Example:
Copy code
f loat f = 65.20298f ;
double d = 876.765d;
Output:
Primit ive Dat a Types Table – Def ault Value, Size, and Range
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 29 -Dec-20 23.
Data Default
Default size Range
T ype 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 or reference data types refer to instances or objects. They
cannot store the value of a variable directly in memory. They store a memory
address of the variable. Unlike primitive data types we define by Java, non-primitive
data types are user-defined. Programmers create them and can be assigned with
null. All non-primitive data types are of equal size.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 29 -Dec-20 23.
Source: Crunchify
Array
An array holds elements of the same type. It is an object in Java, and the array name
(used for declaration) is a reference value that carries the base address of the
continuous location of elements of an array.
Example:
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 29 -Dec-20 23.
For more insights about Arrays in Java, Read: Implementing Arrays in Java
St ring
The String data type stores a sequence or array of characters. A string is a non-
primitive data type, but it is predefined in Java. String literals are enclosed in double
quotes.
Copy code
class Main {
public st at ic void main(St ring[] args) {
// create strings
St ring S1 = "Java St ring Dat a t ype";
// print strings
Syst em.out .print ln(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:
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 29 -Dec-20 23.
For more details, read: OOPs Concepts in Java
An interface is declared like a class. The key difference is that the interface contains
abstract methods by default; they have nobody.
Example:
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 29 -Dec-20 23.
Copy code
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
}
Conclusion
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 29 -Dec-20 23.
Data types are the basis of programming languages. It is essential to know about
Java data types before moving to the advanced concepts of Java. Understanding
data types will help you create a simple program or develop any application or
software.
Suggested Reads:
Dif f erence bet ween Abst ract class and Int erf ace in Java
Bo th abstract class and interface in java helps in achieving abstractio n. They are
similar in nature yet differ fro m each o ther when it co mes to their usage. Let’s
see what...re ad m o re
FAQs
What is the dif f erence between primitive and non-primitive data types in Java?
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 29 -Dec-20 23.
What are non-primitive data types in Java?
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 29 -Dec-20 23.