• 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

byte size of a string?

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pourang,
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.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic