Introduction to Variables and Data Types in Java

Imtiaz Ahmad
A free video tutorial from Imtiaz Ahmad
Senior Software Engineer & Trainer @ Job Ready Programmer
4.6 instructor rating • 12 courses • 255,645 students

Learn more from the full course

The Complete Java Certification Course

Become a confident industry ready core Java developer and get certified as a Java professional!

17:24:55 of on-demand video • Updated January 2021

  • How to write Java Programs from scratch and have loads of fun in the process
  • Go from zero to hero in writing industry ready Java programs
  • Gain enough experience to pass a core Java Programming interview and perform well on the job
  • Be prepared to take the Java Certification Exam for OCAJP 1Z0-808
English [Auto] Either this lesson or talk about variables and data types. First we need a class I'm going to hover my mouse over here on Java basics right click it go to new and click on class. Now we're going to get into all types of details about what classes later on in the course. So don't worry about that for now but we're going to call this class variables because that's all we're going to be learning about. And just a naming convention here the first letter of a class should be capital. That's just a standard that everybody follows that you'll see in the industry so make sure you leave all your class names with the first letter of capital and just check this thing right here. This is the main method that we need for the program to run successfully. And we're going to get into details of what all those what all these keywords such a static and void and you know public mean later on. But for now you can ignore them and just take them for granted and everything all the code they're going to write in the next few lessons is going to be inside of these brackets. The Java interpreter needs this main method to run the program inside of this main method. We're going to write all of our code so let's start with variables a variable in computer programming is a place where you can save data. All right so for example I can have a variable called X. OK. And I can even give it some value. For example for this is just like grade school when you had an equation such as two plus X or four plus X or you know these are simple equations X plus 7 every Java program starts from the main method and it goes line by line executing each line of instruction. So when it gets here it sees that there is a variable x and its value is 4. So anything that follows whenever wherever it sees X the computer is going to know that hey this is this is actually equal to 4. So the first instruction here two plus for this is going to equal 6. This right here is going to equal eight. This right here is going to equal the number 11. Now the reason why they call it variable and nothing else is because variables can be changed. So it's a variable value that goes in it. So I can change what X equals on this line. And all code that follows after this is going to think of X to be the value 3. All right. It's not going to consider X to be the value for because we've changed it. That's the idea that's the reasoning behind why they call it a variable is because guess what the data can vary depending on what you assign it. So another important thing to keep in mind is this equal sign. This is actually referred to as an assignment operator on basic math class you may have thought of it as equality when you're checking to see if two things are equal. Well in computer programming it's different. One equals sign right here. This means we are assigning. OK so here we are assigning the number four to this variable x. Down here we're assigning we're assigning the number three to this variable x. Ok so this is assignment. Keep that in mind. I can assign a new variable and give it that number 49. I'm assigning it using this equal sign. Now of course this is not valid Java code as you can see there are errors and I'm just explaining to you how variables work. So let's fix these problems and get rid of all this code. And let's go to the basic variable assignment here. Here we have a variable called X. And we're trying to assign the number for hover your mouse over this X and it's saying syntax error. Insert semicolon. So every Java statement needs to end with a semi-colon. So that's fixed cool. Now hover your mouse again over this error and it's saying X can not be resolved to a variable. Or it sort of does. It does not yet consider this as a variable. And the reason is it's not declared yet. This variable X has not been declared yet declaration is done by mentioning that type of data that's going to go in here and that's referred to as a data type data type is exactly what it sounds like. It's basically specifying what kind of data goes into something and this variable it's not defined until we specify the the data type. So I can say that this variable is capable of storing integer data. So we're specifying the data type for x. It's going to be int it is a special key word that basically means numbers if we want to put numbers into X we need to specify the data type to be in. Now it should be just fine. I can give X the value of anything I want it and it's not going to error out. So this is very important we're declaring we're declaring the variable here and over here we are signing the variable. Very important to keep in mind. And I could just print as you saw in the previous lesson we can print by doing system dot out the print line. I can print the value X and let's hit the play button and notice that 34 gets printed. Now again I can change the value of x to be whatever I want as long as it's of type Integer. I can give it that value so I'm going to give it to 23 now. OK. And now if I print let's copy that and paste it down here and run the program. Notice that it first prints 34 because over here the values the value of X was 34 right and then down here the value has changed. It's been changed to 23 because this line comes first. So it's going line by line. Over here it's declaring the variable over here. So it's assigning the variable to a value. And over here it's printing it. And over here it's changing by assigning something else to X and it's printing that. Now there are other types of data. Right now you just saw integer. There are other types of data. For example let's say if you want to store words or sentences for that there is something called string string is a data type used to store words sentences and sequence of characters. So I'm going to say I'm going to give this variable the name words or I can call variables anything we want and the data type is string. So I've defined. I've declared a variable called words of type string. Now only strings can go into words. I cannot I cannot give for example the number 20 because guess what. This is actually not a string of e-mails here and it's saying type mismatch cannot convert into string. So the only kind of data that can go into words is stuff that has that has double quotes around it. OK. And if you have words for example this is a sentence if you have stuff like this goes that's the only kind of data that can go into a variable of type string. OK. Now I can't assign X to be a sentence like this if I try to do that. It's going to complain it's going to that's romance mounseer and same type mismatch cannot convert string to int because guess what. X can only contain integer data. It can only contain numbers and we're trying to assign a string value so that's obviously illegal. We can't do that. So I'm just going to give some number here and that's going to fix that problem. So now just like we printed numbers I can print I can print words to the screen of course like we did in the previous lesson and I instead of you know writing a string like this with this with double quotes I could just reference to a variable that contains a string and that's called words. And let's hit the play button and notice that it's printing as expected. So three important things you learned in this lesson one is that the code is executed line by line all right line by line sequentially from top to bottom. Another thing is we have to declare the variables with a data type. OK. So here's the data type. This is a data type and the name of the variable. Is this right here X words is another name of the variable. And then we can change we can change the data that goes into the variables. So over here I'm saying x is equal to 34. And little bit code later we're actually changing that to this number right here. Another thing to keep in mind is that variables must have letters. It must start with letters. So for example we have this variable called words. I can change this to have numbers as well. This is still a valid variable declaration. Is still a valid variable. But as soon as as soon as I try to put some something other than a letter in the beginning. Now this is an illegal variable name we can't have any other character except for letters in the beginning. OK so let me get rid of that and we can't have any any creative syntax like this. This is also not a variable. We can't have these special characters. It needs to be either letters or numbers and that's what makes a variable legal. So of course this is given an error because over here it's saying words cannot be resolved to a variable because we haven't defined words. Well yes. We haven't defined this silly looking variable and words no longer exist so let me fix this. And now words does exist and we can use it later on in our code. Now just a few more minor details about variables. There are two steps that declaration and here we are assigning it. So you can combine both of these. The declaration of the variable as well as the assignment of the variable on the same line. So the way to do that is I could just get rid of all of this and just declare it as well as a sign it on the same line. All right. Let me do the same thing for words. So we get rid of this code down here and you put an equal sign between Again the equal sign is assigning what's on the right to the variable on the left. Right. So this is Declaration and assignment going on in one statement over here this is just an assignment. We don't need to declare x Again we've already declared it up here. OK. We've already declared that this is a variable of type integer and we're not going to do that again even though we assign it up here. We can reassign it to something else but declaration is not required here because a computer already understands that X is supposed to store integer data. Another thing I want to cover is operators certain data types support operators. For example if I wanted to print let me just make some room here and create another print line statement. And by the way I'm using a shortcut but I'll go over later how to solve typing system that out that Parentline. I use a keyboard shortcut that wrote this entire verbose statement. But anyway I'm going to print the variable x plus for now this plus sign is actually an operator that is supported by this data type. OK. And it does exactly what it sounds like. We are adding the number four to whatever is inside of X.. And at this point what's inside of X that's discovered here. 2:34 so I expect this to be printed thirty eight. So let's hit the play button here. And notice after after it prints 34 from this statement X value has changed. And then we print to thirty four plus four right here that's where it's 238 and then we're printing 234 and you know the rest you're already aware. So certain data types have operators. So I have the plus operator I can is set here that's also supported by this integer data type. I can divide for example and I can multiply the basic math operations because guess what this data type is supposed to store numbers. So it of course supports all of these different operators. Now the string data type the string also has certain operators. So I can for example add a statement here saying this is some more words and let me fix my English. These are some more words. OK so now we hit the run button. Notice that it appended to the existing sentence. This right here using this plus. Operator OK. So we are appending when it comes to strings. Now what addition does with integer is it's going to act like a calculator. We are adding numbers. Here we are when we use the plus sign we are appending extra words to the end of a particular string. Right because this is the string data type. It supports the plus sign but it does something completely different than integer addition. OK we looked at integers. We were able to add subtract multiply. So let's try to subtract using the minus sign. Notice this is not supported. If you have your mouse over it saying the operator minus is undefined for the argument type string. OK. So what it's saying is this data type of string does not support this particular operator. All right. And there's plenty more we'll learn about strings in the coming lessons. But I just wanted to show you that these operators can be used and certain data types support certain kinds of operations. So we looked at the integer data type we looked at the string data type and we continue this journey to learn more data types in the coming lessons. So I to wrap it up here thanks for watching. I'll see you soon.