In reference to the link: File IO Tuning, last section titled “Further Tuning” where the author suggests using char[] to avoid generating String objects for n lines in the file, I need to understand how does
char[] arr = new char{'a','u','t','h', 'o', 'r'}
differ with
String s = "author"
in terms of memory consumption or any other performance factor? Isn’t String object internally stored as a character array? I feel silly since I never thought of this before. 🙂
In Oracle’s JDK a
Stringhas four instance-level fields:That means that each
Stringintroduces an extra object reference (theStringitself), and three integers in addition to the character array itself. (The offset and character count are there to allow sharing of the character array amongStringinstances produced through theString#substring()methods, a design choice that some other Java library implementers have eschewed.) Beyond the extra storage cost, there’s also one more level of access indirection, not to mention the bounds checking with which theStringguards its character array.If you can get away with allocating and consuming just the basic character array, there’s space to be saved there. It’s certainly not idiomatic to do so in Java though; judicious comments would be warranted to justify the choice, preferably with mention of evidence from having profiled the difference.