JAVA BASIC DATATYPES
Data Types in Java :
Data type is a special key word , which is used to allocate sufficient memory space for the data, in other words Data type is used for representing the data in main memory (RAM) of the computer.
It can be specifies the size and type of values that can be stored in an identifier.The java language is rich in its data types .
In Java there are two data types available :
- Primitive Data Types -Integer, Character, Boolean, and Floating Point.
- Non-primitive -Classes, Interfaces, and Arrays.
PRIMITIVE DATA TYPES:
Primitive data type mean “very basic “.In Java, primitive data type refer to basic (self-explanatory, you know it’s an Character when you declare an char) datatype such as int, char, short, byte, float, long, double etc.
INTEGER :
Integer types hold whole number such as 123 and -96, and the size of the value that can be store depend upon the integer types that we choose .Why Learn Java?
Type | Size | Range of values that can be stored |
byte | 1 byte | −128 to 127 |
short | 2 bytes | −32768 to 32767 |
int | 4 bytes | −2,147,483,648 to 2,147,483,647 |
long | 8 bytes | 9,223,372,036,854,775,808 to 9,223,372,036,854,755,807 |
The range of the value −(2n−1) to (2n−1)−1
- Int data type is a 32-bit signed two’s complement integer.
- Minimum value – 2,147,483,648 (-2^31)
- Maximum value – 2,147,483,647(inclusive) (2^31 -1)
Where,n- number of bits required
Integer types1) byte (8 bits)
Ex:byte a =100;
FLOATING POINT:
Floating is use to save memory in large arrays of floating point of array .Floating point is single precision 32-bit IEEE 754floating point .
- Single precision floating point numbers occupy -4 bytes
- Double precision floating point numbers occupy – 8 bytes
- Default value is 0.0f (eg. float f1 = 234.5f).
Type | Size | Range of values that can be stored |
float | 4 bytes | 3.4e−038 to 3.4e+038 |
double | 8 bytes | 1.7e−308 to 1.7e+038 |
CHARACTER :
- Character data type is single 16 bit Unicode character (minimum value –‘\u0000’ (or 0) ,maximum value — ‘\uffff’ (or 65,535 inclusive)).
- Character data type can store any type of character (eg : char letterA = ‘A’ ).
BOOLEAN TYPE :
- Boolean type method can represent “one bit of information”.
- Boolean data types are used to store the value in two states (TRUE or FALSE ).
- This Boolean type value is used for simple flag that track (true /false) condition .
- In Boolean method default method is FALSE (eg : boolean one = true )
NON-PRIMITIVE DATA TYPES:
Non –primitive data type or reference data type it is more sophisticated members in the family .Non primitive data type does not store the value but it store a reference to that value .Instead of part Number 4320935 ,can java keeps the reference value are also called ADDRESS ,not the value itself .
Reference Type :
- Class
- Interface
- Array variable
Class is a set of plans for a given object ,there are thousands of tree object but the parent set of plan would belong in the tree class. The variable exist in the tree class such as height or tree type these are called reference variables .An arrays is a single object that can contain the multiple value of the same type .
Let’s see in below diagram ,Primitive variable Vs Reference variable. Notice ,how the values are stored in the primitive variable and the reference variable point to an address variable.
Class Data Types :
Here , we declare a new class called Product :
We have to create a new reference variable for this class and we have to create a new instance of the product class.
Interface Data Types :
Interface is a dashboard or control panel for a class .i have a button but the function is elsewhere.
Array Data Type :
In array does not hold the array value but it can save the address.