Instead of using computer programming language, you use labels to represent them. These labels are called variables in programming. This blog will explain these labels and their different types of variables in Java. Understanding variables is crucial if you're interested in coding or want to build a career in computers.
These variables live in your computer's memory and hold different information. Each variable has a specific variable name that tells you what's inside. For example, a variable labeled "age" might hold 30. Importantly, you can only put one type of thing in each box. So, an "age" box would only hold numbers, not letters.
Before you can use a variable, you need to create it and give it a label. However, this is called declaring a variable. Storing data in a specific memory location and using a name to reference that memory is a crucial step in organizing and accessing information efficiently. You also tell the computer what kind of stuff you'll put in it (like numbers or letters). Before proceeding with the types of variables in Java, let’s understand how to create or declare variable.
You can store different types of items in this computer’s memory, such as variables of numbers, variables of words, or even variables containing other variables.
The Importance of Naming
Giving them meaningful names helps you and others understand what's stored inside without having to open every variable.
Why Good Naming Matters?
In short, while starting out, it might seem like a small detail, but choosing good variable names is a fundamental step towards writing clean, efficient, and maintainable code.
When you declare a variable, you are reserving memory for it. Initialization is the act of assigning a specific value to this reserved space. However, this can be either immediately upon declaration or at a later time. Moreover, the value of a variable is subject to change during program execution.
Example:
public class VariableTest {
public static void main(String[] args) {
int myInt = 5;
String s;
s = "init";
System.out.println(s);
s = s+5;
System.out.println(s);
}
}
We made a box called "myInt" that can hold whole numbers and put the number 5 inside it. Then, we made another box called "s" that can contain words and put the word "init" in it. We showed what was in the "s" box on the computer screen, then changed what was inside the "s" box to something else.
Here, we will provide you a list of Java variables. The checklist is as follows:
Now, we will elaborate on the types of Java variables in detail.
A local variable Is a temporary tool you use to complete a specific task. Once the task is finished, the tool is put away.
In brief, local variables create and destroy within a specific part of your code. They are essential for managing data within a particular section of your program. However, they have no impact on the rest of your code.
// Java Program to implement
// Local Variables
import java.io.*;
class GFG {
public static void main(String[] args)
{
// Declared a Local Variable
int var = 10;
// This variable is local to this main method only
System.out.println("Local Variable: " + var);
}
}
Instance variables are another types of variables in Java. They are personal belongings that each individual copy of an object gets to keep. Unlike shared items, these belongings are unique to each person.
In short, Instance variables are like personalized attributes that define the state of an object. In addition, they provide a way to store data specific to each object instance.
Example of the Types of Variables in Java:
// Java Program to demonstrate
// Instance Variables
import java.io.*;
class GFG {
// Declared Instance Variable
public String geek;
public int i;
public Integer I;
public GFG()
{
// Default Constructor
// initializing Instance Variable
this.geek = "Shubham Jain";
}
// Main Method
public static void main(String[] args)
{
// Object Creation
GFG name = new GFG();
// Displaying O/P
System.out.println("Geek name is: " + name.geek);
System.out.println("Default value for int is "
+ name.i);
// toString() called internally
System.out.println("Default value for Integer is "
+ name.I);
}
}
Static Variables are the least types of variables in Java. It is also called class variables.
Now, we will share an example of static variables - Java variable types.
// Java Program to demonstrate
// Static variables
import java.io.*;
class GFG {
// Declared static variable
public static String geek = "Shubham Jain";
public static void main(String[] args)
{
// geek variable can be accessed without object
// creation Displaying O/P GFG.geek --> using the
// static variable
System.out.println("Geek Name is : " + GFG.geek);
// static int c=0;
// above line,when uncommented,
// will throw an error as static variables cannot be
// declared locally.
}
}
Here, we will discuss some common examples of types of variables in Java. Read and go through them.
public class A
{
static int m=100;//static variable
void method()
{
int n=90;//local variable
}
public static void main(String args[])
{
int data=50;//instance variable
}
}//end of class
public class Simple{
public static void main(String[] args){
int a=10;
int b=10;
int c=a+b;
System.out.println(c);
}
}
public class Simple{
public static void main(String[] args){
int a=10;
float f=a;
System.out.println(a);
System.out.println(f);
}}
There are three primary types of variables in Java: local, instance, and static. Each serves a distinct purpose within a program's structure. Local variables are temporary containers for data within specific code blocks. On the other hand, Instance variables belong to individual objects and store data unique to each instance. Meanwhile, Static variables are shared among all objects of a class, providing a common storage area.
Ans. A data item that can assume multiple values during program execution.
Ans. A variable in programming can hold different things. Moreover, the contents of the variable can change as your program runs. It's a way for your computer to remember information while it's working on a task.
About the Author
UpskillCampus provides career assistance facilities not only with their courses but with their applications from Salary builder to Career assistance, they also help School students with what an individual needs to opt for a better career.
Leave a comment