DEV Community

Cover image for Basics of Java #4
Chathu Rashmini
Chathu Rashmini

Posted on • Updated on

Basics of Java #4

In this article we are going to learn about variables and data types in java.

Variables

You might be new to programming, but you might not be new to mathematics. Therefore, I am sure that you have heard the word variable before. For example if we take the function y = 2x + 10, you know that both y and x are variables here. They are just two labels that represent two numbers and store a numerical value. We can assign any value to them at any time, so that the value they store is not specific. In programming what are variables?

alt variables

Programmers can declare variables and store values in them. The structure of declaring a variable is as follows;
dataType variableName = variableValue;

First you need to tell the data type of the variable. Whether it is an integer or a string.
Then you have to give a name. Name of a variable is also called an identifier. It is always good to have a short but descriptive name. For example if you are storing age, then naming the variable directly as 'age' is good instead of naming it as 'a' or 'x'. If you are storing the age of a set of students then you can name the variable as 'std_age'. This will make your code more readable and understandable even for a third party.
You have to use the equal sign, or we also can say as 'assignment operator' (=) to initialize a value to the variable that you have defined. Left to the assignment operator is the identifier and to the right you can write the value.

Before jumping into coding let's go through some rules which has to be followed when naming variables.

Rules in naming variables

  1. Identifiers should starts with an underscore (_), currency character ($), or uppercase or lowercase character (A to Z, or a to z).
  2. Name cannot be a keyword in java. For example words like class, new, int cannot be identifiers.
  3. You can have more than one word for identifiers, but they have to be in camel notation. (ex: studentIndex, userName)
  4. Identifiers are case sensitive. 'Name' and 'name' are two different identifiers.

Now we are going to store the numerical value '18' in a variable named 'age' and going to display it. Let's see how we can do that.

public class Main {
    public static void main(String[] args) {
        int age = 28;
        System.out.println(age);
    }
}
Enter fullscreen mode Exit fullscreen mode

Here I have declared an integer variable named 'age' and has assigned the value 18 into it. Then inside the System.out.println() I have directly used the name of the variable to get the value of it printed.

What will be the output of the following code? Can we change the value of a variable after we assigned a value to it in one place?

public class Main {
    public static void main(String[] args) {
        int age = 28;
        System.out.println(age);
        age = 32;
        System.out.println(age);
    }
}
Enter fullscreen mode Exit fullscreen mode

Did you try it? Then you may be able to see the following output.

alt 2_variables

Which means you can change the value of a variable, but there are instances where you can't. We will learn about that in the future.

You also can initialize multiple variables of the same data type in the same line as shown in the given code.

public class Main {
    public static void main(String[] args) {
        int mathMarks = 78, englishMarks = 95;
        System.out.println(mathMarks);
        System.out.println(englishMarks);
    }
}
Enter fullscreen mode Exit fullscreen mode

This will execute and will give expected outputs without any errors. However, I do not recommend this because it make the code somewhat untidy.

You also can copy the value of one variable to another.

public class Main {
    public static void main(String[] args) {
        int mathMarks = 78;
                int englishMarks = mathMarks;
        System.out.println(englishMarks);
    }
}
Enter fullscreen mode Exit fullscreen mode

This will give '78' as the output because the value of the variable mathMarks is being copied into the variable englishMarks.

This is the end of the forth article of the article series 'Basics of Java'. See you in the next article. Till then play around with variables :)

Discussion (0)