Udemy

Introduction to Variables and Data Types in Java

A free video tutorial from Job Ready Programmer
Award Winning Instructor | Over 500.000+ Successful Students
Rating: 4.6 out of 5Instructor rating
19 courses
520,382 students
Lecture 3: Variables and Data Types

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:26:34 of on-demand video • Updated April 2025

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 [CC]
Instructor: Hey there, in this lesson we're gonna talk about variables and data types. First, we need a class. I'm gonna hover my mouse over here on Java Basics, right click it, go to New and click on Class. Now we're gonna get into all types of details about what a class is later on in the course so don't worry about that for now. But we're gonna call this class Variables because that's what we're gonna be learning about. And just the naming convention here. The first letter of a class should be capital. All right, 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 as 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 gonna get into details of what all those what all these keywords such as static and void and public mean later on. But for now, you can ignore them and just take them for granted. And every, all the code that we're gonna 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. So inside of this main method, we're gonna write all of our codes. So let's start with variables. A variable in computer programming is a place where you can save data, right? So for example, I can have a variable called X, okay? And I can even give it some value for example, four. This is just like grade school. When you had an equation such as two plus X or four plus X or these are simple equations, X plus seven. Every Java program starts from the main method and it goes line by line executing each line of instruction. So when it gets to here, it sees that there is a variable X and its value is four. So anything that follows wherever it sees X, the computer's gonna know that, "Hey, this is actually equal to four." So the first instruction here, two plus four, this is gonna equal six. This right here is gonna equal eight. This right here is gonna 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 gonna think of X to be the value three, alright? It is not gonna consider X to be the value four 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. Now, in 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 equal sign right here this means we are assigning, okay? So here we are assigning the number four to this variable X. Down here we are assigning, we're assigning the the number three to this variable X. So this is assignment, keep that in mind. I can assign a new variable and give it the 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. I'm gonna get rid of all this code and let's go to the basic variable assignment. Here we have a variable called X and we're trying to assign the number four. Hover your mouse over this X and it's saying, "Syntax error insert semicolon." So every java statement needs to end with a semicolon. So that's fixed, cool. Now hover your mouse again over this error and it's saying, "X cannot be resolved to a variable," alright? So 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 the type of data that's gonna go in here. And that's referred to as a data type. A 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 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 gonna be int. Int is a special keyword that basically means numbers. If we want to put numbers into X, we need to specify the data type to be int. Now it should be just fine. I can give X the value of anything I want. And it's not gonna error out. So this is very important. We're declaring the variable here. And over here we are assigning the variable. Very important to keep in mind. And now I could just print as you saw in the previous lesson we can print by doing System.out.println, 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 gonna give it the value 23 now, okay? 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 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 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 wanna 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 gonna say, I'm gonna give this variable the name words, right? We 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 give for example the number 20, because guess what? This is actually not a string. Hover your mouse here. And it's saying, "Type mismatch cannot convert int string." So the only kind of data that can go into words is stuff that has double quotes around it. And if you have words, for example, this is a sentence. If you have stuff like this, that's the only kind of data that can go into a variable of type string, okay? Now, I can't assign X to be a sentence like this. If I try to do that, it's gonna complain, let's hover our mouse here and it's saying, "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 gonna give some number here and that's gonna fix that problem. So now just like we printed numbers, I can print words to the screen of course like we did in the previous lesson. And instead of, you know writing a string like this with double quotes, I could just reference the 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 the code is executed line by line, alright? Line by line sequentially from top to bottom. Another thing is, we have to declare the variables with a data type. 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 a 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. This is still a valid variable. But 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, okay? So let me get rid of that. And we can't have 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." 'Cause we haven't defined words. Well, yes we haven't. We've 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, the 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 assign it on the same line, right? Let me do the same thing for words. So we get rid of this code down here and we put an equal sign between. So again, the equal sign is assigning what's on the right to the variable on the left, all 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, okay? We've already declared that this is a variable of type integer and we're not gonna do that again even though we assign it up here we can reassign it to something else. But declaration is not required here because the computer already understands that X is supposed to store integer data. Another thing I wanna 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 that I'll go over later. How to, instead of typing System.out.println, I use a keyboard shortcut that wrote this entire verbose statement. But anyway, I'm gonna print the variable X plus four. Now this plus sign is actually an operator that is supported by this data type, okay? 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 this guy right here 234. So expect this to be printed 238. So let's hit the Play button here and notice after it prints 34 from this statement, X value has changed. And then we print 234 plus four right here. That's why it's 238, right? 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 minus it 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 int 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." So now if we hit the Run button, notice that it appended to the existing sentence. This right here, using this plus operator, okay? So we are appending when it comes to strings. Now what addition does with integer is, it's gonna 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, okay? We looked at integers, we are able to add, subtract, multiply. So let's try to subtract using the minus sign. Notice this is not supported. If you hover your mouse here, it's saying, "The operator minus is undefined for the argument type string." So what it's saying is this data type, string does not support this particular operator. 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're gonna continue this journey to learn more data types in the coming lessons. So I'm gonna wrap it up here. Thanks for watching and I'll see you soon.