Originally posted by Thomas Paul:
Strings in Java are characters, not bytes so a String is made up of 16 bits. In Java it is not possible to create the scenario you are describing. We don't allocate Strings by length. We allocate Strings by the contents they hold. Java does not use null-terminated Strings. It actually uses a char array supported by a class to control the array.
Characters do have 16 bits in Java.
Consider the following code:
String str="example";
File f = new File("data.file");
FileOutputStream fos = new FileOutputStream(f);
DataOutputStream dos = new DataOutputStream(fos);
dos.writeBytes(str);
(or)
OutputStreamWriter osw = new OutputStreamWriter(dos, "UTF-8");
osw.write(str);
In both cases, each character in the string is stored as a byte (8-bits). So, it is possible for me to store and retieve characters (in 8-bit format). Am I making any mistake above?
Dayanand.