-1

Why this is invalid in java?

List<String> items=new ArrayList<String>();
items.add("hello"); // valid
items.add('hello'); // invalid (error: invalid character constants)
  • Does this answer your question? [Is there a difference between single and double quotes in Java?](https://stackoverflow.com/questions/439485/is-there-a-difference-between-single-and-double-quotes-in-java) – Ruslan Jun 24 '21 at 13:39

1 Answers1

1

Because '' single quotes are used for char variables. Meaning 'a', 'b', and so on. So only one character per variable.

The double quotes on the other side "" are used to initialize String variables, which are several characters gathered together into one variable e.g "i am a string"

char c = 'c' // I use single quotes because I am single :)
String str = "This is a string" // I use double quotes because I got a lot in me
Renis1235
  • 4,116
  • 3
  • 15
  • 27