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
Asked
Active
Viewed 631 times
4
-
Try this: http://stackoverflow.com/questions/3297867/difference-between-string-object-and-string-literal – mal-wan Sep 09 '11 at 01:03
-
6The two terms aren't mutually exclusive (that is, something can be a "string literal"). Better check your textbook. – larsks Sep 09 '11 at 01:03
-
@mwan: That question is very specific to JavaScript. – Gabe Sep 09 '11 at 01:03
-
@Gabe - It's actually very specific to Java, but point taken - the question lacks context. – mal-wan Sep 09 '11 at 01:05
-
@mwan: Oops, you're right; it's Java. The context of the question is just generic CompSci terminology. – Gabe Sep 09 '11 at 01:12
3 Answers
5
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.

Gabe
- 84,912
- 12
- 139
- 238
1
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.

Chris Vig
- 8,552
- 2
- 27
- 35
-
I disagree. It's not a language specific concept. A literal is represented by the actual string of characters, but a string is just a data type that can be represented by a reference name. But, programmers definitely use the term "string" for both, but you would never say String myString; is a string literal. – Daniel Pereira Sep 09 '11 at 01:11
0
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.

Daniel Pereira
- 1,785
- 12
- 10