In: Computer Science
What are the differences between a literal and a variable in Java?
Select one:
A literal is an exact expression representing the symbolic meaning of a value, a variable allows changes to be made to the expression.
A literal (or string literal) is a String value at a particular point in your program, a variable is a datatype that the literal can be stored under.
A literal is a constant value that is initialised with the keyword final, a variable is a memory address that can hold values and symbolic names.
A literal is a fixed and/or constant value at a particular point in your program, a variable is a memory address that can hold values and symbolic names.
An integer literal allows constant values to be stored at a particular memory address where it becomes a variable.
Following is the difference between literals and variables in java.
In computer progromming we need to store data in some memory locations .These reserved memory locations are called variables,Sometimes varibles are assigned with some constant/fixed value which can not be changed,these are called literals.
For example:
int x=10;
here int is data type,x is variable and 10 is literal.
literals are notations of representing fixed values
Variables are the reserved memory locations to store data with variable names
Now lets come to our question.
i).first stamenet is true as mentioned above and varibles allow to make changes.
A literal is an exact expression representing the symbolic meaning of a value, a variable allows changes to be made to the expression.
ii).In the second statement first half is correct as mentioned avove while in the second part variab;es are not the data type they are used to store the data not to define the data type so this in incorrect. You can refer to the example mentioned above
A literal (or string literal) is a String value at a particular point in your program, a variable is a datatype that the literal can be stored under.
iii).3rd statemnt is correct as we can define a variable with with final keyword and assigned some constant value(literal) to it. Variable is a memory address that can hold values and symbolic names as mentioned above.
for example:
final int x=10;
here int is data type, x is variable that hold the value 10 and 10 is literal.
A literal is a fixed and/or constant value at a particular point in your program, a variable is a memory address that can hold values and symbolic names.
iv).4th stament is also correct as literal is fixed /constant value while variable is memory adrress that can sotre tha value and assigned name (explained above).
A literal is a fixed and/or constant value at a particular point in your program, a variable is a memory address that can hold values and symbolic names
v).this stament is incorrect as a literal can not be a variable
An integer literal allows constant values to be stored at a particular memory address where it becomes a variable.
Highlighted ones are correct difference between literal and variable.