Working with variables

Learn the Basic Java Concepts Working with variables and operators
11 minutes
Share the link to this page
Copied
  Completed

Transcript

Hello there, and welcome back to this Java development course. Okay, so today we're going to be going over working with variables. And we're actually going to be going over some very basic mathematical operations as well. So let's go ahead and create a new lesson, new class. And we're actually not going to call it lesson two. Since this is lesson three, we're actually not gonna call it lesson at all, we're actually going to be calling this working with variables.

This way later on when you're actually going to be actually returning to your Java, you know, course course codes that they've written, instead of, you know, trying to find something to understand what you actually did, you're just gonna be able to look at the names and then based on the names, you're going to be able to understand exactly what it is. I mean, technically you can do you could you do whatever you want. You can name it whatever you want, but it's just recommended that you do it this way so that later on you don't get confused. So let's go ahead and create amendment, the main method, again, don't pay attention to any of this. This will all become more evident as we move on. So let's go ahead and click this public static void Main here.

And let's finish. All right, there we go. Let's also go to rename this lesson one as well. So we're actually going to go out and do refactor, rename. And let's call it Hello, world. There we go.

All right, let's just finish and then there we go. Okay, so now it just renamed it to hello world. Okay, so let's go and start working with variables. Again, we're going to be working in our main method. So how can we create a variable so in the last lesson, we actually took a look at how Java works and actually give you a very basic example of how we can create variables in high level programming languages. And the winner of the variable that actually created was in fact a Java variable.

So to create a variable all we do is int. So in this case, this is just the type of the variable. So in our case we're going to be creating an integer, which is a number. There are a couple of types of numbers in Java, but it is actually a whole number that, you know, takes up a specific amount of memory, we'll go over exactly what memory is and how it works later on. But just know that this, this is just a number that takes up a specific amount of memory. So we're going to create an integer, and then we're gonna assign it a name.

So let's just say our name will be a, okay, so an integer a, and then we just put a semicolon. All right, there we go. So now we have created a variable. So this variable is called a, and it is of type integer, which means that if we assign a value to the variable, it must be of type integer. So we can now go to print our variable using System dot out dot print ln and insert our variable name inside of it. And in this case, it will actually give us an error so error is just a problem with our code, something that our compiler can't understand.

So like it doesn't work, essentially. And if you actually go ahead and hover over there, eclipse will actually give us a very nice message about what the error is. So the local variable a may not have been initialized. So what does initialization mean? So initialization means that we assign some value to our variable. So how can we assign a value to our variable?

Well, we can simply do a is equal to, and then some value. So let's just say that our variable a is going to be equal to four, four is a whole number, and that is pretty small. It's under the integer limit. So we've now assigned four to eight and then we actually need to close off this statement by putting a semicolon so in Java after every single statement, you have to put a semicolon. It's just the way Java works. So this way, there's no confusion.

Going as to what exactly is what. So now we have an int a, we assign four to eight, and then we print a. So let's go and actually run our application. And there we go. So as you can see in our console, it prints four, I'm gonna go ahead and put my console right here, just so we can see the code while we are printing something. So we assign a variable a of type integer, we assign it the value four, and then we print A, which is four, and so it prints four.

We can change our value that we assigned to it, let's say 67. And run our application. And there we go, we get 67. Now, what if we try to assign something else to it like g h? Well, we get an error. It'll tell us g h cannot be resolved to variable.

What does this mean? Well, in this case, Java is actually looking for a variable named gh that we can assign To a. So we can actually assign variables to variables. Let's actually take a look at this. So we can now create a variable b, it's B, and I'll just assign b 56. And now we can assign a to b.

So what does this mean? So we're creating two variables a and b both of type integer, which means a number, a whole number, we are assigning the value 56 to B. And then we are assigning B to A, which is 36. So a will be equal to 56. So what do you think will be the output when we run application? Let's check.

Spoiler it's gonna be 56. I was right. Okay. So that's essentially how, how variables work. Now, we can actually assign values to variables directly when we're creating them. So what do I mean?

Let's delete this will delete the B variable as well. We can now do it In a is equal to four. So instead of doing int a, and then assigning four to a, we can just do int a equals four and a semicolon. So what does this do? Well, this creates a variable a of type integer, which is a whole number. And that assigns the value four to the type integer.

I'm sorry, no, it assigns the value four to the variable a, and then we print a. Alright, let's save it. Let's run it. And there we go, we get four. So that is called working with variables. Now, what if we try to assign this number to integer? We get an error.

Why did we get the error? The literal 675364 and so on of type int is out of range. So what does this mean? Well, as I said before, an int can actually hold a specific amount numbers. So there's a limit to what an integer can hold. And we can actually print it.

I mean, I don't remember it personally. But we can actually print it, we could just do integer integer from a capital letter dot max value. And this will print the maximum value of integer 214 million. Oh no, it's 2,147,483,647. So that is the maximum value of an integer. Now, we can ask, we can also get the minimum value.

There we go. And let's run it. So remember that the minimum value is negative 2 billion and so on and so on. So that is essentially how we work with variables. Now one last thing. In Java variable names are case sensitive.

So this means that if we create an int a equals 56, and if we do right here, Here's a from a capital letter, it'll give us air. Since variable a doesn't exist, so if you hover over it, it should a cannot be resolved to a variable, since we don't have a variable named a, but if we change this variable to a capital A, then it's okay. Okay, and one last thing, actually, we also can start variable names with letters. So if we try with numbers, so try to do this, it gives an error, since we can't start variable names with numbers, only letters, but then we can have numbers in variable names after the first letter. So yeah, except now, you know, to the problems can't find that variable. There we go.

Okay, so that is essentially how we work with variables. One last thing, we're going to learn how to add variables as well, in this lesson, we're actually at least integers. We're going to learn how to add integers. So let's create an integer A equals 12. Then an integer B, which is going to be equal to 45. And then an integer C, which is going to be equal to 12 plus 45.

There we go. Now let's print this print. See? All right, there we go. And what do you think is gonna be? Spoiler alert, it's gonna be 12 plus 4557.

There we go. So we don't necessarily have to put specifically 12 plus 45, we can actually just do a plus b. So what what will this dude this will take a, add the value B to A, and then finally it will assign that into C. So it's gonna do a plus b, which is 57, and then assign that to C, and then we're going to print C. So take a look. There we go. 57. And we don't necessarily need to assign it into another variable, we could actually just do a plus b Inside here, let's run this 57 As you can see, or even, we don't even need to do variables, we can just do system out print ln 12 plus what I have there?

It was a 45. Yeah, there we go. And there we go. And so that is essentially how we work with variables. Okay, so there are a lot of different types of variables. In Java, there are actually I think, eight or seven primitive types.

And then there are infinite complex types, which we'll go over as well. But an integer it's very, it's really the most basic type. And it's really what most students start out with at the very beginning. So let's say you some homework, alright, so for homework, you're going to have three variables A, B, and C. You're going to assign them some values. So you know just whatever values you want. And then We're going to print to the sum of those values.

So the, I don't know, maybe the, if you have a as 12 cS 10, and then B is 10, then it's going to be 32. And it should be automatically so it should be dynamic. So if I change a to 13 instead of 12, then it should automatically update the sum to be 33. That's 32 without changing anything else in the code. So that is your job for this lesson. I wish you luck and I'll see you next time soon.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.