Data types: Data types are used to store data temporarily in the computer through a program. In the real word we have different types of data like Integer, Floating-point, Character, Boolean and String, etc. To store all these types of data in a program to perform business required calculation and validations we must use the data types concept.
Definition of Data type:
1) A data type is something which gives information about.
2) Size of the memory location and range of data that can be accommodated inside that location.
3) Possible legal operations that can be performed in that location.
For example: On Boolean data, we cannot perform an addition operation.
3)
What type of result comes out from an expression when these types are used in that expression. Keywords that gives this semantics is treated as “datatype”.
There are two types of data types available in Java:
1) Primitive Datatypes (8) used to store a single value at a time.
2) Reference/Object Data Types (4) used to collect multiple values.
Java has two categories of data types:
· Primitive data types (e.g., Integer Datatypes, Floating Datatypes )
· Referenced Data Types (Array, Interface )
Based on the type and range of data, primitive types are divided into 8 types.
Byte: Byte variables are declared by the use of byte keyword. A byte is 8bits signed type that has a range from -128 to 127 and its default value is “0”.A byte is the smallest integer type and it can also be used in the place of “int” data type & it saves space because byte is 4 times smaller than an integer. Variables of type bye are especially used in Streams. The byte data type is used to save memory in large arrays where the memory savings is required.
Declaring Byte: byte b;
Short: Short is a signed 16- bit type and its range from -32,768 to 32,767 and it's default value is “0”. It is least used data type in Java and short data type can also be used to save memory. The short data type is 2 times smaller than an integer and its minimum value is -32,768 and the maximum value is 32767.
See also: what is system.out.println in Java
Declaring short: short s = 1000;
Int: int is a signed 32-bit type that has a range from -2,147,483,648 to 2,147,483,647 and its default value is “0”. Int is most commonly used as an integer type the variables of type int are commonly used in control loops and to index arrays. Its minimum value is -2,147,483,648 and maximum value is 2,147,483,647.
Declaring int: int i = 10;
Long: long is a signed 64-bit type and its value ranges from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 and its default value is “0”. The range of a long is quite large. Long is used when big whole number are needed. Its minimum value is -9,223,372,036,854,775,808 and maximum value is 9,223,372,036,854,775,807.
Declaring long: long l = 700000l;
Floating –point type: Float specifies a single-precision value that uses 32 bits of storage. It value ranges from 3.4e-038 to 3.4e+038 and its default value is 0.0F. variables of type float are useful when you need a fractional.