Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

Constants and Variables in Java

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 :

ConstantsVariables
DefinitionNamed memory locations with unchangeable valuesNamed memory locations with changeable values
DeclarationDeclared using the final keywordDeclared without the final keyword
ValueValue cannot be modified once assignedValue can be modified during program execution
NamingTypically named using uppercase letters and underscoresTypically named using lowercase letters and camel case
UsageUsed to represent fixed values that remain constant throughout the programUsed to store and manipulate data during program execution
Data TypeCan be of primitive types or reference typesCan be of primitive types or reference types
InitializationMust be initialized at the time of declaration or in a constructor/blockMust be initialized before use or assigned a value during runtime
Examplefinal int MAX_VALUE = 100;int count = 0;
Java in Hindi Video