• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

BITS, Bytes, char, String

 
Angela D'souza
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
  • 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
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • 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"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks to all,
Is String is terminated by newline terminator?
Thanks
Angela
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NO - there is no specific thing that YOU can look at that terminates a String.
 
Bosun Bello
Ranch Hand
Posts: 1510
  • Mark post as helpful
  • send pies
  • 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.
 
Lucy Smaile
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • 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"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic