Variables in Java

What is a Variable?

Variable plays an important role when you are coding in java. We use Java variables to store information to be referenced and manipulated in our java programs. Variables can store data value in our programs like a container.

Variable is the name of a memory location. We use variable names in our program instead of using use memory locations directly in our code.

In Java programming language, you have to learn about different types of variables, for example:

  • int (for integer values : 50, -50)
  • float (for decimal values : 50.25, -50.25)
  • String (for text values : “Programmer Kansabanik”, “@12H ello World”)

You can see three types of variables in the above example. But there are more than three types available in Java. I am going to explain them in this series.
For now, just focus on how can you declare a variable in Java programming language. You need to declare the variables first before using them to avoid errors in your programs.

How to Declare a Variable in Java?

Now you are going to learn how can you create a variable in your Java program.

  • First, you have to declare the Data Type of your variable (such as char, int, String).
  • Second, you have to write the name of your variable (such as a , firstName, lastName, myAge).
  • Now you need to assign it value. Generally, we use an equal sign (=) to assign values to a variable.

Data_Type variable_name = value;

Note :

Choosing a variable name is not a few seconds of work. You have to choose your variable name wisely. They must accurately describe their purpose in your program.

Your co-programmers should understand the purpose of your variable in your code. Sometimes you need to check your old code. If you don’t write the variable names properly, you will not be able to understand.

Example (store a String value):

Declare a variable name called channelName of type String. Then assign it the value “Programmer Kansabanik“. Then print the variable.

String channelName; // we have declared the variable
channelName = "Programmer Kansabanik"; // we have assigned value
System.out.println(channelName); // this line will print the value of your variable
You may follow another process to create a variable :
String channelName = "Programmer Kansabanik"; // we can do this directly
System.out.println(channelName); // this line will print the value of your variable.
Example (store a int value) :

Create a variable in your program to store a number value. Declare the variable called studentAge of type int and assign it the value 22. Then print the variable in your Java program.

int studentAge = 22;
System.out.println(studentAge);
Overwrite the value :

When you will assign a new value to an existing variable in your code, the new value will overwrite the previous value :

Example( change existing the value of myAge to 25 ):
int studentAge = 22; // declare the int type variable and assign a value
studentAge = 25; // overwrite the previous value
System.out.println(studentAge); // this line will print 25 instead of 22

If you don’t want others to overwrite your existing values, you can use final variables to prevent the overwriting existing values.

How to Display Variable?

We can use the println() method to display variables. Look at the code below :

int mySalary = 50000;
System.out.println(mySalary);

What is Java Identifiers?

In a Java program, we should identify all variables with unique names. These unique names are called identifiers in Java programming language.

They can be short names such as a, b, c. They can be more descriptive names such as myage , mySalary etc.

int s = 50000;
int mySalary = 50000; // this one is easy to understand

How to add variables ?

We can use plus sign (+) to add multiple variables of the same Data Type.

Example(Add two variables) :
int a = 5;
int b = 10;
int c = 20;
System.out.println(a + b + c) // Thus we can add 3 variables

Note :

I recommend you to use descriptive variable names because its easy to read and understandable. It is maintainable too.

Use of final variable in Java program

final keyword is used in different contexts. But in this post we will use final keyword to create constant variables.

If you put final keyword before declaring the variable, you can assign value only once. Its value can not be modified later in the code.

You can initialize a final variable when it is declared.

final data_type variable_name = value;

You or your co-programmers can not change the value of that variable in code. Thus it will be constant.

Look at the following code:

Example (Using a final variable):
final int studentAge = 25;
studentAge = 15; // this line will produce an error : cannot assign a value to a final variable

Example (without a final variable):
int studentAge = 25;
System.out.println(studentAge); // it'll print 25
studentAge = 15;
System.out.println(studentAge); // it'll print 15

How to Declare Many Variables

In this case, You can use a comma-separated list to declare multiple variables (more than one) of the same Data Type.

Example (Assign 3 values in the same line):
int a = 10, b = 20, c = 30; // 3 values are assigned in one line
System.out.println(a + b + c); // it will print the sum

The general rules for constructing names for variables (unique identifiers) are:

  • Variable names must begin with a letter.
  • Variable names should start with a lowercase letter and it cannot contain whitespace.
  • Variable names are case sensitive (“myVar” and “myvar” are different variables).
  • Variable names can contain letters, digits, underscores ( _ ), and dollar ( $ )signs.
  • You can not use “reserved words” (like Java keywords, such as int or boolean) as variable names.
  • Variable names can also begin with $ and _ .

In the next post I am going to talk about types of Java Variables.

Published by Surojit Kansabanik

My name is Surojit Kansabanik. I am a student by profession and a programmer by passion. I like to share my programming knowledge through my YouTube channel (Programmer Kansabanik) and blogs (Programmer Kansabanik) with others. I like to guide newbies in the files of coding.

2 thoughts on “Variables in Java

Leave a comment

Design a site like this with WordPress.com
Get started