• 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

size of a string

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
How to calculate the size of a string. I have a requirement wherein to calculate the size of an object. The closest that i would come up with is:

say for eg: a string is "amal"

now considering that a character is 2 bytes....so the size of above string is 8 bytes. Is my assumption right?

help appreciated
cheers
amal shah
 
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by amal shah:
Hello,
How to calculate the size of a string. I have a requirement wherein to calculate the size of an object. The closest that i would come up with is:

say for eg: a string is "amal"

now considering that a character is 2 bytes....so the size of above string is 8 bytes. Is my assumption right?

help appreciated
cheers
amal shah



How about this one
STRING LENGTH
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you need to know how many bytes exactly an object takes up in memory: There is no (easy) way to know that in Java. And it's also dependent on the implementation of the JVM; for example, a String object with a certain content might be 20 bytes when you're using Sun's JVM, and it might be 24 bytes when using IBM's JVM, and something different on someone else's JVM.

If you really need to know, then there is maybe (I'm not sure) a way to find out how much memory an object takes using the JVM's debugger API.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Generally speaking, Java was designed in such a way that you don't care about memory management, the size of objects or stuff like that.

An important question is WHY do you feel you need to know? What are you trying to accomplish?

If it's nothing more than a theoretical exercise for a class or for fun, that's one thing. But in the real world, this is seldom, if ever, an issue.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by amal shah:
...I have a requirement wherein to calculate the size of an object...


When I see questions like this, I always wonder if there's a professor out there who has thrown together a Java course using old material from other languages.

In any case, you might be interested in this article: JavaWorld - Sizeof for Java: Object sizing revisited.
 
amal shah
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

When I see questions like this, I always wonder if there's a professor out there who has thrown together a Java course using old material from other languages.




No..it's not for the above thing above. It has a purpose behind it.

An important question is WHY do you feel you need to know? What are you trying to accomplish?

If it's nothing more than a theoretical exercise for a class or for fun, that's one thing. But in the real world, this is seldom, if ever, an issue.



My purpose is for the use in J2ME RMS.

Say for example if i want to store 2 strings one int data and a short data in a record. In order to differentiate between this kind of data while retrieval i have to place some Unique Identifier. But in rarest of the rare case this Unique Identifier's also appear in the data,so
all things start going wrong then.

So the best idea possible was to store data in following format:

[size data size data]

Help in this regard would be highly appreciated
cheers
amal shah
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can find the length of a String by calling its length() method. So I don't see any need for storing the length of the String as well as the String itself. That's just redundant. And you don't want to be storing redundant data in small configurations like J2ME.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agreed. If all you want to do is store/retrieve the string in/from a text file, then the writing/reading the length of the string (using the length() method) is good enough. You don't need to know the actual size of the string object, which includes implementation and JVM details.

Henry
 
amal shah
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
In J2ME RMS, storage of data is always in byte format. So the concept of "length of a string" won't work.

help appreciated
cheers
amal shah
 
amal shah
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
In J2ME RMS, storage of data is always in byte format. So the concept of "length of a string" won't work.

help appreciated
cheers
amal shah
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since you posted in the Java in General (beginner) forum I assumed you were asking a question about Java. But it appears you were asking about something completely different. You should have made that clear long ago.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know what J2ME RMS format is, but...:

You convert characters to bytes and vice versa using a certain character set encoding, for example ASCII or UTF-8. Class String has several getBytes() methods that convert the characters in the string to an array of bytes using a certain character set. Have a look at the API documentation of class String.
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
RMS is Record Management Store (i. e., database). The question appears to be "how much memory does a string occupy in RMS store? Which, of course, is totally different than how much memory a Java String object occupies, since among other things, a RMS record may not reside in primary RAM - it may be stored on a memory card.

I'm not a major expert in RMS, but I got the impression it's a lot like the Palm Database format, and strings in PDBs I believe were normally in C format (text with trailing null), for a total length (non-Unicode) of n+1 bytes, where n is the string length. This is disregarding any compression that might be applied.

Also note that String.getBytes doesn't simply dump bytes out of the string's internal buffer - it does a code conversion, and that may result in the Java String (16-bit characters) being returned in something like UTF-8 (8-bit character) format.

The storage occupancy of Java Strings is, as was mentioned, quite another matter, and it's virtually impossible to predict, since while on the one hand, each java.lang.String object takes up a certain amount of RAM, but the actual string text is subject to all sorts of optimizing tricks (since Strings are immutable).
 
Tell me how it all turns out. Here is a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic