Explain the term constants, variables, identifiers and literals.
Q.) Explain the term constants, variables, identifiers and literals.
Subject: Object Oriented ProgrammingIntroduction
In the context of Object Oriented Programming (OOP), constants, variables, identifiers, and literals are fundamental concepts that form the building blocks of any program. Understanding these terms is crucial to mastering any OOP language, such as C++ or Java.
Constants
Constants are fixed values that do not change during the execution of a program. They are declared using the const
keyword in C++ or the final
keyword in Java. For example, the declaration const int PI = 3.14;
in C++ or final double PI = 3.14;
in Java creates a constant named PI with a value of 3.14. Constants are usually declared and initialized at the time of declaration, and their value cannot be changed afterwards.
Variables
Variables, on the other hand, are named storage locations whose values can be manipulated during the execution of a program. They are declared by specifying a data type and a name, for example int x;
in both C++ and Java. A variable can be assigned a value, such as int age = 20;
, and this value can be changed during the execution of the program. Variables have a data type, name, and value.
Identifiers
Identifiers are the names given to elements in a program like variables, functions, classes, etc. For example, in the declaration int myVariable;
, myVariable
is an identifier. Identifiers must start with a letter or an underscore, and can contain letters, digits, or underscores. They are case-sensitive and must not be a reserved keyword in the programming language.
Literals
Literals are data items that have fixed values in the source code. For example, "Hello, World!" is a string literal, 10 is an integer literal, 3.14 is a floating-point literal, 'a' is a character literal. Literals can be of various types like integer, floating-point, character, string, etc.
Comparison Table
Term | Definition | Example | Properties |
---|---|---|---|
Constant | Fixed value that does not change during execution | const int PI = 3.14; |
Declared and initialized at the time of declaration |
Variable | Named storage location whose value can be manipulated | int age = 20; |
Has a data type, name, and value |
Identifier | Name given to elements in a program | int myVariable; |
Must start with a letter or underscore, can contain letters, digits, or underscores |
Literal | Data item with a fixed value in the source code | "Hello, World!" |
Can be of various types like integer, floating-point, character, string, etc. |
Conclusion
In conclusion, constants, variables, identifiers, and literals are fundamental concepts in Object Oriented Programming. Constants are fixed values, variables are named storage locations with mutable values, identifiers are names given to elements in a program, and literals are data items with fixed values in the source code. Understanding these terms and their differences is crucial to mastering any OOP language.
Summary
Constants, variables, identifiers, and literals are fundamental concepts in Object Oriented Programming. Constants are fixed values that do not change during the execution of a program. Variables are named storage locations whose values can be manipulated. Identifiers are the names given to elements in a program. Literals are data items that have fixed values in the source code.
Analogy
Constants are like the North Star, always fixed and never changing. Variables are like containers that can hold different things. Identifiers are like name tags that identify different objects. Literals are like the words written on a page, they have a fixed meaning.
Quizzes
- Fixed values that do not change during the execution of a program
- Named storage locations whose values can be manipulated
- Names given to elements in a program
- Data items with fixed values in the source code