These data types represent integer numbers i.e. numbers without any fractional part or decimal points. for example: 250,-125,0,1045 etc.
Java supports four types of an integer such as byte, short, int and long. Java does not support the concept of unsigned types, therefore all Java values are signed that means either positive or negative.
The following table shows the memory size and range of integer type
Sl.no | Datatypes | Memory size | Range |
---|---|---|---|
1 | byte | 1byte | -128 to +127 |
2 | short | 2 byte | -32768 to +32767 |
3 | int | 4byte | -2,147,483,648 to +2,147,483,647 |
4 | long | 8byte | -9,223,372,036,854,775,854 775,808 to +9,223,372,036,8954 775,807 |
Example
byte a;
short b;
int e;
long s;
When 1st statement is executed 1 byte of memory will be allocated named as a.
Similarly when 2nd statement is executed 2 bytes of memory will be allocated named as s.
Suppose a statement
long p = 135L;
Here 135 is stored into p which is declared as long type. Note that L at the end of the statement. If L is not there, JVM allocates 2 bytes of memory to P as against the usual 8 bytes memory that should be allocated to a long type. The reason for this is that 2bytes are sufficient to store the value 135. But L at the end of the values as shown in the preceding example.
This JVM will consider it as long values and will allocate 8byte of memory to it.
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc