GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes BITS, Bytes, char, String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "BITS, Bytes, char, String" Watch "BITS, Bytes, char, String" New topic
Author

BITS, Bytes, char, String

Angela D'souza
Ranch Hand

Joined: Jan 16, 2002
Posts: 469
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

Joined: Sep 29, 2000
Posts: 8521
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.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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

Joined: Jan 16, 2002
Posts: 469
Thanks to all,
Is String is terminated by newline terminator?
Thanks
Angela
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
NO - there is no specific thing that YOU can look at that terminates a String.
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
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.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Lucy Smaile
Ranch Hand

Joined: Apr 17, 2002
Posts: 45
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

Joined: Sep 29, 2000
Posts: 8521

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.
 
GeeCON Prague 2014
 
subject: BITS, Bytes, char, String