What is a Variable?
- int (for integer values : 50, -50)
- float (for decimal values : 50.25, -50.25)
- String (for text values : “Programmer Kansabanik”, “@12H ello World”)
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?
- 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 :
Example (store a String value):
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) :
int studentAge = 22;
System.out.println(studentAge);
Overwrite the 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
How to Display Variable?
int mySalary = 50000;
System.out.println(mySalary);
What is Java Identifiers?
int s = 50000;
int mySalary = 50000; // this one is easy to understand
How to add variables ?
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 :
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.
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
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.

2 thoughts on “Variables in Java”