@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.)
source share