Java Essentials - Integer type data in java
4K views
Nov 18, 2024
Java Essentials - Integer type data in java Watch more Videos at https://www.tutorialspoint.com/videotutorials/index.htm Lecture By: Mr. Arnab Chakraborty, Tutorials Point India Private Limited
View Video Transcript
0:00
In this video we are discussing integer type of data in our Java code
0:06
So in case of Java we can define this integer in four different formats or in four
0:13
different data types. So in Java there are four data types that can hold the integer type of data and they are
0:20
byte, short, integer and long. So variables can hold a positive and the negative type of data or the value
0:29
data or the value so all this that is a byte short integer and long all these data
0:35
types can hold both the positive and the negative values so for the different
0:40
size of data the range can also differ so let us go for one comparison table
0:45
for them so in case of byte it can occupy only one byte in the computer's memory
0:50
that means the 8 bits if you consider the first bit as a sign so rest 7
0:54
bits will be denoting the magnitude so the range of values consider
0:58
the negative half and the positive half, the values will be ranging from minus 128 to plus
1:04
127. These sort of values can be hold in case of byte type of variables. In case of short
1:11
it is having 16 bits, that is, the two bytes are there. So that's why it can hold the values
1:16
ranging from minus 3 to 7682 plus 3 to 767. So in case of integer, it is occupying
1:24
4 bytes in the computer's memory, that means the 32 bits. So first bit will be denoting the
1:29
and rest 31 bits will be denoting the magnitude part of the number so the
1:33
value which it can hold is minus 2 to 2 to 2 to 3 1 plus to do the part of 31 minus
1:40
1 in case of long it can occupy 64 bits in the computer memory so the value be ranging from minus to be the upper of 63 to plus to be the part of 63 minus 1
1:53
So these are the different data types and these are the range of values. These particular data type define variables can hold, can store and also we have mentioned their respective sizes
2:02
So let us go for one demonstration with the help of which you can show that how this integer type of data types and the value
2:10
can be used in our Java code. So here is the demonstration for you. In this program
2:17
we are going to discuss integer data type handling in our Java programming. At first
2:23
we are going to define one variable that is the my inti of the type of INT. So there is
2:28
the integer type data which occupies 4 bytes in the computer's memory. So if you keep
2:33
50 here then if we print the value of my intee I shall get 50 as outcome. So the
2:39
value of my and t i and t is 50 next time we have gone for 50 by 3 so as 50 and 3 both
2:47
of integer types so the division will take place also the integer division that
2:51
means only the integer part of the quotient will be stored onto onto this my
2:56
i nt because my end i nt is of the type of i nt so that's why only the integer part
3:03
of the quotient during this division whatever will be obtained that will be
3:07
stored so 50 by 3 so we are going to get the value as 16 here so here you have defined one
3:14
variable that is a my byte of the type byte here it is also integer type data
3:20
but it will be occupying only one byte in the computer's memory during
3:24
execution of the code my byte is equal to minus 60 and we printing the value of my byte we getting here minus 60 as well so now let us go for this my byte is equal to byte 150 so here we doing the typecasting of this 150 to bite
3:42
and but 150 is actually exceeding the positive range of the byte here so as 150 is exceeding the positive range
3:51
so that's why what will happen uh the different value will get printed if you printed if you print
3:56
this value of my byte here you see it is printing minus one zero six now
4:02
question is coming in mind that how this minus one zero six is getting printed
4:07
when you are printing my byte even if we have stored 150 in the byte
4:12
format onto this my byte variable the explanation will be something like this
4:16
so 150 is equal to hundred and twenty eight plus 22 so 22 means 16 plus six
4:24
six means four plus two here you can find that here we have written this 150 as a sum of some values where each and every
4:32
value is x can be expressed in the terms up to the part of n so if we convert it
4:38
to the respective binary so here we are finding that 128 is present you see
4:43
128 is present 64 is absent because there is no 64 32 is absent no 32 and here
4:51
we're having this 16 present so 16 is there 8 absent we are not having 8 here
4:56
4 and 2 are present so 4 and 2 are present and 1 is absent so that is a binary
5:02
equivalent of 150 so in case of sign to the presentation we know that if the
5:07
first bit is 1 then the number will be negative if the first bit is 1 number
5:11
will be negative if the first bit is 0 then number will be positive so as here the first bit is 1 so the number is negative to get the magnitude we should calculate its two complement so two complement of a binary number is nothing but once complement plus one so once
5:26
complement means one will be replaced by zero and zero will be replaced by one so
5:31
you can easily find that one zero zero one one zero one's complement will be zero one
5:37
one zero one zero one zero one zero one plus one because once complement plus one will be the
5:42
two complement so it is the place number that is 64 it is 32 16 is absent
5:47
we're having 8 plus 1 so 64 plus 32 plus 8 plus 1 plus 1 we're getting this
5:54
1 as minus 106 so as we're having this minus 106 so you can find that
6:00
we're getting this output as minus 106 okay next one is that shot my shot so
6:08
also the integer type data and which whose size will be 2 bytes
6:12
so if you keep here 233 and if you go on printing you can find that it is printing
6:18
as 233 here so 2 bytes means we can store 16 bits of information so next
6:25
one is the long my long so also integer type data and its size will be 8 bytes
6:30
so it will be occupying 8 bytes in the computer's memory during the execution
6:34
so my long has got initialized with this value large number can be stored in
6:39
case of long type of variables if a print the value we're getting this one as 9876 5 4 321 so these are respective
6:49
value whatever we assigned and that has got stored onto the variable my long so in
6:55
this demonstration we have shown that how to use that integer type of variables in
7:00
our Java code thanks for watching this video
#Computers & Electronics
#Java (Programming Language)
#Programming