Skip to main content

Variables and constants

Variables:

  • Variables are containers for storing data values.
  • In Java, each variable must be declared with a specific data type, which determines the type and size of data it can hold.

Declaration

  • Declaring a variable introduces its name and data type to the program.

  • To declare a variable, you need to specify the data type followed by the variable name.

  • Here's the syntax:

dataType variableName;

For example, the following statement declares a variable named age of type int:

int age;

Initialization

  • Initialization assigns a value to a variable.
  • You can initialize a variable at the time of declaration or later in the program.

Here's the syntax:

dataType variableName = value;
  • For example, the following statement initializes the age variable to 25:
int age = 25;

Changing Variable Values

  • The value of a variable can be changed throughout the program.
  • For instance, the following statement increments the age variable by 1:
age = age + 1 
//or age++;

Using Variables in Expressions

  • Variables can be used in expressions to perform computations.
  • For example, the following statement calculates the sum of two variables and assigns the result to another variable:
int sum = age + 10;

Examples of Variable Declaration and Initialization

Let's look at some examples to understand how variables are declared and initialized in Java.

  1. Integer Variable
  • Data Type: int
  • Code: int age = 25;
  • Explanation: This line declares an integer variable named age and assigns it a value of 25.
  1. Double Variable
  • Data Type: double
  • Code: double salary = 50000.75;
  • Explanation: This line declares a double variable named salary and assigns it a value of 50000.75. A double is used for decimal numbers.
  1. Character Variable
  • Data Type: char
  • Code: char grade = 'A';
  • Explanation: This line declares a char variable named grade and assigns it the value 'A'. A char is used to store a single character.
  1. String Variable
  • Data Type: String
  • Code: String name = "Ram";
  • Explanation: This line declares a String variable named name and assigns it the value "Ram". A String is used to store a sequence of characters.

Variable Naming Rules

  1. Case-sensitive: Variable names are case-sensitive (age and Age are different).
  2. Start with a letter: Variable names must start with a letter, a dollar sign ($), or an underscore (_).
  3. No spaces: Variable names cannot contain spaces.
  4. Reserved words: Variable names cannot be Java reserved words (e.g., class, int).

Constants

  • Constants are variables whose values cannot be changed once they are assigned.
  • They are used to define values that remain constant throughout the execution of a program.

Defining Constants

  • Constants in Java are declared using the final keyword.
  • By convention, constant names are written in all uppercase letters with words separated by underscores.

Examples of Constant Declaration

Now, let's see how to declare constants in Java.

  1. Integer Constant
  • Modifier: final
  • Code: final int MAX_VALUE = 100;
  • Explanation: This line declares a constant integer named MAX_VALUE and assigns it a value of 100. The final keyword ensures that the value cannot be changed.
  1. Double Constant
  • Modifier: final
  • Code: final double PI = 3.14159;
  • Explanation: This line declares a constant double named PI and assigns it the value 3.14159. The value of PI is a common mathematical constant.
  1. String Constant
  • Modifier: final
  • Code: final String GREETING = "Hello";
  • Explanation: This line declares a constant String named GREETING and assigns it the value "Hello". The final keyword ensures that the value cannot be changed.

Why Use Constants?

  1. Readability: Constants make your code more readable by providing meaningful names for fixed values.

  2. Maintainability: If a constant value needs to change, you only update it in one place.

  3. Avoid Magic Numbers: Constants help you avoid "magic numbers" (hard-coded values) in your code.

Using Constants in Expressions

Constants can be used in expressions like variables. For instance, the following statement calculates the circumference of a circle using the constant PI:

double circumference = 2 * PI * radius;

Summary

Here's a quick recap:

Variables:

  • Used to store data that can change.

  • Declaration Syntax: dataType variableName;

  • Initialization Syntax: dataType variableName = value;

  • Examples: int age = 25;, double salary = 50000.75;, char grade = 'A';, String name = "Ram";

  • Naming Rules: Case-sensitive, start with a letter/dollar sign/underscore, no spaces, not reserved words.

Constants:

  • Declaration Syntax: final dataType CONSTANT_NAME = value;

  • Examples: final int MAX_VALUE = 100;, final double PI = 3.14159;, final String GREETING = "Hello";

  • Benefits: Improve readability, maintainability, and avoid magic numbers.