Variables And Data Types In Java

Java Variables

Yuvaraj
3 min readJun 24, 2024
Variable holds data

A variable is a container which stores the value (data) that can be changed during the execution of a program. A variable is assigned with a data type.

Variable is a name of memory location. There are three types of variable in java: local, instance, static.

1. Local Variables:
A variable declared inside the method, constructor, or block is called local variable. It is only accessible within the block or method where they are declared. It is not initialized by default, so they must be initialized before use.

public class Example {
public void myMethod() {
int localVar = 42; // This is a local variable
System.out.println(localVar);
}

public static void main(String[] args) {
Example example = new Example();
example.myMethod();
}
}

// Output

// 42

2. Instance Variables:
It is declared inside the class but outside the method, constructor, or block is called instance variable. Each object has its own copy of instance variables. It is initialized to default values if not explicitly initialized.

public class Example {

int instanceVar;

// Constructor to initialize instanceVar
public Example(int value) {
this.instanceVar = value;
}

public void setInstanceVar(int value) {
this.instanceVar = value;
}

public int getInstanceVar() {
return this.instanceVar;
}

public static void main(String[] args) {
// Creating an instance of Example
Example example = new Example(10);

// Printing the initial value
System.out.println("Initial value: " + example.getInstanceVar());

// Setting a new value
example.setInstanceVar(20);

// Printing the updated value
System.out.println("Updated value: " + example.getInstanceVar());
}
}


// Output

/* Initial value: 10
Updated value: 20 */

3. Static Variables:
Class variables are declared with the `static` keyword inside a class but outside any method, constructor, or block. We can create a single copy of the static variable and share it among all the instances of the class. Initialized to default values if not explicitly initialized. (They are shared amoung all instances of the class)

public class Example {

static int classVar;

public static void main(String args[]) {

Example.classVar = 10;
System.out.println(Example.classVar);

}
}

// output: 10

Data Types in Java

Java is a strongly typed language, which means every variable must have a data type. Data types specify the size and type of values that can be stored in a variable. There are two main categories of data types in Java:

  1. Primitive Data Types:
    There are eight primitive data types which includes byte, short, int, long, float, double, boolean and char.

byte:
8-bit signed integer.
Range: -128 to 127.

short:
16-bit signed integer.
Range: -32,768 to 32,767.

int:
32-bit signed integer.
Range: -2³¹ to 2³¹-1.

long:
64-bit signed integer.
Range: -²⁶³ to ²⁶³-1.

float:
32-bit floating point.
Single-precision floating-point values.

double:
64-bit floating point.
Double-precision floating-point values.

boolean:
Represents one bit of information.
Only two possible values: “true” and “false”.

char:
16-bit Unicode character.
Range: ‘\u0000’ (or 0) to ‘\uffff’ (or 65,535).

Variable Declaration and Initialization

In Java, variables must be declared before they can be used. Declaration involves specifying the variable’s data type and name. Initialization involves assigning a value to the variable. Here are examples:

  1. Declaring and Initializing Primitive Variables:
int number; // Declaration
number = 10; // Initialization
float pi = 3.14f; // Declaration and Initialization
char letter = 'A';
boolean flag = true;

2. Declaring and Initializing Reference Variables:

String message = "Hello, World!"; // String is a reference type
int[] numbers = {1, 2, 3, 4, 5}; // Array is a reference type

Default Values

When variables are not explicitly initialized, they have the following default values:

byte, short, int, long: 0
float, double: 0.0
char: ‘\u0000’ (null character)
boolean: false
Reference types: null

Understanding variables and data types is fundamental to programming in Java. Properly using these elements allows for efficient and effective coding practices.

--

--

Yuvaraj
Yuvaraj

No responses yet