In Java, a variable is a named container used to store values during program execution. Each variable is assigned a data type that defines the type of value it can hold.
Java has three types of variables: local, instance, and static. Local variables are declared within a method or block and can only be accessed within that method or block. Instance variables are declared within a class and can be accessed by any method of that class. Static variables are also declared within a class, but they are associated with the class rather than with any instance of the class.
Java has two categories of data types: primitive and non-primitive. Primitive data types are the most basic data types and include integer types (byte, short, int, long), floating-point types (float, double), character type (char), and boolean type (boolean). Non-primitive data types, also known as reference types, include classes, arrays, and interfaces.
Variable
A variable in Java is a name given to a reserved area in the memory. The name is used to access and manipulate the value stored in that area. The term variable is a combination of “vary + able” which signifies that the value it holds can be changed during the program execution.
int data=20;//Here data is variable
Types of Variables
There are three types of variables in Java:
- local variable
- instance variable
- static variable
Local Variable:
A variable declared inside the body of a method is called a local variable. Its scope is limited to the method in which it is declared and cannot be accessed by other methods in the class. A local variable cannot be defined with the “static” keyword.
Instance Variable:
A variable declared inside the class but outside the body of a method is called an instance variable. It is not declared static. Instance variables have instance-specific values and are not shared among instances.
Static Variable:
A variable that is declared static is called a static variable. It cannot be local. A single copy of a static variable is created and shared among all instances of the class. Memory allocation for static variables happens only once when the class is loaded into memory.
Example Program to understand the types of variables Present in Java
public class A { static int m=10;//static variable void method() { int n=20;//local variable } public static void main(String args[]) { int data=30;//instance variable } }//end of class
Java Variable Example Program: Add Two Numbers
public class Simple{ public static void main(String[] args){ int a=20; int b=50; int c=a+b; System.out.println(c); } }
Output:
70
Java Variable Example Program: Widening
public class Simple{ public static void main(String[] args){ int a=90; float f=a; System.out.println(a); System.out.println(f); }}
Output:
90 90.0
Java Variable Example: Narrowing (Typecasting)
public class Simple{ public static void main(String[] args){ float f=70.5f; //int a=f;//Compile time error int a=(int)f; System.out.println(f); System.out.println(a); }}
Output:
70.5 70
Java Variable Example: Overflow
class Simple{ public static void main(String[] args){ //Overflow int a=190; byte b=(byte)a; System.out.println(a); System.out.println(b); }}
Output:
190 -186
Java Variable Example: Adding Lower Type
class Simple{ public static void main(String[] args){ byte a=40; byte b=40; //byte c=a+b;//Compile Time Error: because a+b=80 will be int byte c=(byte)(a+b); System.out.println(c); }}
Output:
80
We hope that you have gained a thorough comprehension of Java variables. To delve deeper into the topic, we encourage you to follow tutorials.freshersnow.com.