In Java, constants and variables are used to store and manipulate data.
They represent named memory locations that hold values of different types.
Here’s a brief explanation of constants and variables in Java:
1. Constants:
- Constants are identifiers whose values cannot be changed once they are assigned.
- In Java, constants are typically declared using the final keyword, which makes them unmodifiable.
- Constants are commonly used to represent fixed values, such as mathematical constants, configuration settings, or any value that should remain constant throughout the program.
- Constants can be of primitive types (e.g., int, double, char) or reference types (e.g., String, Object).
Example:
final int MAX_VALUE = 100;
final double PI = 3.14159;
final String MESSAGE = "Welcome!";
2. Variables:
- Variables are identifiers that hold data that can be modified during the program’s execution.
- Variables are declared by specifying the data type followed by the variable name.
- Variables can store values of primitive types or references to objects.
- The value of a variable can be assigned, updated, and used in various operations.
Example:
int age = 25;
double salary = 5000.0;
String name = "John Doe";
3. Variable Naming Rules:
- Variable names in Java should follow certain rules:
- They must start with a letter (a-z or A-Z), underscore (_), or dollar sign ($).
- After the first character, they can include letters, digits (0-9), underscores, or dollar signs.
- Variable names are case-sensitive.
- It’s recommended to use meaningful names that reflect the purpose of the variable.
- Reserved words (keywords) cannot be used as variable names.
4. Variable Initialization:
- Variables should be initialized with a value before they are used.
- Uninitialized variables have a default value depending on their data type (e.g., 0 for numeric types, false for boolean, null for reference types).
Example:
int x; // Uninitialized variable
x = 10; // Variable initialization
int y = 20; // Declaration and initialization in a single statement
Difference between Contants and Variables in Java :
Constants | Variables | |
Definition | Named memory locations with unchangeable values | Named memory locations with changeable values |
Declaration | Declared using the final keyword | Declared without the final keyword |
Value | Value cannot be modified once assigned | Value can be modified during program execution |
Naming | Typically named using uppercase letters and underscores | Typically named using lowercase letters and camel case |
Usage | Used to represent fixed values that remain constant throughout the program | Used to store and manipulate data during program execution |
Data Type | Can be of primitive types or reference types | Can be of primitive types or reference types |
Initialization | Must be initialized at the time of declaration or in a constructor/block | Must be initialized before use or assigned a value during runtime |
Example | final int MAX_VALUE = 100; | int count = 0; |