This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes how many bytes  in char Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "how many bytes  in char" Watch "how many bytes  in char" New topic

how many bytes in char

Kameshwar Prasad

Joined: Dec 04, 2007
Posts: 12
this is a question from jawroski quiz sentence construction may be slightly different
String s = "test" ;
how many bytes did s have
ans he gave is 4 bytes
how come this as char is having 2 bytes it must be 8 bytes ?
here is the full code from jawroski quiz i am adding
How many bytes does the fallowing program write to temp.txt?
public class TestIOApp{
public static void main(String args{})throws IOException{
FileOutputStream outStream = new FileOutputStream("text.txt");
String s = "text";
for(int i =0 ;i<s.length();++i)>
[This message has been edited by kameshwar (edited April 29, 2000).]
Ajith Kallambella

Joined: Mar 17, 2000
Posts: 5782
I think you are right, it should be 8 bytes.
String stores the characters in the unicode
format which means, each character occcupies
2 bytes.
Any takers on this one?

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Deepa sivasankar
Ranch Hand

Joined: Jul 22, 2003
Posts: 35
Strings are objects and not like C type strings. Char in java is stored as 16bit and not strings.
Maha, is this right?. I appreciate your help in answering the Q.
Suma Narayan
Ranch Hand

Joined: Apr 03, 2000
Posts: 136
In Java, both strings and string buffers contain sequences of 16-bit unicode characters. Like values of type char, strings are also stored internally as Unicode characters and hence each one occupies 2 bytes.
I think the question should have been about the length of the string and not the space occupied.
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
The first 128 unicode chars in unicode chart are our famous ASCII. and each unicode char occupy 2 bytes (16 bits). In a String object each char from index 0 to (length-1) occupy 2 bytes. Since in the above post ASCII chars are used as String s = "test" it will occupy 4*2 = 8 bytes memory in heap.
Are you sure about the exact wording of the qstn?. As pointed o, by Suma did the author mention anything about the length?
maha anna
Shashank Jha

Joined: May 15, 2000
Posts: 9
I believe I had read someplace that String.length=no.of bytes it takes unlike Char.Sorry to be so vague but if anyone is sure please clarify.
Tom P
Ranch Hand

Joined: May 06, 2000
Posts: 88
String.length() returns the number of characters in the String not the number of bytes in the String. A String could be changed so each character takes 100 bytes and the String.length() of String s="Test"; would still return 4. In fact, it really doesn't matter how Strings store their data. It is irrelevant to the developer. We only need to know what types of characters can be stored inside a String not how the actual implementation of the String stores the data.
vivek rai
Ranch Hand

Joined: May 08, 2000
Posts: 45
hi gurus,
the String has 4 characters ( which also appears as the length of the String, 'in characters' ), which take up 8 bytes in memory.
I have a feeling that the question might actually be about the concept of character stream and byteStreams ( and file encoding).
In this case, we are use FileOutputStream and that shall create a file of size 4 bytes , as the 2 byte java unicode characters shall be converted to the default OS file encoding.
We could change/avoid file.encoding etc.. to write the actual 8 bytes to the file..
for example..
public class TestIOApp2
public static void main(String args[])throws IOException
DataOutputStream outStream = new DataOutputStream
( new FileOutputStream("text.txt") );

String s = "text";
for(int i =0 ;i < s.length() ; i ++ )
this would force java char encoding and write the actual 8 bytes to the file.
maybe that was what being asked.. bcoz I System.out is an object PrintStream class.. and "All characters printed by a PrintStream are converted into bytes using the platform's default
character encoding. The PrintWriter class should be used in situations that require writing
characters rather than bytes. " ( from 1.2 API doc, class PrintStream )
So println would acually write 4 bytes.. ( on most operating systems )
Sorry if i have mentioned something too trivial/ or digressed too far.
#include <std_disclaimer.h>
Herbert Maosa
Ranch Hand

Joined: May 03, 2000
Posts: 289
HereUnder is the question from Jaworski, reproduced Verbatim:
How many bytes does the following program write to temp.txt?
public class TestIOApp{
public static void main(String args[])throws IOException{
FileOutputStream outStream = new FileOutputStream("test.txt")
String s = "test";
for (int i=0;i<s.length();++i)>
The correct answer as given by Jaworski is 4 bytes. I do agree with this answer and you can verify( as I did) by peeping into the file as follows :
public class TestIOApp{
public static void main(String args[])throws IOException{
FileOutputStream outStream = new FileOutputStream("test.txt")
String s = "test";
for (int i=0;i<s.length();++i)>
// Herbert's additional code to peep into the file size
FileInputStream inStream = new FileInputStream("test.txt"); //The line below on my computer prints 4, just as I expect.
My Idea :
This question wants us to appreaciate the difference between input/output stream VS Reader/Writer. It is true that a char has 2 bytes, ordinarily. But I think how i gets stored will depend on whether we store using byte stream or Writer. I think that only Writer recognizes unicode ans respects the true size of whatever we are writing, but as long as we are writing out using a byte stream, as in this code, then everything is considered as a byte.So in the case above, each of the four chars are stored as 1 byte each. You can convince yourself by using Writer instead of stream and the result will be 8 bytes as we all expect.
I stand to be corrected.
It is sorta covered in the JavaRanch Style Guide.
subject: how many bytes in char
Similar Threads
Have I found an error with the jaworski mock exam
char = 1 byte ?? full code from jawroski quiz
How is this?
I/O Clarification