Hey everyone I hope you all are safe and doing good so in the previous lecture we have discussed what are variables keywords identifiers constant and literals as well as Expressions a little bit about Expressions right so now in this video in this lecture we will be talking about what are data types in Java right so what we'll be discussing in this lecture is what are data types then we we will see what are primitive Data types I mean what are two different types of data types or categories of data types and then we see primitive
data types and we will explore one by one all the primitive data types okay now let's understand what is a data type see in the previous video if you remember then we have written something like this when we are uh talking about variables then we have written something like this like I can write int a is equal to some kind of Value or int row number is equal to suppose 100 something like this right so this was a variable so in memory some location would be allocated to this a a is the name of this
suppose the container we call it like a container the value is here 10 the name is a and there is some address of this address is suppose maybe th000 or addresses would be in hexa decimal form but for Simplicity purpose I'm taking thousand so this is name of that variable now What is this int this is what a data type in previous lecture also I have told you what type of value a variable can store data type determines this thing right so this variable can store an integer value so this is what a data type
or if I write like float marks means marks is a variable and this variable can store float type of data means decimal in which we have decimal Numbers that is float so data type basically defines the type of data a variable can store as well as second thing now okay this will store int type of data no so in computers memory how much memory would be allocated how much space would be allocated to this a this also be this also would be defined determined by the data type only different different data types are there and
each and every data type is Having different different size or you can say number of bits required to store that type of data in memory right so they also help allocate memory and Define operations that can be performed on the data right sometime which type of operation we can perform on that particular data that is also determined by the data type right concatenation addition subtraction division or which kind of operation so data type basically Determine what first what type of data variable can store second the size of that variable or they determine how much space
how much memory would be allocated to that variable right now proper use of data types leads to more efficient and error free code so we have to use a proper use you know we have to use these data types properly I'll show you this thing also like if you use a different kind of data type and different kind of data you store in That then you will get an error like suppose int a is a int so we can store here integer value whole number right like 10 or minus5 0 this kind of thing but
if you store here a float value 10.10 what will happen if you store here a string value what will happen it will definitely give error right so this is what a data type is now we will discuss in Java We have basically two types of data types or or let me discuss discuss one more uh Example or more you can say concept first before going further on types of data types you have heard about two concepts dynamically typed languages and statically typed languages so we'll just I'll just give you a brief intro about these things
because uh you should know about these things see we written we have written something like this in a is equal to suppose a value integer value we can store if you want to store in int a is Equal to float 10.10 it will give you error if you want to store here like string Jenny Jenny Jenny is a string literal in a it will give you error right obviously within the same scope we cannot redefine uh variable like int a is equal to 10 again I'm writing int a so you cannot redefine this kind of
red Declaration of a variable so simply if you remove this n sorry int and simply a Is equal to 10 by 10 if you want to change the value then also it will give error because the type is int you cannot store a different typee of uh data here and this thing is known as statically typing in Java we we have to we have to define the type of a variable at the time of Declaration only so this is what that's why Java is a statically typed language because this type is static this is fixed
we cannot change this we have fix This at the compile time only right that's why Java is statically typed language statically type is the variables must be declared with a specific type in Java Java is a statically typed language okay now like this int x equal to 5 now if you if you re reinitialize this x is equal to hello then it will give error at compile time only it will give error so this type of type checking this type would be check For this variable at compile time only right so when you run this
with a command Java C then only it will give you error right although we run this in ID so when you run when you click the Run button then you don't know like I mean it's first obviously compilation would be done then run would be done running of the program would be done execution but this compilation and with Java we run this so these two things you can Clearly see when we run a program in command prompt right so this type of error you will get when you compile a program at that time only compilation
error so at compile time only it will check that type is int but you are storing here string so it will give error it is a static type checking right now what is dynamically typed language see in Python and as well as in JavaScript these languages are dynamically typed language why I am Saying this in Python we simply write see X X is equal to 5 you don't need to specify here the type of the variable at the time of Declaration of the variable there is no need so that's why and further suppose I'm declaring
X is equal to c x equal to 5 here I have done some written some other code and again I'm writing X is equal to Jenny in Python it's correct because the type of the type of this variable is determined at run time Only you don't need to specify at compile time like this would be int or this would be string no so this is not static the type is dynamic it it can be changed see at some point of time it was storing an integer value while later in some other point of time at
some other point of time in the same program we are storing here string so this is what this type of checking is known as Dynamic type checking at runtime only This this would be checked means if you are storing here string so at runtime only this data types would be considered as string so these type of languages are known as dynamically typed languages such as python as well as JavaScript is also dynamically typed language right so maybe you are thinking I hope this much these two concepts are little bit clear to you right I hope
you must you you are you know I know you must be thinking yeah this one is good You don't have to worry about the type and all and just somewhere Let's uh store here integer then float then string and this is fine but here you have to worry about the data type and all yeah that is one advantage of this but one drawback of this dynamically typed language is what suppose in a program at some point of time I have declared X is equal to 5 then I have done written some code then at some
point of time x is equal to JY then I Have also written some instruction or some code and now I want to write Y is equal to x + 1 now this will give you error at run time at run time because the type would be checked because it is dynamically typed language so at run time it will give you error like it cannot be performed something like this but if the same Cas is in Java then when you do this thing then only at compile time only it will Not allow you it will give
you error suppose in ID you writing this program so same ID will give you at Red Line under this as the type is int because in Java we will write here int x equal to 5 and you want to store a string so this is not allowed because Java is statically type language so it means the errors can be detected earlier at compile time only you don't need to wait wait to run a program and at run time because the Error here in Python you will get the error at run time so you have to
wait right so this is why this statically type languages are you can say some advantages of over dynamically typed language in this scenario when you want to you know uh determine the errors early at earlier stage at compile time only so that at run time you will get less error so this kind of Advantage we can say of the statically type language Okay you can try this out maybe in one program you can write in Java in second program you can write in Python if you know something about python right but this is a little
bit concept about statically and dynamically type language I hope this is a little bit clear to you guys right okay now let's you know come come back to the topic what we were discussing and that is types of data types in Java two types are there primitive and reference sorry non- Primitive non- primitive are basically known as reference okay so let me just first discuss what are primitive data types so uh primitive data types means buil-in data types so in Java we have eight built-in data types or eight fundamental data types and those are references
are basically those are using some complex type of thing complex data types such as object objects and all so these thing We'll discuss later class is like nonprimitive data type interface nonprimitive data type so these thing we'll discuss later one by one for now first let's understand what are primitive data types what are inbuilt data types what are fundamental data types in Java okay so in Java we have eight primitive data type and what are those we have Boolean and we have Broadly if we categorize this then numbers so again numbers are divided into integer
then floating Point number and we have character yes character also comes under numbers or numeric data type because internally characters are stored as an unsigned integer in Java okay okay we will discuss this thing in detail don't worry now integer again again are having four category bite short int and Long so basically we have eight primi type Boolean first then bite second third four five and in floating point we have float and double and one is character so so like this we have 1 2 3 4 5 6 7 8 primitive data types in Java
so one by one we'll discuss all these data types right now see first we will discuss these four integer in integer we have these four data types to store an integer Integer means whole numbers whole number like I want to store 10 0 - 5 67 1 2 3 4 these are whole numbers so these numbers comes under in category now you must be thinking why we have four data type to store a simple integer why not one right so it's like in your kitchen you have different different size of container why don't you have
only one size container one big container only to store everything like to store those Spices uh like those uh turmeric coriander P powder or red chili we use those in cooking so for those you have small container if you want to store like some rice or lentils there is some big container to store ATA there is a drum or big container like a more big container more bigger container right so why different different containers why don't your mother just buy a same size of container like this and store everything there only definitely to put Things
more organized way to just you know uh minimize the wastage of the space right suppose I just want to store a like same I have told you there is a lip balm this so for this this container is enough this much container I will not use a large container something like this where I used to where I used to maybe store in my kitchen rice a large container so definitely I'll not use that big to store lip balm this much is enough okay And if you store if I use that container to just store my
this much of flip Bal then definitely it is wastage of that my money and as well as the space okay now same example is same thing happens with here sometimes in program we want to deal with small number only right and sometime small number means I want to store like 10 10 sometimes or five these are small number sometimes you are working on a project and there you need to uh store like 3 2 5 6 7 this much big number sometime you need to store one 2 3 5 6 7 8 like this much
big number right so for these for storing these different different sizes of integer there are different different data types obviously this is integer this is also integer this is also integer this is also integer whole number but there are different different types to store Integers just to optimize the memory use and according to your requirement according to your applications or the project you are working according to that you can just select which type of data you want to store and according to that you can select the type of the data type okay now let's discuss
these four data types now first let me just tell you the size of each data type like we have first first we have bite so for bite how Many bits are required the size or the bit depth somewhere it is written bit depth so the size required is8 bits in memory or you can say 2 by to store a bite a bite type of data size is this now range the minimum and maximum value you can store what is the range so it's 2 to power - 2 to^ 7 to 2^ 7 - 1 so
it's -128 to 127 basically this much of number we can store so I want to to store simple 10 in a variable a so rather than int I can take it by it that would be enough because the range is this this much number we can store now why this range is 2 to power 7 and this see if you have only two bits zero and one how many combination can be there of just two numbers at one place there is zero at one place suppose we have both are Z 0 both are one one
this one zero this one one this one one this one zero There are four combinations so four number we can represent with just only two bits we have only two bits right if you have three bits one 2 3 obviously you will store only zero or one it's not like that you will have 2 3 4 or minus one bits are zero and one but positions you have like three bits 1 2 3 you have you uh read read about the permutation and combination there you will get this kind of thing so let me Just
explain this thing if there are three bits three bits so how many Arrangement you can do different different arrangements with 01 so at first place this kind of thing you can do uh if you have three so at first place 0 0 0 this is one then 0 0 0 1 then again 0o then 1 then 0er like this so there we can have basically eight combinations so eight number we can Represent if we have 1 2 3 three bits okay now the base is two because 01 means it's binary system so base is two
but here 1 2 3 three bits are there so 2 to power 3 so basically how many combinations can be there see eight combination Bas is two number of bits are 1 two only two so basically four number we can represent with this right so here same base is two because we are obviously working in binary system okay bits are eight so here we have no two to Power 8 2 to power 7 because one bit is for sign to uh to represent sign because here minus number also positive number also so one bit would
be for to represent the sign only when in computer you store negative number then computer use two's complement two's complement represent representation to store negative numbers right we will not go in that much de because this would be off track if you want to read about this thing how data Or data representation is to be done in computer you can search for that and you can uh read about one's complement two's complement there are three basically techniques to represent negative number sign magnitude one's complement two's complement but the first two sign and one's complement have
some drawbacks there when we store then there is minus 0 as well as plus 0 but obviously there is no minus plus 0 0 is zero so this is the drawback in first Two that's why in two's complement there is no minus or plus Z there is zero only that's why R's complement representation is preferred to store negative numbers right so if you want to read about this thing more you can Google and you can search or maybe if I find a you know good link for this data representation I can provide you that link
right but for now you just remember computer use Tool's complement representation to store negative number and one bit is to Store the sign of that number whether it's positive or negative if a number is positive the bit would be zero if the number is negative the B the bit would be one and that is the MSB this bit this side bit and here the magnitude of the number here the magnitude of the number so this if the bit is zero it means the number is positive if the bit is one the number is negative it's
most significant bit represent and this so One bit is reserved for sign that's why we are left with only seven bits so that is why it is 2 to^ 7 2^ 7 not 2 to Power 8 I hope you got the concept but here see it was 2 to power three because we were having three bits here we are having eight bits but still 2^ 7 not 2 to^ 8 because one bit is reserved for the sign okay now this is the range of bite now next we have short so for short 16 bits are
required the range is minus 2 to power 15 to 2 to power 15 - 1 so if you uh do this then it will be 3 2 7 6 8 2 3276 7 so this is for short so like this for INT number of the size of the int or the number of bits required to store a int variable or int data type in memory is 60 sorry 32 bits and the range is 2 to power - 2^ 31 to 2^ 31 -1 and this is the number it's 21 4748 3648 2 21 4748 3647
it's minus one because one is Represented sorry one is for one is zero so that's why is it's minus one and why 31 because one bit is reserved for the sign whether it's number is positive or negative same for long the number of bits or the size is 64 bits are required in memory and this is the range it's a very large number right now according to your requirement you can choose whether you want to choose bite short or int or long if a small number you want to like Uh do some calculation on a
very small number so go for bite if a little bit large number go for short if with within this range you want to choose a number if within this range you are working on number on a number which is within this range more than this then go for end if you are working on a very large number you can select long generally we deal with small data so we can use bite or short but by default all the integer see whether you store any number it's 10 - 5 0 or very big number all are
by default would be considered like of type int I mean the by default type of all the integer is int right so uh if you like if you write something like this like bite a is equal to 10 so 10 is what it's nothing but a int literal it's a literal fixed value integer literal that's it if you write something about like short B is equal to 153 4 1534 this Number is also a literal integer literal if you write in C is equal to a number like 3 2 1 5 6 7 8 something
like this this is also a literal int literal okay more specifically if I say this is also a int literal int literal and int literal integer so means in both bite short and int we store integer integer lateral but it's in long it's some kind of different now how this long we when You store in it's long D and I want to store some data 1 2 3 4 5 it's very big number suppose right so you you will specify here at the last L whe whether it's small l or capital L but we choose
the recommendation is to use capital L because if you write down here small L sometimes it is misunderstood with one only because one is also same and small L is also same I will show you practically this thing Right so here we write L this is long literal so basically two integer literals are there int literal and long literal which is having L suffix that is long literal and these all all are int literal so bite short and int store int literal and long store a long literal let's do this thing practically and I'll show
you there are some mistakes also there will would be some error and have to declare all the type Of variables and all I hope this table is clear to you everything is written in my notes don't worry and it is there in the pp as well so you don't need to write right everything is in my notes so let's now uh do some practical on this this data type of integer data type these four types so uh this was our first project in our intelligy idea right here we have some file in source so let's
create another class new Java class and the name I'm taking Is suppose integer demo this is the name of the class let's just minimize this and here let's have we will take first suppose we'll take a short number that is bite so bite and RO number RO number is equal to 22 let's have a method display or if you don't want to to display in this method you can directly call in this in this main also suppose I don't want to have any method later we will have method then Directly just just write down Main
and there s so and just rooll number what do you think it will give error or not yes it will give error I hope till now it's clear nonstatic field RO number cannot be referenced from a static context because obviously this is nonstatic you have to make object of this class to access this but if I make this static then it will not give any error you can directly access this RO number so let me run this and I'll show you what see it Will print 22 right but definitely we will not make everything static
so what we'll do is one thing we can do is we can make an object of this class here how to make object of the class class name object name is suppose obj I'm taking equal to new keyword and then this is the default Constructor right and there we can write simply first the object then dot operator and then uh whatever this uh attribute you want to access R number so this will Also work fine it will print you it will print 22 right or one more thing also we can do here also we can
have like in void display a method we can have and this method can directly access this R number so we can simply PR print roll number is and there just concatenation plus is acting as concatenation operator here and RO number so you don't need to print here you simply call This display see in display you don't need to write down any object and all because this display is within the class only so the methods of this class can access these members also these attributes also directly right and there in main we want to access then
in second method then you have to like uh create the object obj do display now fine now let's run this and it will also show you 22 so see the number is a short number so for this you Can take uh bite in memory it will take only one bite that is eight bits so it will take only eight Bits And if you same for the same if I say in suppose R number is equal to 22 sorry R number see now see now same R number R number so you cannot red declare see variable
R number is already defined in this scope scope means within this class wherever within this block only this is the block opening and closing curly places so within this if You red declare again R number it will give error so you cannot red declare with the St type you can we can just we can just like say this thing and we can just say in R number is equal to 22 now for this variable just to store 22 how many bytes how many bits would be allocated in memory 32 bits so this is wastage of
memory right 32 bits the same task we can do within 8 Bits only why to allocate 32 bits just to a small number right I hope now you got the idea Why these four different different uh types to store simple integer because variables are like containers only maybe we have one large container suppose we have only one large container but now we want to store a small number then it would be wastage of memory and if we have a single small container and we want to deal with large integer then it would be difficult to
store large number in that small container that's why different different Sizes are there different different types are there to store simple integers right okay now so you have to use properly according to your need because see nowadays we have we have the memory I mean large memory in our computer so we no no need to worry about taking the type data type and all because we have a large memory but in previous days memory was not memory was crucial in computers computers were not Having that much memory so then they have I mean you know
invented these different different types so that we can use if uh there is some memory constraint devices then there we can use these bite short these are types okay but by default by default for all the integer data type is in this also I'll show you why I'm saying this thing okay now let's take let's take this thing if if I I use I say bite And your J rank your J rank is suppose 200 let's comment out this line see red line this will give error and the red line is required type is bite
but you have provided int here cast expression to bite so this casting also we'll discuss but right now you have to focus on this thing required type is bite but you are providing here in see this is I told you this is int lateral this fixed value so let me just run this and same error let me just show You see possible lossy conversion what is lossy conversion that also I'll show you but you have to focus this thing from int to bite okay now here bite but where is int so by default this 200
is int literal I have told you 22 0 15 these are in literal that's it so by default these are considered as int now the range is obviously out of this bite so we cannot store this within This so that is why it is giving you error because the range is minus 20 128 to 127 so for this we can use what short see now it will not give any error and if you want to print again for this like here I'm printing your J Rank and R number it will print both the thing so
J Rank and R it will print so this is a little bit large number you can use short also that is also fine but now Same if if I want to store here if suppose a number one is there of short type and I want to store 32769 see same it will give you required type is short but you are providing here int so same error you will get in this case also like that that lossy conversion type of thing from int to short so this is short and this is considered as int so by
default these are considered as int lateral this is out of range from this short so it Will give you error we can take this number as int and that's fine now it will not give you any error okay one more thing I just want to uh show you here suppose I want to store int and uh phone number so phone number is this I don't know whose phone number is this don't try out but it also gives error now what kind of error you will get here let me just turn this and show you integer
number is too Large the number is integer but it's too large it's out of the range of in range of in is I have told you you can check out this is out of that range so it will give error if I remove one then it will work fine it will print this thing also if you want to print so let's run this and it will print role number J Rank and this phone number because this is now within the range we have removed one digit from this now the solution for this is if you
want to uh Store a long number suppose phone number so better to make it long right now it's correct the phone number is this but see okay now you are saying this is within the range of long right because the range of long is huge number definitely a huge number than this but still you are getting an error let me just show you this error integer number too large still it gives you error integer number too Large because this is considered as intal I have told you and the compiler of java doesn't care about that
yeah you have spef specified long and this is a long number so don't worry we can store it no it will check yeah this is definitely an inal in literal and this is out of the range of integer so definitely he'll not check the like uh the data type it will give error but you have to specify that this is a long Literal this is a long literal just specify here just write down here L and that's it and now it will not give any error it is a long literal and your dat type is
wrong and it will work fine this number is still an integer if you don't put here L this number is still an integer so you have to write L as a suffix to tell Java that it is a long literal it's not an integer either it C or small L but generally we don't write small L because if you write one and L So sometimes it's a little bit confusing so better to write here capital L now it will work fine it can store this number and it will print your rle number as well and
let's do one more thing if I write something like this like number as INT in and suppose age is equal to 23 this is fine 23 is within the range of int if I write here L see 23 is within the range of int okay but you have added L here means You have specified that this is a long literal now this Java or this compiler doesn't care about that the number is just 23 it will check that yeah l means it is a it is a long literal and the data type provided is int
that's why it will give you an error let me just over the cursor here and I'll show you the error here only required type is in but you have provided here long so it will show you prob probable possible the loss Conversion from long to int this is long this is int so if you specify any number a long literal you have to provide here long then it will work otherwise if you write here in so just without L the number is within the range you have to uh write down this thing right but one
thing let me just show you one more thing if I have long and I have like a large number and a large number is something Like this one this is a large number okay now here this number is it will not give any error you don't specify L here it will not give you any error here if you don't specify L it will give you error this is long this is long and this is also within the range of long this is also within the range of long but why you are getting error here in
this case because this number is by default I have told you whatever is Written here without L without L if any number is written so that would be considered as intal and this number is out of the range of int literal the range is this one - 2 1 47 48 3 6 and 48 2 + 2 1 47 48 3 six and 47 so this number is you can check out of this range out of this range so that's why it will give you integer number is too large but this number is within the
range so that's why It will not give any error although it's considered as integer but it would be now here compil compiler will do what it will automatically convert this into a long literal without specifying here L because here the data type is long so 64 would bit would be allocated and this would be considered implicitly converted into a long and it will be stored so this type of conversion this type of conversion is called like casting or like implicit Casting I'll I'll show you explicit and implicit casting as well with a one of example
don't worry I hope this is clear to you this will not give any error if you provide here L but better to always add a suffix L if you want to specify that this number is a long leral right so better to add L if you don't add L then also it will work fine don't worry you will not get any error so here you can simply print this large number also you can check Out so it will print this number as well properly it will not you will not get any error right and this
large number generally we write down uh something like this uh when we write then if the number is large then we separate this with like comma somewhere like comma then again comma then again Comma just to increase the readability of the number if the number is large but in programming thing this coma is not allowed right so if a Number is large suppose I have one something more the long number this is also the long number is too large so this is out of the range of long let's remove something if the number is too
large and if you want to make it more readable you can just add underscore something like this underscores are allowed so you can make a number something like this but at starting and At ending underscore is not allowed it's not like that you can put here underscore no it will give error so and here also at the end if you put underscore then then also it will give error illegal underscore in between you can put underscore but if you print then it will print the number the phone number without underscore don't worry see the number
is without underscore this is just to increase the readability this thing also we can do Right so I I hope you got bite short int long so according to your requirement when you need to deal with a short number when you need to deal with a long number and this kind of things you can take the data type of the number right now there is one more concept here let me just show you that thing of rapper classes and uh see we'll just uh discuss a little bit about repper classes here uh because in detail
we'll discuss later So if I write something like this suppose for a int I want to find M value of integer the range so int minan value equal to just write down here integer dot minan value semicolon and for max value also we have this thing max value is equal to integer dot max value now int this is just a data type Min value we have declared a name Of a variable any variable name you can take a b c x y z but I have told you the variable should be meaningful name and descriptive
so here in this I'm storing minimum value of this int now integer this is wer class rapper class for INT int data type in this rapper class we have like this method minan value sorry it's not a method it's a constant public static final int minan value in Min value see the value is the minimum value of that Integer so it's a constant holding the min minimum value of int right and this max value is a constant holding the maximum value of int with within this integer rapper class so if you want to PR now
what is a rapper class let me just uh discuss this thing first so see basically what is a rapper class rapper class provides it just provides a way to use primitive data types like int care Boolean bite whatever the Primitive dat type eight Primitive data types are there as objects we can use these primitive data types as objects right and each primitive data type we have a rapper class for INT we have integer for car we have character for float we have float with capital letter first capital letter so for float this is the class
for integer integer this is the rapper class for character this is the rapper class what Is basically rapper class and why we need it let's uh this give you let me just give you a brief intro about this see if I have something if suppose I have this clutcher right and I have a wrapper of a toffee or a chocolate if I wrap this my clutcher within that rapper I don't have that rapper properly but if you have a rapper of Toopy you can wap this I know within this I have a clutcher but if
someone who don't know maybe he or she Think that there is a toffee right so same this clutcher is like a primitive data type whether it's int care buan or any their Ty and that wrapper we are putting some wrapper here and that's why after putting that rapper it becomes an object so as an object we can use in this Java not just primitive type now why we need this because there in later videos we'll be discussing some like uh hashmap Vectors and AR list so there these data structure Stores Only objects they don't store
primitive data types so first to store primitive data types or the values in those data structures we convert our primitive data type as an object then we store so Java provides a concept of wrapping up of these primitive dat types with the help of wrapper class so we can wrap these primitive data type with the wrapper class like suppose this is int primitive Data type and this is rapper class for this so outside of this I'll put a rapper class integer and then this will be converted this will be act as an object right and
like this there are so many use of rapper classes this is one of them we will discuss rapper classes in detail in later videos don't worry I hope this little bit of this uh need or this reason is clear to you why we need rapper classes and what is a wrapper class right so with the help of this Integer we can convert this int data type as an object although it's not an object this primitive data types will not be acted as an object as an object in general these are primitive data types buil-in data
types right so how to use rapper classes I'll show you and I'll discuss this thing with practical see so this is rapper class for INT and here we are getting Min value and max value there are many other you can say other things also within this rapper Class we can do but for now just take these two things and let's just print here let me just comment out everything here rather than these two lines and we will just print here Min value of int is the variable name is minan value and same we can print
max value so if you run this you will get the range the minimum value and maximum value okay semicolon expected definitely this would be an Error because you have to put semicolon to end the line so see minimum value of int and this is maximum value of int Now One More Concept is there I just want to tell you something interesting this is a little bit deep the rapper classes and whatever concept I'm going to tell you here we are going a little bit further here okay so try to understand if you are exhausted you
can just pause the video take a break and then come back right but these things are important Because when you know when you are going to sit in an interview so everybody knows the basic details what is an INT data type and the range and all right everybody can uh tell this thing but if you know a little bit deep and if you tell these thing also then definitely you will stand out from the crowd that's why I'm discussing this in a little bit deep you will not get these things you know you will rarely
get these things in any Course okay now what is this thing if I right Maxim value is maximum value so if I do this thing right a is equal to whatever the max x value or a suppose we have like int a is equal to whatever the max value here + one so what do you think see Max value is the maximum value of integer if you're adding one here means it should give error it will give out of range right but I don't know let me just check this it will give error or not
so Whatever the value of a let's just print that value let's run this and focus on what you are getting see a is this - 2147 4836 and see maximum value was maximum value was 2 1 we have seen this thing 47 48 3 6 47 here we are adding plus one so it should be positive one or otherwise it should give error because it would be out of range but it is printing negative value this why this is printing this thing if I do here + 2 and if I run this see it again
give minus but this time it's 47 at last so this concept is basically known as wraps around I will tell you this concept in detail just wait for one minute let me just show you one more thing here if if I write something like this suppose the second statement I'm writing int B is equal to and whatever the value the maximum value is 2 1 4 7 4 8 3 6 and 4 7 This is the maximum value we can store in an integer B so suppose if I want to print this B value so
what should it it will print definitely it will not give any error and it will print the B value see it is printing B value now what if I do here 48 see the error integer number too large right but here it will not give you any error it is also the same thing you are Adding this plus one so definitely it's same thing but here you are getting error here you're not getting any error why this thing and why the minus value you are getting here see this would be checked at compile time only
the number is too large so the compiler will give error now this is like some arithmetic operation we are doing this is an expression right so some X Value Plus one so at the time of compile Time it will not be detected like it will give what more value or less value or what so when this thing comes into picture then wrap suround concept is there so what is this uh wraps around concept let me just explain this with example here see if you if you print this thing it will give you no error and
it will print because this is the maximum number we can store in an INT if I modify this with eight it will give error integer Too large because at the compile time only it will check like OB this is a number which is out of range this integer value it will give you error but what if you modify something like this if I write here 7 only but here I do a is equal to a + 1 or B is equal to a + 1 and then if I print a then what it will print
this it will print without giving any error so this is kind of when you the value exceed to the maximum limit Then overflow this is Overflow this condition is overflow and the binary representation wraps around now what do this wraps around I'll just I just try to discuss this with the graph or a circle type of thing visually because then you will get it better right so it is just to make you understand this is what int and here we have zero 1 2 3 something like this so the positive maximum value for in is
it's 2 1 47 48 3 6 and 47 and like this -1 -2 - 3 anticlockwise -4 like this so the negative number is -2 1 47 48 3 6 and 48 like this and here we have - 2 1 47 48 3 6 4 7 something like this something like this we have so when you try to do something like this now this number a is this here I want to add one to this a so here overflow occurs because the binary representation of this number wraps Around wraps around means when you go one
step further see the next number is this one so it print this one if you want to add here plus two so it will go further one more step and the next number is this one so it will print this number like this it happens right so that's why this wraparound concept is there so you have to take care when you are using these things right these numbers because maybe you are thinking After this it will give error or The Loop once you are using this kind of thing in Loop so maybe you are thinking
when you reach to the maximum then the loop is going to stop automatically but this will not happen it again go to minus like this like this then again zero then again here again here so this wraps around concept this will continue something like this or maybe when once you reach to zero maybe then it will store so you have to use these data Types precisely right maybe suppose in in where in counter is important somewhere maybe in your website or some cases counter is important and when counter reach to here then counter again go
to minus then that is not a good good thing okay when you're counting something and that again go to that minus and then zero then then is definitely not a good thing so this is kind of a wraparound concept so this is Overflow condition there is underflow Condition as well if I store int a is equal to minus in minus we have this is the highest or the maximum negative number so if I do here something like this a is equal to a - 1 it is minus again I'm doing minus one so this is
minus so this kind of this is underflow this is underflow and again the binary representation will wrap around wrap around means if it is minus one so we are going again minus one we are going This side so this will be printed the output would be this if you do minus two one more this would be printed this kind of thing I hope you got this concept right so maybe somewhere in interview they ask this type of Ricky question also like the value is this I want to add this or I want to do here
A minus minus or A++ these are increment and decrement operator so what you will get so directly if you don't know about wraparound concept and this thing Overflow underflow you will say it will give error because this is out of range so these small things you know matters a lot in interview if you know these things in a little bit detail although from a fresher they expect that you know the basics but the basics also a little bit further in deep because about Basics almost 99 99% students know but if you know something extra then
you will stand out from the crowd I hope you got this concept let me just show you one Underflow condition as well okay before that let me just tell you something about to's compliment we not go that much deep because that is not our subject but although we are dealing with this minus number and computer's memory so let that's why I'm showing you a little bit uh this thing see I have told you in Java this negative number are represented with two's complement right so how this number is to be represented and how this wrap
Around or this things comes into picture see if the number is this uh suppose the number is so in binary this number is represented how this number is to be represented in this form so they are these are 32 bit it is taking 32 bits and this is how in binary this number is to be represented now if we do a equal to a + 1 so if we add here one obviously one otherwise all the things all the these bits would be 0 0 So you know about binary addition if you don't know then
you can go and check but that's why I told you we'll not go in little bit deep because things would be added and all so we'll not discuss that thing let me just show you in brief what is how the addition would be performed binary addition so it's one one so 1 one becomes zero and carry would be forwarded that is one so again 1 one becomes zero carry would be forwarded Again one so 1 one is zero so like this so all these ones would be zeros after adding one here because of the carry
forward concept 0 0 0 0 0 0 0 and when carry is forwarded here one so 1 plus 0 is one so this is the number after adding one now this is what the first bit I have told you this is the sign bit so if this is one it means the number is negative right so this is for sure the number is negative but how to find out the magnitude of the number what is the Number it's not like that it is 0 0 0 so the number is zero no in two's complement have
to find out the magnitude of the number because computer use two's complement representation to represent a negative number so first apart from this sign bit all the bits we will find out one's complement of this to find out the magnitude first find out one's complement how to find out one's complement if there is zero Change it to one if there is one change it to zero now everything is zero here so everything would be converted into one so this is one's complement of this number after finding one's complement add again one so if you add
one then again 1 1 0 carry would be one 1 1 0 carry would be one 1 one 0 carry would be one so like this there would be all 0 0 0 all the ones become 0 0 0 and at last again we will Get 0 0 0 and after that again one so one so because one carries one one 1 zero carries one so there is nothing so one so this is the magnitude of the number so if you find out this this binary number number if you convert this binary number into decimal
then you will get this thing 2 1 47 48 36 48 so the magnitude this magn magnitude is this and the sign is negative that we have already figured out so negative This that's why this number is you are getting after adding one this thing I hope you do this I hope you got this how this wraps around concept would be done how why this is the number but after adding one you are getting this thing how the number is converted into minus number because of this thing right if you are exhausted you're feeling exhausted
you can just take a break pause the video have some five or 10 minutes break And then come again or you just first read out about two's complement one's complement binary addition and all then come to this thing right so this is how it is going to store but this is a little bit deep if you're not getting you can leave it also right now this is like overflow condition now same underflow condition so let's just uh discuss this with one or two more example same if I take like int B let's just comment out
this line Int B is equal to whatever the uh minimum value whatever the minan value you have have get minus one so you will not get any error and you can just if you print this B you will get a positive value this thing I have discussed I have discussed see why you are getting this thing because of the wraparound concept but directly you cannot store but directly if you want to store here like suppose in C and here I want to store minus 49 Then it will give error directly at compile time only it
will figure out that okay this is out of range integer is too large so it will give error here directly we cannot store but yeah this thing we can do minus this Min - 8 minus one this thing we can do see numeric overflow in expression so this is this is fine you can just print here rather than b let's print C so we're not printing B so let's comment Out this line we are printing only a and C A and C let's run this and I'll show you see you are getting here positive value
it's not B it's c c value we are printing C value is this so directly we are not storing the number out of range we are doing some arithmetic operator we are applying some calculation this is expression that's why it it is not giving any error directly if you provide an inte out of range it will give Error the compiler will give error but here compiler doesn't is not able to understand like it is it would be a large number or not that's why you are not getting any error here okay now few more things
I want to tell you other than this int literal or long literal you assign to this integer or long we can initialize this with some other literal as well so what are those literal these are decimal suppose I write here int a is equal to 37 so these Are decimal 10 5 or any number decimal we representing right but we can assign here an octal number as well or a hexade decimal number or a binary number directly if you want to assign an octal number here generally we don't assign hexa or octal or binary number
we directly write decimal value we take and addition of two numbers or anything we directly take the decimal numbers right those literal we don't take this thing but you have some idea If you have some idea then it would be better we can assign this in other way also octal number octal means base 8 so octal number will start with a zero always and after this it will have a number from the set 0 to 7 because the base is eight so from 0 to 7 digit we can use so suppose I write here 3
7 this is correct 0 is correct 0 1 2 5 is correct but if you write 0 8 1 this is not correct because 8 we cannot use from 0 to 7 we can use so this is invalid oal Number hexi is what hexi number will always start from 0x this is more into number system if you want to skip you can skip this either 0 x or 0 capital X you can start with this so hexadecimal number in hexadecimal number we can take from 0 to 15 so from 0 to 9 all the numbers
and 10 rather than 10 we write a 11 B 12 13 14 15 like this so C D E and F till F we use 0 to9 numbers and then a to F we use in hexadecimal number system in decimal we use from 0 to 9 any Number that we use in real life also right from 0 to 9 but in hexade decimal 0 to9 number and then a to F so like this 0x and I write like a f that's correct 0 x 1 2 that's correct only 0 x and suppose uh we have
0 f that is also correct right binary number you know will contain only zero and one so always be preceded with zero B b or 0 capital B and here I write like 1 0 1 0 1 0 1 1 or 0 B 0 1 0 1 1 2 oh sorry not two we cannot write two here obviously B in Binary base is two so you can just use 0 or one or 0 one something like this so this is these are binary number these are exxod decimal these are octal so I can write here
if you write 07 then this is an octal number then octal number when you get this the output then you will not get 37 this octal number will be converted into decimal then that output will be given to you now if you want to convert this 37 if it's an octal then what is the formula I'll tell you here we write like we have 37 so 7 into base is 8 8 power 0 plus 3 into 8 power 1 so it would be 24 + 7 so whatever value you will get here that would be
I think that would be 31 so let's let's show I'll just show you practically this this is correct or not same for EXA decimal if you want to print like 0 X1 2 so the decimal number equivalent to This is 0x means it's hexadecimal so 1 two let's convert this into decimal so have to convert the base is zero for xxa 0 to^ 1 sorry into 2 plus + 16 ra to^ 1 into 1 so you can just 16 + 2 it will give I guess 18 for binary you know how to convert this binary
to decimal this would be same like binary is 1 0 1 0 so 0 into 2 to^ 0 plus this 1 into 2^ 1 plus this 0 into 2 power 2 plus this 1 into 2 to^ 3 So 8 this is 0o this would be + 2 this would be plus 0 so it would be 10 so it would be it will print 10 like this so rather than integer literal you can assign this inter type with octal number hexadecimal number or binary number as well although we don't use these numbers we don't assign these
numbers when we write our programs or when we are uh write working on projects we use decimal numbers only right but just for a sake of information you have the knowledge This is also possible let me just show you practically this thing so let's just comment out everything here and let's take more numbers here I'm taking suppose um a number that is int and suppose for number is equal to 037 right and let's just print in display let's just comment out all these things and in display I'm printing just this octal number or the value
of this OC OCT number Is plus and whatever the variable name that's it this I'm Printing and in main we just created the object and we are create we are calling this obj display in display we have only one line let's run this and it will print I guess 31 see the number is 31 in decimal so this was octal number right now if I take one hexa number hexa number is equal to either you can write here zero X or you can write here 0 capital x both are fine so if you write like
1 2 semicolon and in the next line let's print our hexadecimal number let's run this and see it will paint both like octal number and hexadecimal number so oal was 31 and hexa number is 18 so this number is equivalent to 18 in decimal same with binary also we can print like Int binary number is equal to that would be preceded with sorry it's zero either small b or capital B and suppose one 0 1 0 so same let's print this so three number we are printing first octal then hexi and then binary number so
octal was 31 hexi is 18 and B number is 10 so you can initialize int data type with decimal exi and binary number as well rather than simple integer literal but we generally don't use these things when we are writing Programs this is just for the sake of information you can do this thing also right but one more thing I just want to show you let's just delete all these things and and I want to show you one more thing right in this program only so if I take if I take this is for your
assignment if I take like bite and uh we take like Let's find out minimum value of bite using the by rapper class dot minan value same let's find out out max value see this bite rapper classes Java do Lang package and this is already imported by default automatically imported here so bite dot max value I want and you simply print these two value so we are simply printing using the integer uh Sorry the rapper class of bite we have found the Min value and max value of by so let's just print first this and then
I'll show you something interesting about this thing so we know this minimum minus 128 maximum 127 okay this is not something new let's do some experiment and I am doing like bite now a is equal to whatever the minan value minus one or whatever the max Value let's do for max value + one what do you think in int it was working fine it was having the I have told you the wrap around concept it was wrapping around but here see you are getting an error provided type is int and required type is bite let's
run this and I'll show you the error see possible lossy conversion from int to bite from int to bite now okay bite is fine but it's not int right we are finding out just Minimum value in bite and we are adding plus one so it should give like minus 128 after wrapping around so with bite and short the thing is when we perform arithmetic operations on bite short or carrot care we will see later so on bite and short then these operants see this is operator plus and the value one and mix value these are
considered as operant suppose I write something like this 2 + 3 so here plus is operator and two and three are operants on which we are performing the operation so here this mix value and one these are operant so when we are performing arithmetic operation plus minus or any arithmetic operation with the bite and short these dat types then the operants are automatically promoted to int automatically convert it to int this is implicit conversion and this compiler Will do automatically regardless of their original type whether it's buite or short and the operation would be performed
and after performing the operation the definitely the result would be int right so if the result is int then by by default this Java does not automatically downcast the result back to bite or short because maybe doing so Could lead to loss of data or truncation of data that's why you are getting an error here because this is converted into int this is also int the result is also int and if the result is int we cannot store that into a bite to store this you have to typ cast this right let me show you
this thing how to typ cast so this is the thing you are getting an error maybe you are thinking here it should do some bite cast like so type cast or you can say Here it should give some wrap around not casting bite from 0 1 to like this here would beus 128 - 127 127 126 something like this right -1 -2 so this is minimum value is what - 128 if you do minus1 again then it should give you 127 but it is giving you error why so because these operates are converted into int
int int on int it is performing some operation right and the result is also an INT now result is int here but here we have bite that is not possible Java will not downcast this because int is uh large in size and bite is small in size so maybe it it will think think that it would lose it would lose some data so by default it will not truncate it so the solution is you can truncate it that is why it is not like suppose we have two glasses or suppose we have something like this
uh see upcasting promotion has been done By Java automatically this was in bite right so automatically it was converted into in theint this is promotion suppose uh let's take one example uh suppose on my table I have there are some wal Nets I have just put these walnuts to dry up and my mother asked me to put all these walnuts in a container so she gave me a container but that container is small when I put all the uh those Walnut in that container there are few walnet Still left so I can do what I
can take one more container or I can take a big container and then just put all the wal Walnut in that container that's it if you have those Walnut in a large container and your mother asked to put those Walnut in a second container and which is small right then the container which is is having right now Walnut so it's not like that you will put the walnut in the small container and whatever the Left you can just discard those Walnut and you just throw these walnuts into dust bin no definitely without you know asking
to your mother you cannot do this thing right although you can ask like uh these walnuts are left because this container is small so what to do with these can I discard can I throw this in dust bin if your mother say yes then you can throw so like this you cannot downcast you cannot throw something you can take a big container Without asking your mother that is fine so same here small number would be converted into a higher number by it is small it is higher without asking automatically because there would be no loss
of data right and because of high Precision so that the addition or whatever operation you are performing that would be done with more Precision that is why this bite would be converted into these operant would be converted into int but Java will not downcast These into bite without asking us because of the fear that it may lose some data right but here we know that we can do this thing if the solution of this problem is you can do casting type casting now what is this casting conversion of one data data type to another data
type that is simply casting so two type of casting implicit I have told you from smaller to larger data that is implicit automatically would be done explicit we Do from large to small data type so the result is in so if you want to convert this into bite because a is of bite type then you have to write something like this bite a is equal to here in bracket the name of that data type you want to type cast this and then minan value minus one this will not give you any error and now this
will wrap around it will give you the value 127 so this is casting and why we need Casting that also I have shown you right so this thing basically happens with bite and short and Care in int this thing will not happen in in it will automatically wrap around it will not be converted automatically by uh into long because int literal is different long literal is different two integer literals are there one is in I have told you one is int one is long literal which is having LS suffix and as This bite short and
car also but that thing we'll discuss later bite short these are containing whatever there at right hand side that is a simple int literal so that's why they are promoted into integer here in this case right let me show you this thing with the example so to typ cast this we just write here bite and in bracket just put this in bracket and that's it it will not give you any error and now you can simply print this Value it will you if you print this a it will perform wrap around and it will give
you minus 128 see the value is minus 128 this is min value this is Max value and this is the value a we have printed a this isus 128 so with bite and short you have to do it explicitly the type casting same with short so you have to try this out this is your assignment same with short using Short weer class find out Min value mix value then add max value + one minan value minus one and try to print here the variables and do and see what you are getting right but in int
and long automatically this will work and no error you will get integer about int I have uh told you about long let me just show you for long we have rapper class this long dot suppose uh minan value same let's find out mix value Let's just comment out the these bite and for long let's just print here rather than this here only I will print something like this that Min long value minus one and in second one let's just print Max long value + one see it will not give you any error it will automatically
wrap around and I don't know so you are getting a huge value Min long value minus one so you Are getting plus Max long value + one you are getting minus right so like this this is a huge number so automatically it will wrap around right so now we have discussed you will get everything in my notes as well as in PP in PP it's not in like detailed form in notes you will get but you have to do this thing practical with me only whatever I'm writing here you have to try this out you
can post the video and try this out right and if You're getting any error you can just let me know once we'll have a live class or in discussion also or in WhatsApp also you can post so this thing we have already discussed the data types their range and everything right and so this is the practice problem for you declare a bite variable to store day of a month then the solution is this you can see the explanation and the solution but before seeing the solution you have to declare a variable same declare a Variable
of type shot to store current ear and for INT declare a int variable to store population of a large city right practice problem for long is use a long to represent the number of stars in a Milky Way or any practice exercise you can take rapper classes also we have discussed and wraparound concept is also there that we have discussed only right so this see the wraparound can lead to unexpected results if not handled properly and that thing I have shown you Unexpected results you are getting because of this wraparound concept so you have to
handle this properly and if you know what is this concept then you are able to handle this right casting also I have shown you how casting is done what is Type casting converting a value from one data type to another implicit as well as explicit casting implicit means automatic conversion to a larger dat from int to long from bite to int from Short to int explicit means manual conversion from uh potentially some lose of losing of data can be possible for example long to int int to bite int to short right this we do explicit
means we do implicit means automatic so this is some casting example also you can see I have shown you that example as well right say this is a small number is equal to 10 then long big number is equal to I'm assigning this small number to this big Number so this will automatically do conversion the small number value is 10 10 so compiler will automatically convert this into long and store this here but it's not the case this is a long variable long number having this value now we are taking again int a variable name
is back to small you cannot directly assign another big number this will give error but you can type cast this into int and then you can assign this is explicit casting right so This is all about integer data type bite short int and long we have discussed many about this so it's time to practice this thing right now in the next lecture we will see other PR type that is float double Boolean and car right so that's it for this video now I'll see you in the next video till then bye take care