Why this is invalid in java?
List<String> items=new ArrayList<String>();
items.add("hello"); // valid
items.add('hello'); // invalid (error: invalid character constants)
Why this is invalid in java?
List<String> items=new ArrayList<String>();
items.add("hello"); // valid
items.add('hello'); // invalid (error: invalid character constants)
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