• Post Reply Bookmark Topic Watch Topic
  • New Topic

What's the difference between String and char  RSS feed

 
Lenin Dias
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.......
Why there are variable declarations as      String word = “bottles”       -other than-        char word = “bottles” ?
 
Stephan van Hulst
Saloon Keeper
Posts: 7964
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

The char type represents a single character*, and the String type is a sequence of zero or more characters.

char word = "bottles" is not valid Java. "bottles" is a String and you can not assign that to a char variable.

* Actually, char represents a single UTF-16 code unit, but that's a more advanced topic.
 
Duc Ta
Ranch Hand
Posts: 46
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Himantha Dias wrote:Hi.......
Why there are variable declarations as      String word = “bottles”       -other than-        char word = “bottles” ?


char is one of eight primitive types in Java
String is not a primitive, a String is an object
 
Lenin Dias
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot......!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!