Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

BITS, Bytes, char, String

 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I need to confirm:
1 bytes = 8 bits
1 char = 1 byte
string is collection of chars
Am I right?
Thanks,
Angela
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1 byte = 8 bits correct.
1 char = 16 bits (Note that a short is also 16 bits)
A string is not a collection of chars in the same way C++ deals with it. It is a squence of Unicode characters.
From the JLS Spec


4.3.3 The Class String
Instances of class String represent sequences of Unicode characters. A String object has a constant (unchanging) value. String literals (�3.10.5) are references to instances of class String.

 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the JVM Spec
2.15.1 Array Types


In the Java programming language, unlike in C, an array of char is not a String (�2.4.7), and neither a String nor an array of char is terminated by '\u0000' (the NUL-character). A String object is immutable (its value never changes), while an array of char has mutable elements.

 
Angela D'souza
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all,
Is String is terminated by newline terminator?
Thanks
Angela
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
NO - there is no specific thing that YOU can look at that terminates a String.
 
Ranch Hand
Posts: 1512
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Angela, the Java Tutorial at Sun's site and Beginning Java 2 by Ivor Horton, helped me out a lot when I started to learn Java a few years ago. They will definitely help answer most of your questions. Actually any descent java textbook should have answers to most of your questions.
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Cindy Glass:
[QB]1 byte = 8 bits correct.
1 char = 16 bits (Note that a short is also 16 bits)
A string is not a collection of chars in the same way C++ deals with it. It is a squence of Unicode characters.

Isn't a char a Unicode character? What's the difference if both are 16 bits? (or am i completely confused?)
Thanks

 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Isn't a char a Unicode character? What's the difference if both are 16 bits? (or am i completely confused?)
Thanks



Yes a char is a Unicode character. Angela asked if a char was 8 bits. It is not.
It sort of depends on what she meant by "string is collection of chars". At String is a bunch of Unicode characters maintained in a special place. It is NOT a "collection" such as an array. In some languages Strings ARE just arrays of characters with a terminator character. This is not true in java.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic