Question

I am taking an introductory Java class, and strongly suspect one of the answers to a quiz I took today may be incorrect. The question was as follows:

String name = "Grizzlies";

The above is an example of a __ variable.

There were two choices for the blank: class or literal.

I picked class (reluctantly), since calling something a "literal variable" did not make sense to me at all.

The answer key said "literal variable" is the correct answer.

Does this even make sense in Java? From my understanding, a literal is a fixed value that is expressed as itself rather than in the form of a variable, and so never changes and never gets assigned to a variable. Some example of literals include numeric literals such as 3.14 or String literals such as "Notre Dame".

A variable, in contrast, gets assigned a value and can and does change by its very definition.

So in my mind, it would be just as nonsensical to say something is a "constant variable" as it would be to say something is a "literal variable."

Can anyone verify whether my understanding is correct? If I'm wrong and the answer key is right, where am I going wrong with my thinking? If I'm right and the answer key is wrong, what should I tell my teacher?

Thank you very much.

No correct solution

OTHER TIPS

The terminology the quiz used is confusing (I've never heard someone refer to anything as a "literal variable"), but it is correct to say that "Grizzlies" is a "String literal".

It is kind of ok as of the duality of Java Strings. For once they are immutable, they are String literals, on the other hand the variable is nothing but a pointer to one of these immutable Strings and therefore variable.

One could call this "literal variable", while "variable that points to a literal" would be more correct, yet longer.

Taken from Java datatypes

"A literal is the source code representation of a fixed value; literals are represented directly in your code without requiring computation. As shown below, it's possible to assign a literal to a variable of a primitive type:"

Also its strange to call something a literal variable. Just literal or String literal makes more sense here.

As per JLS

There are seven kinds of variable. One of these is , class variable. Among the 7 types , there is no mention of Literal Variables. :) . Other notables are instance variable,Array components,Method parameters,Constructor parameters,An exception parameter and Local variables.

A class variable is a field declared using the keyword static within a class 
declaration , or with or without the keyword static within an 
interface declaration

Now ,since there is no body defined in your question , you cannot classify it as Local or Class etc.

String literals  are references to instances of class String.

So the answer should be reference variable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top