This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Java in General and the fly likes byte size of a string? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "byte size of a string?" Watch "byte size of a string?" New topic

byte size of a string?

Pourang Emami
Ranch Hand

Joined: Sep 21, 2002
Posts: 127
Would you please let me know:

1-How the byte size of a string can be determined?
For instsance how much is the byte size of s in the following statement:
String s = new String"ABC";

2-How could I create a String with a specific size?(for instance a String with th esize of 100 bytes)

Thank You,

Best Regards,<br />Pourang Emami
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33132

1) s.length() will give you the number of bytes. Since characters are one byte (at least in ASCII), the number of characters is the same as the number of bytes. Another way is to get the bytes themselves and count them s.getBytes().length.

2) As Strings are immutable, you would need to have a 100 byte array with data on hand at string creation time and pass it to the constructor.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Characters are 2 bytes in Java (not 1).
This does not give you the ability to measure the memory consumption of a String instance. This is impossible to achieve, since it is undefined.

Tony Morris
Java Q&A (FAQ, Trivia)
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
As Jeanne and Tony's answers make clear, it depends on what you want to do with the String.

In Java, the primitive char type is 2 bytes as they are Unicode values. As String uses a char[] to hold the characters, in memory the characters themselves take 2 * s.length() bytes. This doesn't count the overhead of the array itself or the String. This is just the block of bytes for the characters.

If you want to write a String to a file or send it over a Socket, you need to first convert it to bytes. By default, Java uses a modified form of UTF-8 to encode and decode char[] to byte[]. For standard ASCII characters (everything in my post), it maps the lower byte of the char directly to a byte.

For example (\u??? is a Unicode char constant and 0x?? is a byte constant, both in hexadecimal -- base 16 rather than base 10):The short of it is, if you want to create a String that will encode into 100 bytes, create a String of 100 ASCII characters.

Of course, if your goal is to send/write byte arrays, why not just create one and skip the String entirely?
[ January 31, 2005: Message edited by: David Harkness ]
I agree. Here's the link:
subject: byte size of a string?
It's not a secret anymore!