1

As i know,a Java object contains object header,reference and it's data.

//jdk 1.7
public final class String{
       private final char value[];
       private int hash;
       //...
    }

For new String("abc") ,how many bytes will it take if we just think about 32-bit system?

totleBytes = 
    8//String Object header
    +4 //value reference 
    +4 //hash
    +12 //char[] object header
    +3*1//value[] data

does it right,or miss something?

Kerwin
  • 1,212
  • 1
  • 7
  • 14
  • 1
    Java uses UTF-16 internally, so the char data itself would be 6 bytes in the given example, and generally varies from multiples of length since UTF-16 uses variable-length encoding. – Jiri Tousek Oct 11 '16 at 11:00
  • 1
    Have a look at http://openjdk.java.net/projects/code-tools/jol/ – Mikhail Kuchma Oct 11 '16 at 11:50

0 Answers0