Variable in Java: Explain variable with example

Variable in Java

Variable in Java is fundamental elements that store data values. Understanding how to declare, initialize, and use variables is essential for programming in Java. Here’s an overview:

Declaration and Initialization of a variable in Java

Variables in Java must be declared before they can be used. Declaration involves specifying the variable’s data type and its name. Initialization involves assigning a value to the variable.

Declaration:
int age;
double salary;
String name;
Initialization of a Variable in Java:
age = 25;
salary = 50000.75;
name = "John Doe";

Declaration and Initialization Together:

int age = 25;
double salary = 50000.75;
String name = "John Doe";

Data Types of a Variable in Java

Java supports various data types, which can be broadly categorized into two groups: primitive data types and reference data types.

Primitive Data Types:

  • byte: 8-bit integer
  • short: 16-bit integer
  • int: 32-bit integer
  • long: 64-bit integer
  • float: 32-bit floating point
  • double: 64-bit floating point
  • char: 16-bit Unicode character
  • boolean: true or false

Example:

byte b = 10;
short s = 200;
int i = 3000;
long l = 400000L;
float f = 5.75f;
double d = 19.99;
char c = 'A';
boolean bool = true;

Reference Data Types:

Reference data types refer to objects and arrays. The most common reference data type is String.

Example:

String message = "Hello, World!";
int[] numbers = {1, 2, 3, 4, 5};

Variable Scope in Java

The scope of a variable determines where the variable can be accessed within the program. There are three main types of variable scope in Java:

Local Variables in Java:

These are declared inside a method or block and can only be accessed within that method or block.

public void myMethod() {
    int localVar = 10;
    System.out.println(localVar);
}

Instance Variables in Java

These are declared inside a class but outside any method. They can be accessed by all methods within the class.

public class MyClass {
    int instanceVar;

    public void display() {
        instanceVar = 5;
        System.out.println(instanceVar);
    }
}

Class Variables in Java (Static Variables in Java)

These are declared with the static keyword inside a class but outside any method. They can be accessed by all instances of the class and by static methods.

public class MyClass {
    static int classVar;

    public static void display() {
        classVar = 10;
        System.out.println(classVar);
    }
}

Variable Naming Conventions

  • Use meaningful names that describe the variable’s purpose.
  • Start with a lowercase letter. Subsequent words should be capitalized (camelCase).
  • Avoid using Java reserved keywords.

Examples:

int studentAge;
double accountBalance;
String customerName;

Constants

If a variable’s value is not supposed to change, it can be declared as a constant using the final keyword.

Summary

Variables in Java are used to store data values. They must be declared with a data type before use and can be categorized by their scope (local, instance, class). Following proper naming conventions and understanding the different data types is crucial for writing clear and efficient Java code.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *