In this tutorial, we will see introduction to variables, declaration, and types.
Table of Contents
Introduction
Variables
in java is used to store values of elements while the program is executed.
Java variables are nothing but memory location. It stores the values which can be manipulated by program. Variables in java are strongly typed hence they all must have a datatype followed by an identifier.
Each variable has the specific data type which defines size and layout of the variable’s memory.
- Looking for ⚒️ tech jobs? Go to our job portal.
- Looking for tech events? Go to tech events 🗓️ Calendar.️
Declaring a variable
We can declare a variable as below:
1 2 3 |
data_type variable_name = value; |
Here is the example:
💡 Did you know?
value is optional while declaring variables. You can declare variable and assign value to it later in program.
Variable has a name that is used to identify it. The name of the variable is known as the variable’s identifier.
There are some naming conventions that you need to follow while declaring a variable.
- You can use
A to Z
ora to z
- You can use
0 to 9
- You can use special symbol
$
and_
to declare a variable - You can not start a variable name with number
- You can not have spaces in variable name.
- You can not use reserved keywords in variable name
- Varibles names are case sensitive.
Examples:
1 2 3 4 |
int age; String person_name; |
You can declare variables in groups as well.
1 2 3 4 |
int age,weight; String person_name,gender; |
Assigning a value to variable
Each variable holds some data which it describes. We use =
operator to assign value to a variable.
1 2 3 4 |
int age=30,weight=60; String person_name = "John"; |
Compiler will give compile type error if value is not matched with datatype.
For example:
You can not assign String literal to int data type.
1 2 3 4 |
// Compilation error int age= "NA" |
Types of variables
There are three kinds of variables in java.
Local variable
A Variable
which is declared inside a method can be termed as Local Variable
. It is mandatory to initialize local variable otherwise Compiler will complain about it.
Instance variable
A Variable which is declared at class level can be termed as Instance variable
. It is not mandatory to initializeInstance variable.
All instance variable will be by default initialized by JVM.
Static variable
A Variable which is declared as static is known as Static variable
. Static variables are class level variables.
Let’s understand it with the help of simple program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package org.arpit.java2blog; public class VariableDemo { int a; // Instance variable static int b=20; // static variable public void print() { int c=10; // local variable System.out.println("Method local variable: "+c); } public static void main(String args[]) { VariableDemo demo=new VariableDemo(); System.out.println("Instance variable: "+demo.a); // Printing Instance variable System.out.println("Static variable: "+b); // Printing static variable demo.print(); //Printing local variable using print method. } } |
When you run above program, you will get below output:
Static variable: 20
Method local variable: 10
That’s all about Variables in java.