This week's book giveaway is in the OCP forum.
We're giving away four copies of OCP Java SE 8 Programmer II Exam Study Guide and have Kathy Sierra, Bert Bates, & Elizabeth Robson on-line!
See this thread for details.
Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

What's the difference between String and char  RSS feed

 
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” ?
 
Saloon Keeper
Posts: 8580
161
  • 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.
 
Ranch Hand
Posts: 58
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......!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!