简体   繁体   English

关于java字符串“hello”和“hello”的问题

[英]Question about java String “hello” and 'hello'

Why this is invalid in java?为什么这在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.因为''单引号用于char变量。 Meaning 'a' , 'b' , and so on.意思是'a''b'等等。 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 eg "i am a string"另一侧的双引号""用于初始化String变量,将几个字符聚集到一个变量中,例如"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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM