javastringcharmemory-optimization

Is there a difference in SIZE between String "a" and a Char 'a'?


Is there a difference in size between a String "a" and a Char 'a'? Should you use a Char when you need to save single letters or numbers?


Solution

  • Note there are actually 3 cases

    String "a"
    Character 'a'
    char 'a'

    of these, char 'a' will take up the least amount of space (2 bytes), whereas Char 'a' and String "a" are both objects and so because of the memory overhead associated with an Object, both will be approximately the same size