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

Data Types and Variables in Java

The document provides an overview of data types and variables in Java, categorizing data types into primitive (such as byte, short, int, long, float, double, boolean, and char) and non-primitive types (like arrays, classes, and interfaces). It explains the purpose and range of each primitive data type and describes the three types of variables in Java: local, instance, and static variables. The document emphasizes the importance of specifying data types when creating variables to allocate appropriate memory.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Data Types and Variables in Java

The document provides an overview of data types and variables in Java, categorizing data types into primitive (such as byte, short, int, long, float, double, boolean, and char) and non-primitive types (like arrays, classes, and interfaces). It explains the purpose and range of each primitive data type and describes the three types of variables in Java: local, instance, and static variables. The document emphasizes the importance of specifying data types when creating variables to allocate appropriate memory.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

DATA TYPES AND VARIABLES in java

V.Preethi sai
sri
JAVA DATA TYPES
Data types in Java are the type of data for which a memory will be allocated in the variable and for
which memory is allocated in variables

Types of Datatypes in Java


Data types in Java can be categorised mainly in two forms:

1.Primitive Data Types

2.Non-Primitive or Reference Data Types


Primitive Data Types in Java
The data types in Java in which are pre-defined in java are called primitive data types. Primitive data types are not
user-defined data types because it is not defined by the user.
Primitive data types in Java can be further categorised into eight types:

•Byte datatype
•Short data type
•Int datatype
•Long datatype
•Float datatype
•Double datatype
•Boolean datatype
•Char datatype
Data Type Function Range
Byte datatype in Java is used to save space in large
byte arrays, It is used in placean of integer as it is four- +127 to -128
time smaller than integer

Short data type in Java is also used to save memory


short as byte datatype. The short data type is 2 times +32,767 to -32,768
smaller than an int.

Int datatype is used as a default data type for


int integer values.
+2,147,483,647 to -2,147,483,648. (-2^31)

Long datatype in Java is used when a wider range +9,223,372,036,854,775,807 to -


long than int datatype is needed and Default value is OL. 9,223,372,036,854,775,808

Float datatype is used for saving memory in large


float arrays of floating point numbers.
3.402,823,5 E+38 to 1.4 E-45

Double datatype is used as a default data type for 1.797,693,134,862,315,7 E+308 to 4.9 E-32 13.04,
double decimal values -145.5427, 0,0

Boolean datatype is used for simple flags tracks


boolean true/false condition
true, false

char Char datatype is used for storing any character All Unicode characters such as 'a', 's', '%', '9'
Byte Data Type
The byte data type is an example of primitive data type. It is an 8-bit signed two's complement
integer. Its value-range lies between -128 to 127 (inclusive). Its minimum value is -128 and maximum
value is 127. Its default value is 0.
The byte data type is used to save memory in large arrays where the memory savings is most
required. It saves space because a byte is 4 times smaller than an integer. It can also be used in place
of "int" data type.
Example:
1.byte a = 10, byte b = -20
Short Data Type
The short data type is a 16-bit signed two's complement integer. Its value-range lies between -32,768 to
32,767 (inclusive). Its minimum value is -32,768 and maximum value is 32,767. Its default value is 0.
The short data type can also be used to save memory just like byte data type. A short data type is 2
times smaller than an integer.
Example:
1.short s = 10000, short r = -5000

Int Data Type


The int data type is a 32-bit signed two's complement integer. Its value-range lies between -
2,147,483,648 (-2^31) to 2,147,483,647 (2^31 -1) (inclusive). Its minimum value is -
2,147,483,648and maximum value is 2,147,483,647. Its default value is 0.
The int data type is generally used as a default data type for integral values unless if there is no
problem about memory.
Example:
Long Data Type
The long data type is a 64-bit two's complement integer. Its value-range lies between -
9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807(2^63 -1)(inclusive). Its minimum
value is - 9,223,372,036,854,775,808and maximum value is 9,223,372,036,854,775,807. Its default
value is 0. The long data type is used when you need a range of values more than those provided by
Example:
int.
1.long a = 100000L, long b = -200000L

Float Data Type


The float data type is a single-precision 32-bit IEEE 754 floating point.Its value range is unlimited. It is
recommended to use a float (instead of double) if you need to save memory in large arrays of floating
point numbers. The float data type should never be used for precise values, such as currency. Its
default value is 0.0F.
Example:
1.float f1 = 234.5f

Double Data Type


The double data type is a double-precision 64-bit IEEE 754 floating point. Its value range is unlimited.
The double data type is generally used for decimal values just like float. The double data type also
should never be used for precise values, such as currency. Its default value is 0.0d.
Example: double d1 = 12.3
Boolean Data Type
The Boolean data type is used to store only two possible values: true and false. This data type is used
for simple flags that track true/false conditions.
The Boolean data type specifies one bit of information, but its "size" can't be defined precisely.
Example:
1.Boolean one = false

Char Data Type


The char data type is a single 16-bit Unicode character. Its value-range lies between '\u0000' (or 0) to '\
uffff' (or 65,535 inclusive).The char data type is used to store characters.
Example:
1.char letterA = 'A'

Non-Primitive or Reference Data Types


Non-Primitive datatypes in Java are not pre-defined datatypes. It is a datatype which has to be created
by a programmer as needed. It is also called as Reference or Object data type.

There are three types of Non-Primitive Datatype

1.Array
2.Classes
3.Interface
JAVA VARIABLES
Variables types in Java are memory location which is used for storing any value. Therefore for storing
any data value in JAVA, a variable must be created. As soon as the variable gets created the memory
will be allocated to the data value and the java program will be executed. The size of the memory
allocated to a data depends on it datatype for which variable is created. Therefore we must have to
mention the datatype before creating the variable.
Types of Variable in Java

Java has three kinds of variables in JAVA:

1.Local Variables

2.Instance Variables

3.Static Variables
Local Variables
•Local variables in Java are declared in constructor, method, or blocks.
•This variable is only visible in the blocks or method in which they are declared they can't be used
outside the methods or block in which they are declared.
•Local variables are created when the method, constructor or block is entered and gets once it exits
the method, constructor, or block

Instance Variables
•Instance variables in Java are declared outside a method, constructor or block.
•They are declared in a class and are available for an entire class method, constructor and blocks.
•Instance variables are created when an object is created and destroyed when the object is
destroyed.

Static Variable
Static variables in Java are also declared in the class outside the method, constructor or any block like
instance variable but unlike instance variables static variable can only be declared with a static
keyword.
•Static variables are created when the program starts and destroyed when the programs end
THANK YOU

You might also like