Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

what is the size of "abcd"?  RSS feed

 
Jerry Lee
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something confuses me for a while, please help us if you know this.

What is the size of a string "abcd" in Java? In other words, how many bytes it is?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the data for "abcd" is 4 characters. Read up on "Unicode Character Representations" in the doc for Character to see how much memory each character might use.

I'm not clear if you're asking about that size, or the size of the object. We're not meant to know how much memory any Java object takes, leaving the JVM implementors some freedom in how they do things or even change from one release to another.

Finally, Strings have some special handling with literal pools and interning and such. See String doc for more on that.

Did any of that hit the mark?
 
Allan Jacobs
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There was a good summary of string length concepts and utilities
in September 2006. It's URL is http://java.sun.com/mailers/techtips/corejava/2006/tt0822.html
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!