Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java- when a user inputs a string, is it a string literal?

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

My question isn't about my program which works fine, but rather i can't find anywhere online or in my book which says an inputted string 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 then is an inputted string not the same as a string literal?
  • Does it have to be written out in quotes to be considered a string literal?

I'm assuming the answer will be yes since my book basically says it has to be in quotes, but i want to double check before i hand in my assignment that i'm turning in the correct version. Thanks in advance for any clarification!

like image 228
user1576766 Avatar asked Dec 15 '22 21:12

user1576766


1 Answers

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

String literal = "string literal";

An input string is not a string literal, just a string.

like image 137
Nate W. Avatar answered Jan 01 '23 02:01

Nate W.