• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String vs. Char

 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,
I am a beginner to Java. Can anyone tell me what's the different between String and Char?
Thank you very much!
 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mindy:
Pl. post your qstn in the beginners forum, it will get
the right attn. Pl. don't be surprised if this thread
is moved.
regds.
- satya
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mindy,
A String is a collection of charecters (just like the literal meaning of the word-"String" eg:- "a String of charecters"). And char is a single letter or a number.
In Java, char is called a primitive datatype, whereas a String is a class. char occupies 16 bits(2 bytes), and must have its literal(i.e-the letter or number you use) enclosed in single quotes('')....eg- char ch = 'A';
A String on the other hand is always enclosed in double quotes(""). And when you do String str="This is a new string";, you are actually creating a reference variable str pointing to an object with "This is a new string" in it.
When you create char or String as instance variables in your program, then char is initialized to '\u0000' (something of not much use ) and a String variable being a object reference is initialized to null.
Hope this helps
Rebecca
 
Mindy Wu
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Rebecca! It does help me understand better.
 
reply
    Bookmark Topic Watch Topic
  • New Topic