Java

Variables

Java / Variables

Variables

Variables

A variable in Java is a named memory location used to store data values. The value stored in a variable can be changed during program execution. A variable works as a container that holds data in computer memory.

Every variable has a name, data type, and value. It must be declared before use in a program.

Declaring (Creating) Variables in Java

To create a variable in Java, you need to follow these simple steps:

  • First, choose a data type (such as int, String, float, etc.)
  • Then, give the variable a name (such as x, age, name)
  • Finally, you can assign a value using the = symbol (this step is optional at the time of declaration)

Example

Β Format:

dataType variableName = value;

Example:

int age = 20;
String name = "Java";

Declare Many Variables

In Java, you can declare multiple variables in different ways depending on your need. This helps in writing clean and efficient code.

1. Declare Multiple Variables of the Same Type (Same Line)

You can declare more than one variable of the same data type in a single line by separating them with commas

dataType var1, var2, var3;

2. Declare and Initialize Multiple Variables

You can also assign values while declaring variables.

int a, b, c;

3. Assign Same Value to Multiple Variables

You can assign the same value to multiple variables using separate statements.

int a = 10, b = 20, c = 30;

Β 

Technology
Java
want to connect with us ?
Contact Us