Codementor Events

Java tutorial 2 - Variables

Published Dec 16, 2020Last updated Dec 18, 2020
Java tutorial 2 - Variables

In the last tutorial, we created a basic program in java. In this 2nd tutorial, we will aim at understanding, what a variable is.

In case you haven't read the previous tutorial, I recommend you to first go through it and then continue with this one. I'll wait for you.

DRUM ROLL

So what is a variable?

Imagine it like a container having a name on it and a value inside it.

 int a = 2;

Here, the name is a and the value is 2.
Now, what is int?
Int is the datatype of the variable.
It means the value contained in a can only be an integer.

Practical usage

Now, once we know what a variable is let's try to understand a small program using variables.

public class Main {
    public static void main(String[] args) {
        int a = 2;
        int b = 3;
        int c = a + b;
        System.out.println(c);
    }
}

We can see a is 2 and b is 3.
Int c will contain the sum of a and b i.e 5 (2+3).
Here the + symbol is called the operator
Finally, we are printing the value of c i.e 5.

5

And there we are

I think that you were able to understand how variables work. In case you have any doubts, I am happy to help you out.

In the next part, we will learn about operators. Hope to see you there too.

That`s it for this blog. If you wanna become a great programmer, I am there to help.

You can see my profile by clicking on my name below.

Discover and read more posts from Aniket Malik
get started