permaculture playing cards*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Null teminating a string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Null teminating a string" Watch "Null teminating a string" New topic
Author

Null teminating a string

Dayanand Kangala
Greenhorn

Joined: Sep 09, 2002
Posts: 26
Hi,
Have a basic doubt!
Consider we have a string of length 50 (8-bit) characters. If we store a value which has n (less than 50) characters, how do we "null-terminate" the string (for the remaining 50-n characters)?
Dayanand.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
There's really no need. You won't go past the last char of a String, unlike in C. If you want to shorten the String to fit, you can use trim().
[ September 11, 2002: Message edited by: Anthony Villanueva ]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Dayanand Kangala:
Consider we have a string of length 50 (8-bit) characters. If we store a value which has n (less than 50) characters, how do we "null-terminate" the string (for the remaining 50-n characters)?
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.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Dayanand Kangala
Greenhorn

Joined: Sep 09, 2002
Posts: 26
Originally posted by Anthony Villanueva:
There's really no need. You won't go past the last char of a String, unlike in C. If you want to shorten the String to fit, you can use trim().
[ September 11, 2002: Message edited by: Anthony Villanueva ]

Correct. But, I have to interact with a legacy database which requires all records to be of fixed length!
I was told to terminate strings with less than field length with null characters ! I have currently appended "space"s to the value, but is this the same as appending null-characters?! (I am not sure if other legacy systems accessing the database would recognize the data if space is not the same as null characters).
Dayanand Kangala
Greenhorn

Joined: Sep 09, 2002
Posts: 26
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.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Strings in Java always contain 16bit characters. You can convert them to 8bit bytes as you did in your example.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
See this:
http://java.sun.com/docs/books/tutorial/java/data/cStrings.html
Java Strings are not compatible with C/C++ Strings. You may want to think about using a byte array instead and inserting a null character as the last entry in the array.
 
GeeCON Prague 2014
 
subject: Null teminating a string