Hey everyone I hope you are safe and doing good so in this lecture we are going to discuss some basic building blocks of programming that is what are variables keywords identifiers constants literals and we will do some practice exercise as well right so this is the thing we are going to discuss in this lecture now first is What are keywords in Java or not in Java keywords are in any other programming language also like C C++ python in those programming languages also we have keywords so what is meaning of keyword keywords are basically reserved words
or these are predefined words having some special meaning to the compiler right now what does that mean see we know uh when the signal is red so you know you have to stop there when the signal is green you know you can you can go now so the meaning we know red light and green light we know the meaning this Is universal everybody knows these are predefined suppose on a gate it's WR pull so you will pull the door if it's WR push so you will push the door so meaning is predefined right it's like
see uh if suppose I am head of my Village serpant of my Village then it's my responsibility to do some development task in my Village construction of roads and everything to take care of electricity and water and all so suppose I'm going to the work is going on for construction on the road for construction of the road in my Village and I'm head of my Village so can I do this thing that whenever I put that signal or the traffic signal on in my Village so for green light it means you have to stop and
red light means you can go it's specified it's defined for my Village only can I do that thing obviously not because green light means go red light means stop and these are Predefined thing universal truth so we cannot specifically our vage can do this thing like green means stop and red means you can go no so same in every programming languages there are some special words and the meaning of those words are already defined to the compiler once compiler get that word in your program compiler knows what is the meaning of this word right and
when you declare a variable obviously we cannot use Keywords to declare variables like the example I have given you we cannot again Define for my Village like green means stop red means you can go no because they have special meaning so those are keywords keywords are predefined reserved words in Java that have special meaning to the compiler and they cannot be used as identifier this thing I'll show you practically also identifier also I'll tell you like simple name of variable or any name of a class or name Of a method those are identifier nothing else
right and say there are these many keywords in Java 51 keywords you don't have to remember all but you can see many of keywords will be using in later videos see we have used this class class keyword so class is a keyword and the meaning of this class is already defined right so compiler know once it will find out the class keyword means it has to define a class and then next next thing is name of the class only we will be Using IF for new int final finally float else double break these are all
keywords they are having special meaning to this Java right we'll be using this throw throws try once we'll be uh discussing exceptional handling exception handling super keyword also final also finally also so you don't have to exactly remember these all right now because in our programs will be using many of these and you will be habitual of you know uh getting these things like okay this is The keyword this is the keyword right I'll or you can also go to that let me just show you how you can I'll give you a link also on
that link you can see like in Java the we are having these many keywords let me show you that that thing see once you search Java SC specifications this thing we have discussed in previous lectures also once you click on the first link from Oracle Java specifications there you will get we are not uh using 23 so you'll not see This thing we are using 21 so for Java language specifications you can go to the HTML specifications JLS specifications and there you will get see written by James goling and many others so there are many
chapters and there we have keywords sorry these are identifiers so these are keywords see there are 51 character sequence so these word are 51 so you can check out the thing also or the link I'll give you the resources you can check out so these Are keywords in this jdk 21 now one more thing is reserved words so let me show you what are reserved words basically reserved word and keywords are not exactly same but we can say keywords are reserved words but vice versa is not completely true we cannot say reserved words are keywords
because see keywords are currently in use currently used in Java language and have specific meaning but reserved words are those words which are reserved for future use in the Language maybe we are not using those uh words right now in our language in Java language but we we kept these words reserved maybe in later in future we'll be using these words so in jdk 21 these two are reserved words go to and const right now in 21 these words are not in use but these are reserved I don't know about 22 or 23 maybe they
have used these words as a keyword you can check out in JLS specifications of 22 and 23 right so I hope the difference Is clear there are basically students you know got confused get confused in these two words keywords and reserved words so like uh we have some money so currently we are using some money and the money we have reserved or we have saved for future use in bank account so obviously the money which is in bank account or we have saved for future we are not using that money it's not like that we
will not use that money and anyone can else access that money or Like this no we have reserved that money for our future use okay maybe right now we are not using that money same maybe we are not using they haven't used this Cod to and const in jdk 21 but they have reserved these words for future use and same we cannot use these words as a name of variables or we cannot use these words as identifier right so keyword and reserved words are clear to you guys I hope there's nothing much about these Things
now next is variables in Java what are variables see in the previous example we have seen let me just show you that example first see this was the example we have taken this this kind of thing so this is basically a class student which is having these three attributes Name ID and age or these are known as instance variables as well or these are known as like state state of an object and these two The methods these are known as methods or we can say Behavior the state of a object is represented by the attributes
and the methods we have in that object those that those represents behavior of that object so this is just a class just a blueprint obviously to use these things we have to create object so suppose we have created one object the name we have given Rahul ID is 100 and age is 23 another name we have given suppose like G ID is 101 and age is one sorry 21 so like this we can create many object of this class this thing we have used now see name is equal to Rahul name equal to Rahul so
to store this thing we have used a name I can a name so this is a variable nothing but a variable in Java we call this a variable this ID is a variable age is a variable so variables are like containers they store some data values during program Execution right like see we have our own brain so suppose here I I have written something like this I have written something like this like 10 20 30 40 I'll say just look at these numbers now I'm going to rub these numbers still you remember still can you
do some of the numbers I have written here yes you can do because you have your own brain in your memory you have saved these numbers without here I'm not Seeing any number here but you know the number here is was 10 20 30 and 40 and you can easily tell me the sum right but this is not the case with computer computer is not having its own brain yeah obviously computer has memory but to store something in that memory we need variables to hold that data in computer memory you need something so that something
is known as variable it's like a container right so we give it a name Maybe a b c D and in AB b c d the values are stored in the memory so this is a this is B this is C and this is D so here we have like 20 30 like this so a is just a container name of this container is a and the actual value is 10 okay so in computer memory we can in other words we can say say variable is a named memory location which stores some value you got
it now it's not like that these every Container is having same size see now what kind of value we can store in variable see in name I'm storing Rahul I'm the name the string we can say that's a string lateral what is lateral I'll discuss later in ID I'm storing 100 so this is simple integer value in age also simple integer value or maybe some other thing also suppose I am having I'm having marks so there suppose 98.9 so this is decimal value so we can store different different kind of value It's a textual data
as well or a string we can say or a integer value or a decimal value different types of values we can store so how this thing is determined which kind of value is stored in a variable with the help of the type of that variable or the data type of that variable see the data type of name is we take is string there are many types we'll discuss data types as well in which we Are going to store this integer value so for that data type is int if you are going to store this decimal
value for this data type is float so there are many data types I'll discuss every data type so basically what kind of value you store here that is determined by the type of that variable right and every variable is of different different size see in your kitchen it's not like that the container you use your mother use in Your kitchen is of same size not at all you can go and check and ask to your mother like why there are different size of containers if you want to put like for uh containing those spices maybe
termeric coriander powder or uh red chili the container are small right and for some suppose I want to put rice so for that container is I guess a little bit big to just put some like cardamum the container is maybe very small and in Which I have ATA floor for that we have a drum means I we have a drum I don't know what you have this is like drum so this is very big so in our house also we have different different size of container like this is my lip balm so for this the
container is see it's so small right it's not like that in our kitchen I have the container the drum or in there I can use this lip balm definitely not it Just wastage of the the space definitely because I'll Not be using that much lip balm this is fine for a lip Bal right or maybe like for veline I have this kind of container this is a little bit different in shape as as well as in size so in real life also you see you you use different different type of containers like for moisturizer I
have this type of container so in real life also we have this thing right and why different size of container that also I'll tell you when We discuss data type but to somewhat it's clear to you why we use different different types of data types different different types of container you can ask your mother also that why don't we use same size of container in our kitchen to store everything she'll tell you better right so same variables are also of different different size right and that size is determined by the data type we use and
also the data type defines determines what type of value would be Stored and the size of that data to be stored these two things right don't worry we'll see data types later okay now I hope variable is clear to you guys these are containers just to store data values during program execution in the RAM memory because obviously in program we are using variables when you write program you use variables int a int B int age float marks and these type of variables many type of variable so when you run a program then the program would
Be loaded into which memory RAM which is volatile memory or main memory primary memory so we use data value we store data values using variable during program execution means the data values are stored in Ram using variables right now let me show you some that P so see variables are containers that save data values during program execution meaning the data is stored in the Ram uh using variables right now I have told you variables act as a named storage locations in computer memory these are simply you can say named storage location because the name of
that location is the variable name the actual value we store is maybe it may be a string or may be integer or it may be decimal value or character value or anything okay but basically the name of that storage location is the variable name how to represent how to declare This thing I'll show you I'll show you why we need variables obviously for data storage variable store information or we can use for later use in the program so for need variables because we can remember the things or the numbers because computer cannot remember because computer
is not having its own brain code readability now generally we take variable name like uh int age or student age so from the name itself we come to know that it's age of a student this Variable right so generally we put descriptive name some meaningful name right like float marks so by looking at the name itself we can determine like okay this variable is storing marks of student or marks of anyone okay or name from the looking at the name of the variable name we can see that yeah this variable is having name of any
student so it will increase the code readability and obviously variables Allow for easy modification and calculation of data we use some calculation we do some calculations also using variables now real life example also we can take like storing current temperature readings in whether applications in racing game also to track the vehicle speed item count in shopping cart once you add the item that the cart the number would be increased 1 2 3 4 so that is like a account variable Okay in online shopping cart so how to deare variables this thing let me just show
you when how to declare variables in Java and I'll tell you the difference also declaration definition and all so basic syntax is what we just write down here the type of the variable or you can say the data type of the variable then we write name of the variable simply and this is also done but if you write down the proper syntax then here we Write here we write here is equal to you can assign any value or value or more precisely directly write it don't write down like simple value in technical terms or more
precisely if more precisely if I say then we write here literals or Expressions don't worry we'll discuss what are expression and literals also but these things are optional so that is why we Put like this and semicolon so this is proper syntax of variable declaration now what is this type the data type we use int float string Boolean VOR or whatever data type that we write here like if you want to suppose declare an integer variable we will see all the data types but right now you know int integer data type so I'm using int
and Float only so simply write here int the data type the name of the variable name suppose I'm using I'm Writing student age right if you simply write down this thing this is simply declaration or we can say definition see there are two terms here many students get confused in these two terms definition declaration and all so let me just first clear this doubt of yours declaration means simply we are telling the compiler the name of the variable and the type of the Variable no memory allocation no me memory allocation during declaration so no memory
would be allocated in the in this computer memory for that variable while declaring that variable right definition means we are telling the compiler type the name of the variable as well as memory would be allocated at that time only main difference is this main difference is this but now at by looking at this what do you think Memory would be allocated memory has been located or not yes memory has been allocated here because this declaration is what see it's declaration is not a concept in Java basically it is definition at this time only you here
you write down any value or not you initialize this student AG or not memory has been allocated right now integer take four bytes generally like in 64bit machine or recent you know machine maybe it depends on your machine also but Generally it takes four bytes you can check this size so memory four bytes has been allocated to this now what does this declaration means this concept is in C basically see in C maybe if you haven't heard this thing don't worry you can leave it there is a word extern extern in a so this is
declaration at this point of time memory no memory space has been allocated to this variable extern means it has been defined somewhere else In different file and we are using that variable in different file in different C program have you used this concept before like we have this extern scope also external variable Global scope and all but if you haven't used this don't worry this extern means this thing means we have this variable defined in some other file and we are using in some other file so what once you are using this variable in that
other file at that Time memory would be allocated so if you are using this extern keyword in this case only it's declaration means no memory would be allocated but if you write simply int a in any program it means memory would be allocated this is definition but this concept extern is not in Java so there is no such kind of thing like declaration actually every time it's definition memory would be allocated if you write simply like this I hope this difference is clear now two terms are more assignment and uh assigning a value to a
variable and initialization this thing also I'll show you I hope this difference is clear so don't worry there's no such kind of thing declaration always if you write this thing memory would be allocated and generally generally everyone say that it's variable declaration because if but but more precisely declaration is this thing but There is no declaration definition you know different different in Java it's only space would be allocated so generally you can call it variable declaration also don't worry it's okay but actual meaning I have told you right okay now let me just uh show
you one more thing now what is this lals and expressions so if I write here something like this rather than this if I write here Student age at this point only at this point only I am writing here is equal to 21 so this 21 this value this is a literal it's a constant fixed value right so this we generally it's it's called as literal it's integer literal 20 21 22 these whole numbers so this fixed value are known as literal in Java in Java these are uh you can say that integer lateral okay now
this this proper complete Statement this is known as initialization initialization means at the time of Declaration Only You assign value to that variable this is initialization of a variable second is one more thing is uh assignment so if I take not this thing if I take something like this suppose int if I take uh ID or role number and ID is equal to 100 this is known as assignment or assigning a value to the variable using this is operator assignment operator we'll see what differ kind of operators but this assignment operators will do whatever at
the right hand side the value that would be assigned to the left hand side the name of the variable so this is assigning a value to a variable or assignment this is not assignment this thing is initialization because here we Are declaring proper data type name and then value here we don't have any type already we have declared this variable or defined this variable here and after that I'm assigning this value this is assignment but this is not possible every time I'll show you the cases when this thing is not this thing will not work
this thing we cannot do with instance variables instance variables have told you those class level Variables in a class we have taken int R number int age student name there we cannot assign a value something like this for local variables we can assign I'll show you practically what are local variables and all so the cases I'll tell you when this assignment will not work right so I hope the difference is clear declaration definition initialization of variable and assignment of a value to a variable what are variables now next thing is why these Are called as
variables because the value you assign here that can change suppose I have assigned a value int student is is equal to 21 now at next point of time in my program I can write something like this student age is equal to 23 this is fine so I can change the value at first student age was having 21 right now student age is 23 that is why it's known as variables I mean the value can vary the name is Variable right and we can access these variables by its name only if system do out. print Ln
you simply write this name of the variable it it will give you the value so just have to tell you just have to tell the compiler the name of the variable and it's the Heche of compiler the compiler will search in the memory where is that variable and it will show you the value you just need to remember the name only In memory memory is something like memory is simply you can say it's a long tape of whites we are having in memory memory cells each individual cell is representing a a bit either zero or
one right something like this a long tape of bites this is memor somewhere someone represent this vertically also these are the blocks someone represent this in horizontal form also somewhere you will see like this kind of block and here we have like this kind of thing these are Blocks in memory memory cell these are memory cells something like this right so just in any way you can represent because we have to just visualize this thing actually we don't know how the memory is stored or looks like in computer but we we know this thing that
memory is having many memory cells and each cell represent a single bit either zero or one four bits form one bite and then different different word length also 8 bit length 16 bit 32bit and this Kind of thing okay that is a different concept now see he the we have int it int means the data type tells the compiler that it is the name of the variable and int means four bytes would be allocated to this variable so here four 4 by means 4 into 8 32 bits so like this 32 bits would be allocated
to this variable student a so like this suppose from here and till here we have these are 32 bits total in 32 bits so the name name of this would Be given student age and this data 21 would be converted into obviously first zeros and ones and that would be stored here 21 so rather than representing this each individual bits and all we just for a sake of Simplicity we represent something like this just one block and there 21 has been stored and the name is name of this variable is student age that's it and
obviously this will having some address we don't know the address that would be In hexadecimal form but suppose for Simplicity purpose to understand this thing let's suppose address of this variable is thousand but actually definitely not thousand it would be in some hexadecimal form and all okay so suppose address is this name is this and value is this so just a named memory location we simply access the value using the variable name we don't have to remember we don't know where this is store the address and all This is hadc of compiler compiler will search
in that thousands of cells in the memory where is this address where the and what is the value and it will give you the value see how smart the compiler is right so you can access variable using the name itself right now there are types of variables but those thing we will discuss with the help of practical I'll show you right now but before that there are some naming rules for variables and what are those uh Naming rules means you cannot it's not like that just put any name no there are some rules to name
a variable so what are those rules let me just show you those rules here this simply initialization and declaration and all this thing we have discussed this slide we will discuss practically what are type of variable local instance and static okay now first see the naming rules for variables so variables contains alpha Numeric value alpha numeric value means either alphabet A to Z or capital A to Z or numbers from 0 to 9 or two special symbols are allowed dollar and underscore these things are allowed in the name of a variable right variables are always
case sensitive or start with a letter must start with a letter either this or this or can start with dollar or underscore but cannot start with the number this thing is not allowed variables must begin with a letter or Dollar or underscore variables are case sensitive case sensitive means if I write age and if I write age these two are different variables here a is small here A is capital Java is Cas sensitive next is no keywords are allowed keywords I have told you there are 51 keywords in Java so those keywords are not allowed
we cannot simply write like in and again I can write a like class because class is an keyword so this will give error you cannot write int int name of the Variable is again int this is a keyword but you can write this thing int and int B Java is case sensitive so this thing is allowed capital in this is not keyword small in is keyword but you should not take these kind of name of the variable you should avoid name should be descriptive should not be matched with any keyword descriptive means age or student
age would be better than simply age marks or student marks would be better rather than writing Simple int a and having 21 better to write student a 21 so that by looking at the name of the variable we know that this is name of the variable no whes space are allowed means in a name of a variable white spaces are not allowed if I write student a something like this student Space Age int this will give error white spaces are not allow camel case use camel case for multi-word keyword camel case means Something like this
no white space is there here uh student age camel cases first should be small and every other word the first letter would be Capital so this is lower camel case basically it's not rule that you have to do this thing but it's just a convention they have said the makers have said this is a convention everybody should follow these Rules okay to make their program good and you know it's always better to follow the conventions right now let me just show you some variables these are valid or not see these are valid variable names age
here I have dollar see dollar price this is valid variable name can start with a dollar first name valid R number in between I have underscore this is valid at first we have underscore this is also valid count one some 1 2 three digits are also Allowed so these are valid variable names right now some invalid variable names are 1 2 3 ABC at starting digits are not allowed first hyphen name any special character uh rather than those dollar and underscore not allowed hyphen is not allowed class it's a keyword not allowed my variable is
having space between these two not allowed so these are some naming rules for variables now let's discuss not constant first let's discuss The slide types of variables I hope Basics are clear I'm going further deep we can skip this also types of variable it's not that much like required generally uh you will not get this thing right here types of variable no but I'm going to cover in deep whatever you will not get anything you will get you know in this course so we'll go a little bit deep I'll discuss what are local variable instance
variable static variable so stay focused And try to understand if you need a you can pause the video take some break and then come okay I just hope things will not be you know overwhelmed for you guys just don't you just don't feel that ma'am we are what you are talking and we are not getting anything I just want to give you a if we are discussing variables so I just want to give you a deep kind of thing deep intro I know to this extent you can understand but I'll not give I'll not go
into that that much deep it's okay you will understand these three types so let me just show you these thing with the help of example practically till now whatever we have discussed let's do practical then we'll discuss further right so let's open our ID intelligy ID or you can use any other ID as well or you can go for any text editor also notepad also there also you can write code in the terminal you can run with Javas command and you can with Compile with Javas command and run with Java and the class name so
here in our first project we are having in SRC hello Jen power of two three files we have right so rather than these three files let's create one more file now one more new class Java class and the name let's take simply student so this thing you will get automatically public class student and see you here you see opening Braes here and closes closing Braes here many Programmers developers do this thing but some I follow this also the opening bra is here closing brace is also here but generally this is the convention you can follow
any one of these there would not be any error kind of thing but this would be followed generally by the programmer so we'll follow this convention only now in this class let's take a simple example because we haven't discussed till now the data types and all so we'll not go Into that much deep or we will not take different type of variables we'll take some simple variable that is int integer because everybody knows integer what is integer right the whole numbers in and I'm just taking uh let's say take row number is equal to suppose
RO number is uh 22 simply I'm taking this thing one one variable only RO number is the name of the variable in is data type and the Value is 22 okay now here let's take one method display method is simply or you can we follow this also this extension and this convention method is simply like the functions functions are having within this we will be having maybe different different one or more instructions and that would be executed to perform some specific task those are functions or methods we will see these thing in detail but here
we will will simply Write down here one statement so how to define these methods simply the data type void means it will not return anything displays the name of the method and you have to write down these bres opening and closing round bracket so this and then opening and closing brushes these brushes here we write down body of the method so this is simple how we Define a method or a function here we don't call this function these are methods because these are you know bind These are bound to this particular class student these are
not free floating in the program it's not like that anyone can access and these are free free floating no that's why we know we call this as methods and this is known as attribute we call these as attributes sorry it's attributes and one more thing we can call is that these are instance variables I have told you types of variables or class level variables in Variables or class level variables because just after the class outside of any method I'm declaring these variables I'm initializing this variable I'm taking this variable so this is instance variable or
a class level variable right or whatever variable or attribute you take those are also known as state of the object these represent state of the object and methods represent behavior of The object object how the object behave like my status my name my role number my age or whatever attributes I have my color or these kind of thing and my behavior is or my methods are what I can do I can teach I can dance I can sing I can cook so these are my behavior this this show my behavior so methods show behavior of
an object now in display simply I will not write anything complex just system. out. print Ln and I'm simply printing the name sorry the role Number of the student okay R number is and here we write a plus symbol and simply the name RO number this is how we print something see whatever you write in double Cotes that is what a string string leral so that would be exactly printed on the screen now plus plus is addition for addition but here plus means concatenation when we use plus here in you know with Strings it means
concatenation concatenation means whatever is there on the right hand side that is R number name of the variable so value would be taken from here 22 whatever the value here that would be appended to this string that would be appended to the string so first the string would be printed R number is this and after that R number whatever the value of the RO number 22 that would be printed so this In this context this plus meaning is concatenation it's not just addition okay now obviously you have to uh call that you have to create
that main method also to execute this class otherwise the class is of no use so either you can write down within the same class a main method or you can take another class any other class and you can write down main method there that is also fine but right now what I'm Doing public static void main with in the same class I'm taking main you also just practice this with me okay string ARS this is the body of the main and now I told you how to access the members these all these things attributes and
methods all these things are known as members of a class so you can access these members using the object of this class only you cannot directly Access if static variable or static method is there then you can access without creating the object of that class but these are not static so let me just show you what error you will get if you print something like this system print L and in main I'm simply I simply want to access whatever the role number is that's it so it will give you error see it's red without running
also you can see you Can overover the cursor here and you can see obviously this err this semicolon but again nonstatic field r number cannot be referenced from a static constant context if I run this let me just show you the error see see now nonstatic variable role number variable is this variable RO number is non-static it's not static cannot be referenced from a static cont context Because directly without name of of a class if you want to access then that thing that variable or that method should be static otherwise you have to create object
if suppose I write here static in that case if it will not give any error let me just run this and I'll show you what thing you will get here c22 it's running it's not giving any error but it's not like that for every variable and everything we use static no we will discuss about static final these Key words later but this is not at all a good idea to make everything static just delete it generally we take these variables so this this is instance variables okay now to access this we have to create object
of this class how to create object of a class just name of the class that is student object name object name suppose I'm taking o b j or any name you can take is equal to new keyword and student this is basically Default Constructor name of the class with these round brackets this is Constructor as the name is same as class name so this is Constructor and no arguments are passing this is known as default Constructor this thing also we'll see Con in Constructor but for for this point of time you have to remember that
this is the way to create object of a class right obj this is like an object of a class student so if you want to access this R number so so you can write Down here obj dot R number and if you want to access this display this method also then you can simply print obj do display that's it what output you will get pause the video and think and then run let's run this and I'll show you what out you will get 22 R number is 22 2 line because at first control will go
to here I mean the Jvm directly execute this main there second line is to create object system. out print means whatever the value of R number that will be printed then it will call display now control will find out where is the definition of this display obj do display so let's call this display here and it find that there is one line r number is and it will print 22 right okay this is simple variable now let me just show some extra here now see let me show you this thing if let's Suppose do this
thing only in troll number and here let's assign uh R number is equal to 22 this will not work because this is instance variables these are class level variables okay these are not local variables local variables are within any method if you declare any variable those are local variables so if within this method I I take like INT in a is equal to 23 or in a is equal To 45 this a variable this is local variable local to this method right but these are instance variable or class level variables every object of this class
can use these variables instance variables right now this variable can be used within this method only int a outside of this method the variable is of no use okay these are local to this display Only now let me just show you this thing this thing also see it will give you error here at this point of time I have told you assigning a value something like this an instance variable is not allowed it will give you error let's run this and see what error you will get here see Java identifier expected in third line see
in third line third line Java identifier expected so identifier means any name of a variable class or method is identifier So here it is showing like a non class role number so you have to give here like the type of this so like this assigning is not allowed so let's just delete this and let's assign the value here that is 22 but here the assigning is allowed assigning of a variable suppose I'm not assigning it a something like this and a is equal to 67 or 45 or any this one is allowed this is assigning
or in system out print Ln if you want to print this a So we can simply print a is equal to and same plus and a so it will give you the value rather than here we not print here obj we just call this display so let's run this and I'll show you what output you will get here first it will print R number 22 and then a is equal to 67 right okay assigning is done now initialization this is initialization at the time of Declaration only we are initializing but Reed declaration is not allowed
or let's just show you Reinitialization at first suppose it was 56 and again I have changed the value a is equal to now 67 so we have reinitialized this value and that is it becomes now 67 or suppose it becomes now 78 so the recent value or the updated value it will give is that is 78 so we can change the value in the program this is what that's why it is known as variables next thing I want to show that is the this is simply Lateral integer lateral any fixed value these are laterals expression
also I have told you uh when we were seeing the syntax then there I have written the type name of the variable equal to literal or expression expression is simply uh combination of some operators operant variables and those evaluates to a single value see these are fixed value no evaluation would be done but for Expression some evaluation would be done suppose I write here suppose I write here in number is equal to this and then int next R number is equal to whatever the role number + one so this this is expression this needs some
evaluation some evaluation would be done and then it will give a value so expression evaluates to a value RO number 22 + 1 that is 23 Now 23 would be assigned to next RO number you got It if you print here this next R number is plus and whatever this next R number the name of the variable so let's not print this a let's just print R number and next R number so it will give you Ro number is 20 22 next problem is 23 so this is expression okay so here rather than this we
can we can do something like this also like a is equal to This a + 1 or a + 2 + some expression 5 into 7 some complex expression also we can use so what the value would be suppose right now a value is one only just to just for a Simplicity purpose A1 so 1 + 2 is 3 3 + 7 into this would be I think 35 and 38 let's just print a value and see what output you are getting see a is 38 so at right hand side we can use any Simple
fixed value literal or we can use any expression as well expressions are also allowed so it's not like that simple value we can add that's why I have told you this is the proper syntax type name equal to literal or expression and this is optional it's not like that you have to compulsory write down here the value you can simply here write semicolon and here now here you cannot use this value a because you haven't defined this value first so rather than A you can use here 3 + 2 and something like this so this
is allowed but here the assignment is not allowed something like this because this is instance variable and static variable I have told you static variable means if you declare it a or one more variable let's declare that is static and that is static and static variable generally we declare those things static for which we want like some default value would be Initialized default value for static would be zero so suppose static and int and any other thing I am writing like maybe a variable name X that's it so see we haven't taken any value but
still if you want to print here suppose let's print here in main obj dox simply let's just don't call this one thing I'm printing obj do X let's run this and see what it will print see Zero because we haven't Assigned any value to this okay so by default zero would be assigned if without object name if you access this then also you can access this let's run this because this is static variable if variable is not static then we cannot access is zero right so this is static so class level variable basically generally we
some variables we can make static we will see proper what is static What is final at all don't worry worry now let's let's do one more thing let's not make it let's just delete this variable and just don't we are simply taking in roll number we are not initializing this and we are simply printing this we are simply accessing accessing this value obj dot RO number what do you think what it will print see we haven't initialized this Here we are simply printing this obj do roll number value let's run this and see okay cannot
find the variable next R number in class student in 12 fine you are using here next R number but here we have commented out this thing these are comments these are not to be executed by compiler so if you think that if you want that this particular line would not be executed by the compiler just put these comments double slash Here so let's just comment let's just remove the comments here okay now you will not get any error I hope see value is zero right although we haven't initialized this R number at this point of
time but still it is zero so by default I have told you the value of int is zero but only instance variable in only in case of instance variable if you take int a here and I don't assign anything let's just delete this Line or rather than delete let's just comment this okay let's just comment this now I want to print this a see it will give you error you can already see a red line variable a might not have been initialized let me just run this and I'll show you the error variable a might
not been initialized at line number 11 line number 11 so the thing is because a is local variable so local variable will not get any default value compiler will not initialize any default value to local variables okay you have to initialize these first before using these or if you don't want to initialize you can assign a value after that a is equal to 6 this is assignment before using you can assign a value now this will work fine this will not give you any Error so I hope this default value thing is clear to you
guys when the variable will get default value and when the variable will not get default value six is not going to be printed because we are not calling this over obj let's call this obj we are not calling this display function so let's call this and show you a is equal to 6 right so local variables will not be initialized by default by the compiler But instance variables are initialized and default value for INT is zero okay and one and one more point I just want to clear here see while you are writing a class
while you're defining a class so I have written here in R number and this so at this point of time no memory has been allocated to this R number maybe you think that ma'am you said in Java it's definition so memory would be allocated there is no Declaration although it's definition but the thing is because this class is simply a blueprint if you do not create any object then what is the use of this class how memory would be allocated to these things this is just a logical thing just a blueprint like Ive told you
map the the map is simply a blueprint there's no space there is no land you require for that map if you Have a map you cannot say that I have 10 buildings and this is the map where is the actual building where is that land you haven't paid something but when you want to construct then you pay then you buy that land and then you construct your building using that map and actually at that time you are constru constructing your building and then you reserved the space you have that land now you can say that
yeah I am owner of this building I have the space Okay same with class class is just a blueprint without creating object these are nothing if you want to do something with the class you have to create object and while creating object memory would be allocated at this time so in number no memory would be allocated for this at this point of time when you create object at that point of time memory would be allocated I mean basically this new keyword when you use this keyword then Memory would be allocated basically right so try to
understand this thing logically this is just map no land is there for this role number this is bedroom so no space or no land is there for this bedroom until and unless you create a building so this is I'm creating the building I'm constructing the building and now my bedroom has a space or a land right but the thing is static if You take something some variable static here at this point of time you can see without creating object of this class I don't want to create object of this class so let's comment out all
these lines and let's just access the name of the static variable is int X right so let's access this x here what do you think everything is suppose everything is Commented there is nothing in this I have simply public class student a variable static variable index main function in main I'm just printing X No Object nothing else now see what what do you think what output you will get now maybe you will give this logic ma'am you said no memory would be allocated unless and until until we create an object of this class so obviously
there is no memory for this x then how this x would be printed right Yes the logic is right but there is one thing for static members for static variables memory would be allocated when this class is loaded only if you don't create any object still for static only this is the power of this word static for static memory would be allocated and four bytes would be allocated and by default by default X is zero by default X is zero for all the static variables this would be Initialized with zero the value would be initialized
the variable would be initialized with zero so without creating object you can access this X and you can see that you will get an output zero that is you can say the beauty of using static keyword we will see later in detail what is static and all because for accessing static variables or static methods you don't need object of any class and if you don't need object of any class then Obviously memory would be allocated if you create object or not memory would be allocated value would be stored by default zero would be stored initialized
by the comper itself and you can simply access this thing so this is one more difference between instance variable local variable and static variable right I hope you got this thing so you try out you practice different different things right now suppose I want to take a variable int and suppose Class it will give you error see public class student so what is this class it's a keyword we cannot use this for a variable class is equal to suppose a variable integer value I want to store eight let's create an object of this and let's
obj do class let's run this and see what error we are getting here identifier expected in three so it's not like proper given identify or name of the variable we cannot take this but if you take Class C Capital what do you think see C is capital here at this point of time you will get eight value eight value would be printed C but it is not recommended to use the keyword name just by modifying this with capital and all it's not recommended you can use but better to not use simply use a descriptive variable
name int age or anything you want to take or maybe count or anything or this underscore count is Also allowed see you can simply access here underscore count underscore count class is not there this will also print so now you try out there are you have the rules of the variables you try to apply all the rules of the variables try to use keywords try to use dollar space and all in these keywords and use these okay now next let's discuss the next concept here so the the SL types of variables I hope clear to
you guys local variables Declared in methods or maybe Constructors or blocks have limited scope block means opening Braes closing curly Braes within this is known as a block if you declare a variable within that block the scope is within that block only means we can use that variable within that block only the scope and all we will see later videos in detail instance variable know static variables declared in class outside any method these are class level we don't Declare instance variable within a method outside static class variables declared with static keyword and shared among all
the instances if you write static keyword then that is like static variable I have explained the difference and all okay these are local variables in count is equal to Z inside a method this is instead simply string name in a class outside method static static final double pi value is this shared constant Now what is this final this also I'll tell you right now in the next slide Okay naming rules we have discussed valid and invalid variable names now what are constants in Java see you have seen that thing you have seen that thing here
see this thing final variable name was static final and then this this was double then pi and the value is 3.14 something like this right see So constants are basic variables means once you assign a value to those variable you can reinitialize those variables during the program you can change the value that thing I have showed I've shown you constants means if for some variable you want that let's just fix the value at first starting of the program and during the complete program during the bill of the execution this value should not be changed anyone
cannot change the value you just make That thing that variable constant so constants are simple these are variables whose Val values cannot be changed once they are assigned right they are declared using the final keyword now this final keyword is the one which makes this this is simply the type double is the data type Pi is simply the name of the variable this is simply the value one thing that is different from The one we have discussed variable declaration is this final keyword so this final keywords make this variable a constant so this value of
this Pi cannot be changed during the program execution right so when you want to store fixed values then we use constant like like the Spy value we know the Spy is fixed it's not like that somewhere in the program Pi would be uh Pi would be 2.14 no why would be fixed right some configuration settings or mathematical Constants like Pi or some commonly used values like days in a week we know seven so this we can fix at starting of the program some max value if you want to use in your program so let's declare
max value is equal to 100 or th000 max value cannot be less than this cannot be greater than this or suppose let's one example is in banking account there is one uh one thing that is minimum balance minimum balance should be suppose Th000 so let's make in that application this constant minimum balance should be 1,000 in your bank account otherwise maybe some penalty or some kind of thing happened so for these if you want to fix some values then we use constants we generally don't use constants okay have to make a variable a constant use
final keyword now it will increase the code readability and maintain because as the values purpose is evident From its name because see days in a week we know this is like number of days in a week by looking at this we can know that this is okay days in a week or by looking at the name we can know that mix okay this is maximum value of something so this is going to make your program more readable and maintainable okay now we we can use this constant wherever you use regular variables such as in calculations
comparisons or method Arguments anywhere you can use this spy or this max value or this St in a week in any calculation in any method argument or see in any comparisons also it's not like that if you made this is a constant then you cannot use in your program anywhere where you use a normal variable you can use a constant as well but the fact is that the difference only is what variables can be reinitialized we can change the value of those variables but constant value once you Fix that value you cannot change the value
during the program during the program execution you got it if you want to change the value of the constant at starting suppose I have declared minimum balance is this and suppose in my program at 10 places or 100 places 100 places I'm using this variable minan balance and this is constant this is like final and this is like like long or long or Suppose int or any data type at 100 places I I'm using this so if you want to change if you want to change the value of this Min balance so you don't need
to change at the 100 places or the 100 times you are using in the program no you simply change here suppose balance I want to make 10,000 minimum balance you just change this here and this would be reflected everywhere in the program where you are using this variable so so This is going to increase the maintainability also right one more thing is what yeah during the program like anywhere in the program suppose this is like from right from this is suppose I'm writing the program and here I want to change the minan value so suppose
Min value Min balance is equal to I want to make 20,000 you cannot make this this will give error definitely because this is Constant simply you can change here only if you want to change at first where you have initialized this constant there only you can change otherwise in any in the program you cannot change so it will restrict someone else to change your constant values if you want that this should be fixed and if some other person is reading your code or using your code so and you want you want to restrict that person
that this value should not be changed so make that Constant right let me just show you this thing practically so let's make uh on Del this keyword final it's final intore count is equal to 8 okay now obj do count is this and if you want to change it anywhere in the program or suppose I'm using this here let's remove the comments of this void display and uh let's try to change it within this Method that is underscore count is equal to 9 I want to change it so it will give error let me just
run this and show you what error you will get here cannot assign a value to a final variable count it is a final variable if it's not final variable let's just delete this final and now in this method within this method I'm going to update this I'm going to reinitialize this let's run this and I'll show you This time okay variable R number cannot find variable R number in 10 okay we have just commented out this R number so let's uncomment this let's remove the comment of next R number as well and let's just take
RO number is equal to 22 now let's run this save this and run this variable a okay we we haven't we have deleted this a so let's come and out this line also a Also let's run this see it will give you eight Y8 because control will go here and it will execute this line only we have commented out this obj this display function we are not calling this display function so let's uncomment this and now we will call display function and now the count would be nine so rather than here printing a let's access
the count so count is equal to this let me Just show you now at first you will get count eight and next time you will get count at first it was eight and next time it is nine so it's not final it's not constant but if you make it constant then you cannot change it here see the red line cannot assign a value to a final keyword so this is what a constant is now we have made this constant but generally we make constant to like some pi value or max value in a program or
I have told You the example as well right so this is how we can declare constants in Java using final keyword right so next let's discuss what are literals I have discussed this also like simple when we're writing int age is equal to 10 so 10 is or 21 is lateral so laterals are fixed values these are it's not like that we call these constants constants I have told you what are simply constants in Java somewhere you you somewhere you know you find out That they known as these are constants but that's not precisely these
are constants these are basically literals although the value is fixed so we can call somewhat constant but not those constants literals are fixed values that appear directly in the code in the source code they can be assigned to variables used in expressions and do do not change during program execution so if I write something like this in age is equal to 21 so this is what a Literal obvious this is fixed value you can directly assign this value to this variable and this 21 21 would not be changed although if you do age is equal
to 22 means the variable value has been changed it's not like that 21 becomes like 22 actual value actual value of 21 is 21 but now it's 22 age becomes 22 in this context you try to understand this thing right okay so there are different type of literals in Java so we have Integer literal and floating Point literal so the these comes under numeric literals integer means whole number it's negative or positive or zero floating means having decimal points like this right now basic difference is is what these are float in floating point we have
float and double so if you write something like this like 3.14 so by default compiler consider This as double any floating number by default compiler consider this a double number double data type there are two P types we'll discuss this thing float and double and if you want to consider if you want to specify that this is a float not a double so you have to specify at last F either capital F or small F then compiler will consider this as a float number what is the difference for float less memory would be allocated in
the Computer memory less space for double more space would be allocated so if you want to you know store less know short number not a very large number you can make it float or for a large number keep it as it is like it by default it would be double so don't keep here F okay so these are float and double literal also this is float and this is double so these are floating Point laterals next comes to character Literals so there we have character laterals means a symbol Single Character if we assign like if
I write here care and uh choice or some something is equal to a or if I write here care grade your grade is a so in single quotes a single this this is single quotes whatever you write that is a character literal or WR here one in single quotes that would also be character literal not Integer because you have put here this one in single quotes So this would be considered as you can say character okay if you write down here something like this like uh Slash in some back slashes are also in character literal
right so slashing it is character literal slash T is for tab this is also character literal there are more back slashes also those are those comes under character literal string literal means like Jenny If I assign string name is equal to Rahul in we write string in double quotes So this Rahul is nothing but string literal we call it simply string but if you say more precisely then it is string literal these are fixed values right any values 10 20 name of anything or this so different type of literals are there but in double quotes
if you write this this is also string literal in double quote if you write single A this is string literal this a is character literal because it is in single code so see Focus this thing you have to differentiate character and string even though it's a single character but it is considered as string literal because it is in double quotes by the compiler right now these are basic but more literals are also there in Java true and false these are not keywords these are Boolean Literals so we can use these also I will show you
how to using data types Boolean is also a data type in Java primitive data type like bu we have in I guess in C++ we have bu it will take two values either true true or false so same Boolean we have true and false these are Boolean literals we cannot use this true and false as a name of a variable although these are not keywords but still we can not use these name of a variable because these Are Boolean literals right now like is correct is equal to True like this we can use these Boolean
literals true okay now first you have to write like Boolean here this would be same line Boolean then name then the value Boolean is data type null literal is also there in Java it represents absence of an object reference this is something different if you don't want to go into that much deep you can just skip this it is not these kind of literal it is it Comes under reference object reference right Java basically we have two types of variables primitive and reference variables right so now let me show you this thing this thing first
so I've told you like this 22 this is integ a literal if I write here like string name is equal to Rahul this Rahul is string literal right if I write some character care and this that would be character Literal but if you want to assign like let's take let's use null literal here like int R number or this suppose here I I want to don't just update this let's here let's take int a is equal to null that is not allowed because see maybe you say that here I'm using integer literal here I'm using
string literal in float I'm using float literal so let's make this null a is equal to null but it will not work it will give you error see required type is Int but you have provided null it is a type null okay you cannot do something like this so null basically I told you represents absence of an object reference object reference now see in Java string is not a primitive data type not built-in data type string is reference data type reference I in variables also I have discussed I have told you here in variables see
variables I have told you Basically are of two types primitive and reference this is little bit in deep about variables if you don't want to go into that much deep it's okay till now we have considered what variables that is fine that are primitive see like primitive dat type we have in float double Boolean character yeah and these are primitive dat type so if a variable store some This kind of value like 10 or 10.1 or a simple character like a or like if we write here F so that variable is known as comes under
this category primitive category reference means when you are creating object suppose student is a class and we are creating OBG that we have created and this thing we have written new and the name of that class this is basically Constructor so this a obj basically obviously this is you can consider you can map this something like This int a is equal to 10 a is name of variable int is the type here student is the type but type here is class and we have defined this type obj is name of that variable but this obj
comes under reference variable reference means see this a will store the value in memory we have a and there the value is stored that is 10 so this will store some value float also some value would be stored for character also that value Would be stored right and for double also that value would be stored but here this obj is a variable but this is reference variable why why because this will store the reference of this object in memory we have created created some block of memory according to whatever in student we have whatever the
members and this obj is just reference just just pointing to this memory block there we have like in student we Have name of the student rle number of the student right or not name let's take just R number and marks and both are like int and Float so some value would be assigned int will take four bite like float will take four or eight bytes according to that specification we'll see so the to memory would be assigned here right how many bytes that would be according to the members that class have so this Object obj
just a reference just pointing to this this memory location it's not actually the containing the value so it doesn't come under primitive variables it comes under reference variables so if you are creating object of any class then that that variable would be reference variable okay I hope you got this so null is also that kind of thing null Represents absence of an object reference like this let me just show you now see for INT a we cannot write down here null but if I write here string name or let's take here null this is fine
this is fine because string is actually a reference data type it's not primitive data type so name is what string is basically a class in Java name is basically we are creating object of this class right and That's why the object will contain the reference of whatever is here and null means there is no reference so null in this case we can use null but for integer for character we cannot use at the right hand side null right now let me just show you for this also let's not create an object let's keep it as
it is or let's is equal to null so see D reference of obj we are now D Referencing obj do count we want to access this count using obj but this obj this D referencing of this obj will produce null pointer exception because it is representing by it's like absence of an object reference this object yes we have created a variable or object of this type but no memory has been assigned it is pointing to what there's no reference it's null it's not pointing to any memory in the computer's memory and if you leave It
blank then also you will get error it's not like that you have created an object something like this no memory has not been assigned same error you will get variable obj might not have been initialized you have to initialize this and how to initialize is equal to new and student now it will work fine okay so you can leave this if you're not getting what is null and all You can leave this because it's not that much needed right now later we will use this but if you got this then it's well and good so
you use try to use this null at different different times in your program in method or here or something like this okay or you can use Boolean also I have told you true and false these are Boolean literal so you can you cannot use these as a name of a variable suppose I want to use int R it will give you error unexpected token if you want to assign like eight in true unexpected token so it will give you some error see at line number five Java identifier expected identifier means name of the variable expected
true is not a name you cannot use this true or false if capital T this will not give any error this will work fine but better to not use these kind of name for variable declaration right for Boolean you can use something Like this like Boolean and and name of the variable I'm taking suppose is correct is equal to true so this is I'm using Boolean literal at this time this is Boolean literal is correct is the name of the variable and Boolean is the data type so if you print this is correct suppose here
so I want to print this Within this display method and uh let's just call only the display method that's it are we using count here no we are not using count underscore count so let's comment out this line right let's run this and see what output you are getting see is correct is true so it is printing the value Boolean literal value right so you can use Boolean leral also some in this Way although you cannot use like in R number is equal to true this will give error the required type is integer and you
are giving a provided is Boolean this is not allowed right this is like mismatch in the data type this thing we will see next lecture what are data types because this is all about variables now I hope you got what are literals variables and expression also I have discussed like this kind of thing If you write this is what expression which is going to be valuated to a single value then that is expression so I hope this is clear to you this these are simple we are using these I hope you got this if you're
not getting this null you can just skip it for now buan are also simple okay this are basically you know not just primitive these are in case of like objects kind of thing but I hope this this Lal is also not not that much stuff Expression also I have told you these are combination of some variables okay sorry combination of variables operators values method invocation and that would be evaluated to some single value like the these are kind of expression we can use this is also an expression Is Res is correct is a suppose variable
Boolean variable is Java fun is also a variable and I'm we are combining this with the help of this as an operator logical and operator we Will see operators so this will evaluate to a single value right how this evaluation would be done if a little bit I tell you then suppose is correct this is true we have declared this true this is Jaa easy this is false so true and false would be false logical and for logical and the truth table is if both values are false false false then it will give you false
if both are true it will give you true if anyone is false false true true false Then it will give you false this is truth table for logical and operator don't worry we'll see this later when we discuss we'll discuss all the operators so these are just expressions in Java we have seen how to initialize variables with literals and expressions so with literals we can initialize with something like this simply age is equal to 25 so this is integer literal this is double this is string this is character this is Boolean literal this is how
we can initialize with Expressions this is correct this one is also correct some suppose at first obviously you have to initialize these variables like int a is equal to 10 int B is equal to 20 so this is the expression 10 + 20 would be Valu to a single value 30 and that would be a assigned to sum Boolean is result is a variable age greater than equal to 18 Suppose at first in my program age is equal to 17 so 17 greater than equal to 18 no so this would always be false this is
how we can use expression and literal to evaluate now you have to practice some variable declarations so declare a variable named count also declare an initialize a double variable Pi declare a Boolean variable named is Java fun within a class let's modify this within a Class any class name you can take according to you maybe your name you can take as a class name so these three would be the variables there right and you just call a method display and just display all these three values okay with the help of Life create object of this
class this is for your practice assignment you have to do this thing right now next is same you have to practice Variable reinitialization as well this thing I have told you first we have initialized then again we have reinitialized and then we can print the value of x you can practice this thing also you have to practice these things now next is identifier in Java so till now we have discussed keywords variables constants literals expressions s last is identifier identifier is nothing but name of a variable whether it's name of a class Name of any
method name of these things are called as identifiers identifiers means used to identify something my name Jenny is an identifier for me it identifies me that I'm Jenny so identifiers are names given to various program elements such as variables methods or classes or Constructors also we'll discuss so name of Constructor those are identifiers just to uniquely identify the program elements why we name Variable we have discussed why we use variable names scope include names of variables method classes and many more things these are identifiers importance is increase the code readability and maintainability of the program
this thing we have discussed these are related to the variable name we have discussed nothing else but simply variable not is not just an identifier identifier is name of variable name of class name of method name of any Constructor these are name of any interface these are identifiers there are some naming rules also for identifiers same as we have discussed for variables same naming rule characters are allowed same A to Z A to Z 0 to9 dollar and underscore first character must be either letter or dollar or underscore first character cannot be a digit length
there is no limit on the length variable name length can be anything any number of words you Can take but generally we keep short and descriptive name like age or student age rather than writing a long variable name right no white space are allowed between words and identify also case sensitive okay now that's it for today's class I hope you got whatever we have discussed so now you have to practice you have to practice of all the variables declaration and all don't go into that much deep you haven't Discussed data types in the next class
we'll discuss data type different type of data type then you can use you can do some more practice but for now you simply use int and float you can use this thing for declaring a variable or simply use int and how to assign how to reinitialize what is instance variable how to Define local variable if I use static then what would be the effect if I use final then what would be the Output so try to use these things try to reinitialize the variable try to use keywords for a variable name try to violate the
naming rules of the variable name and then see what error you are getting right so you have to practice now practice will make you perfect right once you practice you will face many errors and then you will be habitual of seeing those errors and by looking at those errors only you will get to know that okay this is the error this is how We can correct this error so it's better to face more and more errors at this phase only rather than facing when we are actually writing the Logics for large programs okay so now
that's it for this video now I'll see you in the next video till then bye-bye take care