Integer Type Data:: Integer Types of Data Represent Integer Number Without Any Fractional Parts or Decimal Points
Integer Type Data:: Integer Types of Data Represent Integer Number Without Any Fractional Parts or Decimal Points
Integer types of data represent integer number without any fractional parts or decimal
points.
For example: age of a person, number of students in a class, distance between planets, distance
between galaxy, etc.,
Now the question arises that to store Integer type data why do we require four data
types??
It is important for one to understand that, given a certain amount of memory how much data
can be stored inside it. There is a certain maximum value and minimum value that can be stored
and the formula to find it is:
For n bits: The minimum value that can be stored is
The maximum value that can be stored is –1
1) byte is the smallest integer data type which has the least memory size allocated.
Assume, if we create a byte type variable as byte a;
It means in the memory one byte is allocated and it is referred as a.
Example-1:
class Demo
{
public static void main(String[] args)
{
byte a;
a = 127;
System.out.println(a);
}
}
Output:
127
Example-2:
class Demo
{
public static void main(String[] args)
{
byte a;
a = 128;
System.out.println(a);
}
}
Output:
Compilation error (because this is out of range).
Example-3:
class Demo
{
public static void main(String[] args)
{
byte a;
a = -128;
System.out.println(a);
}
}
Output:
-128
Example-4:
class Demo
{
public static void main(String[] args)
{
byte a;
a = -129;
System.out.println(a);
}
}
Output:
Compilation error (because this is out of range).
Which means, one value more than +127 or one value less than -128 cannot be stored but any
value between the range of -128 to +127 can be stored using a byte type variable.
Therefore, the age of a person, the current month of the year etc., can be stored using byte
data type.
Which means, one value more than +32767 or one value less than -32768 cannot be stored
but any value between the range of -32768 to +32767 can be stored using a short type
variable.
Therefore, salary of a fresher, height of Mount Everest etc., can be stored using short data
type.
3) int is a signed 32-bit byte. It is the most commonly used integer type. In addition to
other uses, variables of type int are commonly employed to control loops and to index
arrays.
Which means, one value more than +2,147,483,647 or one value less than -2,147,483,648
cannot be stored but any value between the range of -2,147,483,648 to +2,147,483,647 can be
stored using an int type variable.
Therefore, distance between planets etc., can be stored using int data type.
4) long is a signed 64-bit byte and is useful for those occasions where an int type is not
large enough to hold the desired value. The range of long is quite large. This is useful,
when big whole numbers are needed.
Assume, if we create an long type variable as long a;
It means in the memory eight bytes is allocated and it is referred as a.
Therefore, distance between galaxy etc., can be stored using long data type.
class Demo
{
public static void main(String[] args)
{
long a;
a = 9,223,372,036,854,775,807;
System.out.println(a);
In Java, there is rule that whenever
}
we want a value to be stored as
}
long, at the end of the value a
Output: suffix of ‘L’ must be given.
Compilation error.
Hope now we have got the answer for our question of having four data types.
byte a = 24; 2 24
2 12 - 0
2 6 -0
2 3 -0
1 -1
0 0 0 1 1 0 0 0
This type of conversion is called base-2 format.
MSB = 0 positive
1 negative
The below is an example of how negative number is stored.
byte a = -24;
For this, we require base-2 format + 2’s compliment of a number
1 1 1 0 1 0 0 0
MSB = 1 negative
float
4
2) double The double data type is a double-precision 64-bit IEEE 754 floating-point. The
double data type is normally the default choice for decimal values. The data type should
never be used for precise values, such as currency. Double data type stores decimal values
with 15-16 digits of precision. The default value is 0.0d, this means that if you do not
append f or d to the end of the decimal, the value will be stored as a double in Java.
Assume, if we create an double type variable as double a;
It means in the memory eight bytes is allocated and it is referred as a.
double
8
Now let’s see few examples to understand the above topic.
Example-1:
class Demo
{
public static void main(String[] args)
{
double a=45.5;
System.out.println(a);
}
}
Output:
45.5
Example-2:
class Demo
{
public static void main(String[] args)
{
float a=45.5; In Java, there is rule that whenever
System.out.println(a); we want a value to be stored as
} float, at the end of the value a suffix
} ‘f’ must be given, otherwise it is
considered to be a double number.
Output:
Compilation error.
Solution:
Method 1
class Demo
{
public static void main(String[] args)
{
double a=(float)45.5;
System.out.println(a);
}
}
Method 2
class Demo
{
public static void main(String[] args)
{
double a=45.5f;
System.out.println(a);
}
}
0 4 5
100 101
Now we have 100101 as our number, wherever 1 is present we take 2index and add all.
We get 20+22+25 = 1+4+32 = 37
3) int a=0x45;
S.O.P (a); //Output is 69.
Note: Here 0x acts as the prefix to the literal and converts it from decimal to hexadecimal
literal.
0 4 5
0100 0101
Now we have 01000101 as our number, wherever 1 is present we take 2index and add all.
We get 20+22+26 = 1+4+64 = 69
4) int a= 0b100101;
S.O.P (a); // Output is 37.
Note: Here 0b is a prefix for binary literals.
Wherever 1 is present we take 2index and add all. As seen previously we’ll get 37.