Constant vs Variable vs Literal

Literal ->It means “A literal is some data that’s presented directly in the code, rather than indirectly through a variable or function call.”

The term “literal” comes from the fact that you’ve written data literally into your program, i.e. exactly as written, not “hidden” behind a variable name (i.e representing a fixed value in source code)


"hello world"

A literal is a value that has been hard-coded directly into your source.

For example:

string x = "This is a literal";
int y = 2; // so is 2, but not y
int z = y + 4; // y and z are not literals, but 4 is
int a = 1 + 2; // 1 + 2 is not a literal (it is an expression),
 but 1 and 2 considered separately are literals

The data constituting a literal cannot be modified by a program, but it may be copied into a variable for further use:

int a = 42;  // creates variable `a` with the same value as the
literal '42'
int i = 4;  // i is assigned the literal value of '4'
int j = i   // j is assigned the value of i.  Since i is a variable,  
            //it can change and is not a 'literal'

Assignment itself has nothing to do with the definition of a literal, but a literal can also be passed into a method, etc.

Variable -> a variable is a name that can represent different values during the execution of the program.

Constant -> a constant is a name that represents the same value throughout a program.

Note -> But a literal is not a name — it is the value itself.

A literal can be a number, a character, or a string. For example, in the expression,

x = 3

x is a variable, and 3 is a literal.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s