• 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

how many bytes in char

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is a question from jawroski quiz sentence construction may be slightly different
String s = "test" ;
System.out.prinln(s);
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?
import java.io.*;
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)>
outStream.write(s.charat(i));
outStream.close();
}
}
[This message has been edited by kameshwar (edited April 29, 2000).]
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
Thanks
 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
kameshwar,
Are you sure about the exact wording of the qstn?. As pointed o, by Suma did the author mention anything about the length?
regds
maha anna
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
---
import java.io.*;
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 ++ )
outStream.writeChar(s.charAt(i));
outStream.close();
}
}
----
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.
cheers,
vivek
#include <std_disclaimer.h>
_I_am_a_novice-_forgive_/_correct_me_if_i_m_wrong
 
Ranch Hand
Posts: 289
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HereUnder is the question from Jaworski, reproduced Verbatim:
-----------------------------------------------------------------
How many bytes does the following program write to temp.txt?
import java.io.*;
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)>
outStream.write(s.charAt(i));
outStream.close();
}
}
-----------------------------------------------------------------
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 :
-----------------------------------------------------------------
import java.io.*;
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)>
outStream.write(s.charAt(i));
outStream.close();
// 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.
System.out.println(inStream.available());
inStream.close();
}
}
----------------------------------------------------------------
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.
Herbert.
 
Do you pee on your compost? Does this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic