When a user enters a string, is it a string literal?

I have a question to help me with my homework. We had to write a program using a string literal and print it in reverse order. I wrote it and it works great, but I have two different versions. The first uses a scanner so that the user can enter a string and then print in the reverse order. In the second, I declare a string literal and just print it in reverse order.

My question is not in my program, which works fine, but I can not find anywhere on the Internet or in my book, which says that the string entered is a string literal. I understand that a string literal is usually written as

String a = "Welcome to Java" 

but can be written as

 String a = new String("Welcome to Java") 
  • So, the input string does not match the string literal?
  • Does it need to be quoted as a string literal?

I suppose the answer is yes, since my book basically says that it should be in quotation marks, but I want to double check before I pass on my assignment that I will return to the correct version. Thanks in advance for any clarification!

+4
source share
4 answers

A string literal is one that is in quotation marks and literally in your code:

 String literal = "string literal"; 

The input string is not a string literal, but just a string.

+7
source

@Shakedown implies that a string literal is a syntax element of your Java source code. Also (in the free sense) a Java String object that matches this literal when executing your program. You can say that a literal letter denotes a run-time line. In addition, JLS indicates that two string literals with the same characters will designate the same String object in the context of simple program execution.

In your code snippets.

 String a = "Welcome to Java"; 

and

 String a = new String("Welcome to Java"); 

... the sequences of characters "Welcome to Java" in the source code are both string literals, and assuming that they are used in the same execution, they will denote the same (internally) String object.

However, new String("Welcome to Java") not a string literal. This expression creates a new String object. And the result of executing this expression is guaranteed to be another String object to the one indicated by the letters.


Do I need to write it in quotation marks to be considered a string literal?

Yes. And what's more, it must be quoted in the source code of your program in order to qualify as a string literal.

If I wrote a program that accepted the use of GUI input and the user entered input with double quote characters around it, it is NOT a string literal. Rather, it is a String that begins and ends with double quote characters. (Yes ... in this case, the string value will contain quotation marks.)

+3
source

A string literal ( "foo bar baz" ) is a String object - it's just good syntax to create such objects. When you receive input from the user, it will also be a String object - the same type as this literal.

However, the string literal refers to this syntax (material in quotation marks), and not to the type of the object. So, in order to accurately answer your question, user input is not a string literal.

+1
source

The string entered is no different from a string literal.

Strings are stored in Java as objects of the String class. One way to get it is a string literal: "hello sailor" is a string literal, and when used in a Java expression, it is an object of the String class. Methods that return strings also return objects of the String class. Thus, they are equivalent for all purposes.

There are ways to tell the literal from new String("...") , but they are pretty far-fetched. As a beginner, you do not need to worry about this.

+1
source

All Articles