Data Types and Variables in Java
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
•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
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
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
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
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