ernies
ernies

Reputation: 93

What's the difference between a string and a literal?

What is the type of "I like Comp Sci!"? I'm pretty sure its either a string or a literal, can anyone point out the difference between the two and help me find the answer

Upvotes: 4

Views: 1140

Answers (3)

Chris Vig
Chris Vig

Reputation: 8772

In most languages, it would be considered both a string and a literal. A string is a collection of characters that make up a section of text. A literal is an unconditional constant which is directly entered into your source code. They are not mutually exclusive.

Upvotes: 1

Gabe
Gabe

Reputation: 86708

A string is a sequence of characters. A literal is data that's typed in as part of the program. If you have "I like Comp Sci" typed into your program, then it's a string literal.

Upvotes: 5

Daniel Pereira
Daniel Pereira

Reputation: 1785

A string is a data type, but a string literal is when a string is defined literally usually in quotes:

String myString = "This is a literal string";

In the example String is the type, myString is a variable or reference name, and the text in quotes is a literal string.

Upvotes: 0

Related Questions