【问题标题】:Question about java String "hello" and 'hello'关于 java String "hello" 和 'hello' 的问题
【发布时间】:2021-09-08 00:16:09
【问题描述】:

为什么这在java中无效?

List<String> items=new ArrayList<String>();
items.add("hello"); // valid
items.add('hello'); // invalid (error: invalid character constants)

【问题讨论】:

标签: java string list arraylist


【解决方案1】:

因为'' 单引号用于char 变量。 意思是'a''b',等等。所以每个变量只有一个字符。

另一边的双引号""用于初始化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

【讨论】:

    猜你喜欢
    • 2013-12-03
    • 2013-11-25
    • 2014-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-29
    • 1970-01-01
    相关资源
    最近更新 更多