开发者

the data type "char" in java

why doesnt any of these work:

 char word = "sds";
 char word = 'sds';
 myDog.ba开发者_如何学JAVArk("voff");
 myDog.bark('voff');

in the object to myDog i have typed:

 void bark(char word) {
      System.out.println(word);
 }


Because a char is just a single character. You want to use the String type instead.

void bark(String word) {
   System.out.println(word);
}


You want to use "String" not "char". char is only for 1 character, "String" is for multiple characters.

With "String" type you use double-quotes, with "char" you use single quotes:

char c = 'a';
String s = "hello";


The char data type can only contain one character. For multiple characters, you should use the String data type.


char is one character, String is a sequence of chars. You are looking for a String

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜