简体   繁体   中英

the data type “char” in java

why doesnt any of these work:

 char word = "sds";
 char word = 'sds';
 myDog.bark("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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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