Java Variables

Variables in Java are containers that hold data values during program execution. Each variable has a specific data type that determines the kind of data it can store, such as integers, floating-point numbers, characters, or strings.

Key Topics

1. Declaring Variables

To declare a variable, specify the data type followed by the variable name.

int age;
String name;
boolean isStudent;

2. Initializing Variables

You can assign a value to a variable at the time of declaration or after declaring it.

int age = 25;
String name = "Alice";
boolean isStudent = true;

// Initializing after declaration
int height;
height = 170;

3. Variable Types

Variables can be of different types:

  • Primitive Types: byte, short, int, long, float, double, boolean, char
  • Reference Types: Classes, Interfaces, Arrays, Strings

Key Takeaways

  • Variables store data for processing in Java programs.
  • Declare variables by specifying the data type and name.
  • Initialize variables by assigning them a value.
  • Understand the difference between primitive and reference types.