Data types in java | java tutorial for beginners
Data types in java refers to the type of data used to stored in the variable. It is used to declare the variable or functions of different types.
| Data types in java |
There are two types of data types:-
- primitive data types :- The primitive data types are the basic fundamental data types present in java. It includes boolean, char, byte, short, int, long, float and double.
- Non-primitive data types :- These are the data types that are derived from the primitive data types. It includes arrays, linked list ..etc.
Today will discuss about the primitive data types. They are- boolean
- char
- byte
- short
- int
- long
- float
- double

data types and its size
boolean data type :- The boolean data type contains only two values either true or false. But by default the value of boolean is always false and it takes only one bit of information but the size cannot be specify precisely. The keyword used to declare a boolean datatype is boolean.It takes one byte to store a boolean value.
For example:- boolean isTrue = true;
Here, isTrue is a variable of type boolean which holds a value of true.
char data type :- The char stands for character data type which is used to store a single character value. It takes 2 bytes.The keyword used to declare the character variable is char.
For example:- char c = 'A';
Here, c is the variable of character data type which stores the value A.
byte data type :- The byte data type is a 8-bit signed two's compliment integer. The default value of byte data type is zero. The size of the byte data type is one byte. The value ranges between -128 to 127.
For example:- byte a = 10; byte b = 50;
Here, a and b are the variables which stores the value 10 and 50 respectively.
short data type :- The short data type is a 16-bit two's compliment integer. The default value of byte data type is zero. The size of the byte data type is two byte. The value ranges between -32,768 to 32,767.
- boolean
- char
- byte
- short
- int
- long
- float
- double
| data types and its size |
boolean data type :- The boolean data type contains only two values either true or false. But by default the value of boolean is always false and it takes only one bit of information but the size cannot be specify precisely.
The keyword used to declare a boolean datatype is boolean.
It takes one byte to store a boolean value.
For example:- boolean isTrue = true;
The size of the short data type is 2 byte.
For example:- short a = 1000; short b = 50;
Here, a and b are the variables which holds the value 1000 and 50 respectively.
int data type :- The int stands for integer data type which is the most commonly used data type which is used to store numbers. The default value of int data type is again zero and it's value ranges between -2^31 to 2^31-1. The size of int is 4 byte.
For example:- int a = 100; int b = 50000;
Here, a and b are the variables which stores the values 100 and 50000 respectively.
long data type :- The long data type is used when we need to store values of large numbers and that does not fit int an int data type. The long data type is a 64-bit two's compliment integer. The size of this data type is 8 byte.
The default value of byte data type is zero and its value ranges from 2^64 to 2^64-1.
For example:- long a = 100000L; int b = 50000L;
Here, a and b are the variables which stores the values 100000 and 50000 respectively.
float data types :- The float data type is used to store decimal values in the variable. The default value float data type is 0.00F where F indicates the floating point value. The size of the float data type is 4 byte.
For example:- float a = 23.4f; float b = 55.55f;
Here, a and b are the variables which holds the values 23..4 and 55.55 respectively.
double data type :- The double data type is similar to float data type which is also used to store the decimal values in the variable. It is generally used for precision. The default value of the double data type is 0.00d where d indicates the double data type.
The size of the double data type is 8 byte.
For example:- double a = 23.44d; double b = 63.33d;
Here, a and b are the variables which stores the values 23.44 and 63.33 respectively.
Difference between Float and Double :-
Float is used for single precision (32-bit) and the size of the float is 4 byte where as double is used for double precision (64-bit) and the size is 8 byte.
Comments
Post a Comment