0% found this document useful (0 votes)
17 views

Datatypes

The document discusses primitive and non-primitive data types in Java. It explains that primitive types like integers, floats, chars, and booleans represent simple values, while non-primitive types like arrays, classes, and interfaces enable more complex data structures. It then provides details on each primitive type, including their default values, sizes, and ranges of possible values.

Uploaded by

tomasina
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Datatypes

The document discusses primitive and non-primitive data types in Java. It explains that primitive types like integers, floats, chars, and booleans represent simple values, while non-primitive types like arrays, classes, and interfaces enable more complex data structures. It then provides details on each primitive type, including their default values, sizes, and ranges of possible values.

Uploaded by

tomasina
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Data Types in Java – Primitive and Non-Primitive Data Types

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.

Explore Online Java Courses

What are Data Types in Java?

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!

Data Types in Java

Data types in Java are divided into 2 categories:


Primitive Data T ypes

Non-Primitive Data T ypes

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

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.

Access Modif iers in Java


Access mo difiers are keywo rds that define the accessibility o f a class and its
members. Access mo difiers are used in Java to co ntro l the visibility
(accessibility) o f classes, interfaces, variables, metho ds,...re ad m o re

Jump St at ement s in Java


In this article, we’ll co ver the Jump Statements in Java, such as break, co ntinue
and return, alo ng with examples and explanatio ns.

Loops in Java Explained


The belo w article co vers the iteratio n statements o r lo o ps in Java. It go es
thro ugh the wo rking o f lo o ps with implementatio n and examples.

For more information, Read Access Modifiers 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.
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

class BooleanDat aT ypes


{
public st at ic void main(St ring args[]) {
boolean var1 = t rue;
if (var1 == t rue) //checks if the value is true or false
{
Syst em.out .print ln("Boolean value is T rue");
}
else
{
Syst em.out .print ln("Boolean value is False");
}
}
}

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

class CharDat aT ype {


public st at ic void main(St ring[] args) {
char var1 = 'A';
char var2 = 'd';
Syst em.out .print ln(var1);
Syst em.out .print ln(var2);
}
}

Output:

Int eger t ype –

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

class Int egerDat aT ypes


{
public st at ic void main(St ring args[]) {
int a = 10;
short s = 2;
byt e b = 6;
long l = 125362133223l;

Syst em.out .print ln("T he int eger variable is " + a + '\n');


Syst em.out .print ln("T he short variable is " + s + '\n');
Syst em.out .print ln("T he byt e variable is " + b + '\n');
Syst em.out .print ln("T he long variable is " + l);

}
}

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:

is a single-precision 32-bit or 4 bytes IEEE 754 f loating-point

can have a 7-digit decimal precision

ends with an ‘f ’ or ‘F’

def ault value = 0.0f

stores f ractional numbers ranging f rom 3.4e-038 to 3.4e+038

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:

is a double-precision 64-bit or 8 bytes IEEE 754 f loating-point

can have a 15-digit decimal precision

def ault value = 0.0d

stores f ractional numbers ranging f rom 1.7e-308 to 1.7e+308

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

class Float Dat aT ypes


{
public st at ic void main(St ring args[]) {

f loat f = 65.20298f ;
double d = 876.765d;

Syst em.out .print ln("T he f loat variable is " + f );


Syst em.out .print ln("T he double variable is " + d);
}
}

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

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:

int Array_Name = new int[7];

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

Int erf ace

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

int erf ace print able {


void print ();
}
class A1 implement s print able {
public void print ()
{
Syst em.out .print ln("Hello");
}
public st at ic void main(St ring args[]) {
A1 obj = new A1();
obj.print ();
}
}

Also Read: Python Vs Java – Which One is Better to Learn?

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

Sort ing Algorit hms in Java


So rting is the pro cess o f putting a list, a sequence o f pro vided items, o r data
co llectio n into a specific o rder. In this article, we will discuss different so rting
algo rithms in...re ad m o re

Underst anding ArrayList in Java


The belo w article go es thro ugh explaining ArrayList in Java with suitable
examples. It co vers the creatio n and o peratio ns o n ArrayList alo ng with a few
metho ds in it. Let’s begin!

FAQs

What is a data type in Java?

What is the dif f erence between primitive and non-primitive data types in Java?

What are 8 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?

What are the Java Literals?

What is the f unction of Data type?

What is Variable in Java?

Is array an data type 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.

You might also like