DEV Community

Cover image for Java Programming Language (Variables)
Nihal Islam
Nihal Islam

Posted on • Updated on

Java Programming Language (Variables)

Variables

To work with data in programming, we need to store the values into a container. Variables work as containers which stores different data types and later helps us to use them into programming.

Now if we want to print an integer number in the console we can simply write the code

class Variables {
    public static void main(String[] args) {
        System.out.println(10);
    }
}
Enter fullscreen mode Exit fullscreen mode

This will simply prints 10 in the console. However, we want to store the number 10 into a variable then print it.

Before storing a data into a variable in java we must need to declare what kind of data I am storing in that particular variable. Here I want to store the integer number 10 into a variable. So, we will write the code as follows
int number = 10;
Now let me define the code step by step.

Declaring Data Type

'int' is one kind of data types which holds integer values. There are different kinds of data types in java programming language. For now, I am storing integer value in the variable. That is why before writing a variable name I wrote 'int'.

Variable Name

Here 'number' is the name of my variable. As I am storing a number I named my variable 'number'. Variable names can be anything. However, there are some constraints and conventions of writing a variable name. We will look into that.

Assigning a value

'=' sign means we are assigning a value that comes after the sign. The value will be assigned to the variable which we have declared. In this case 10 is the value which we have assigned to the variable named 'number'. Now if I want to print the number 10 we can pass the variable name into the print function as follows.

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

Variable Naming Conventions

Constraints

  • Variable names cannot start with a number such as int 1number = 1
  • Variable names cannot have empty space such as int number one = 1

Conventions

  • Give a proper variable name such as int number = 1 rather that using a single letter such as int x = 1
  • Use lowercase letters for naming a variable.
  • You can find some of the proper naming conventions here.

Appendix

  • You can declare a variable without assigning a value. It will set a default value into the variable. Default value varies for different data types. For example int data type has default value 0.
int number;
System.out.println(number) //prints 0
Enter fullscreen mode Exit fullscreen mode
  • You can write comments in java code using double slash like this //comments. Anything written after // will not be executed in code.
  • You can change the value of a variable. However, new value needs to be of the same data type as declared before. Also you don't need to declare variable type again while assigning a new value into the same variable.
int number = 10;
number = 20;
Enter fullscreen mode Exit fullscreen mode
  • You cannot change data type of the same variable. Variable name must be unique.
int number = 10;
float number = 10.0; //you cannot do that
Enter fullscreen mode Exit fullscreen mode

Happy Coding.

Top comments (0)