Page 45 - CA_Blue( J )_Class10
P. 45
3.6.2 Assigning a Value to a Variable
The process of assigning a value to a variable is called an assignment. The process of assigning the initial value to a
variable is called initialization. Assignment carried out with the help of assignment operator "=". This operator assigns
the value on the right side of it to the variable on the left side of it. There are mainly three ways to assign the values
to the variables which are:
• At the time of declaration: You can assign a value to a variable at the time of its declaration. For example:
int roll_no = 10;
You can also assign values to multiple variables in the same line. For example:
int roll_no = 10, age = 15;
• Declaring and Initializing Separately: Java also allows you to declare and initialize a variable in separate lines. For
example:
int roll_no, age;
roll_no = 10;
age = 15;
Notice that when you initialize a variable after declaring it, the data type is not required.
• Assigning Expressions: You can also assign an expression to a variable. For example:
int a, b, c;
a = 10;
b = 20;
c = a + b;
Program 1: To use different types of variables and data types.
You will get the following output:
3.6.3 Constants
In Java, constants refer to fixed values that do not change throughout the execution of a program. Constants are
typically declared using the final keyword, which makes the variable immutable (unable to be modified once assigned).
Constants are often written in uppercase letters to distinguish them from other variables, though this is just a naming
convention, not a requirement.
43
Values and Data Types 43

