Java String contains extraneous data -
i have input in utf16le encoding. time input reaches code been through fileinputstream encased in filereader encased in linenumberreader.
the first line read gives string like:
"1 piece of data string"
however, looking string value along lines of:
[, 1, p, i, ...]
notice empty element start.
no string passed through couple of functions here , there, converted object , being put through paces. @ point, should first part of string (the 1 or in case number including decimals) passed function has parse actual number.
the content of string appears "1" in value says:
[, 1, p, i, ...]
so whole string still in there.
in case returns parseexception , print unparseable number exception messages , logging tels me "1" unparseable number.
the real problem appears leading empty element subsequent lines show similar behavior except leading empty element , parse.
a string (at least implementation in openjdk) stores char[], offset , count. actual content of string characters in char[] indices offset offset+count.
that means char[] can hold more characters string represents.
this done in order able share char[]s between different string instances.
for example, if have string value foobar , call .substring(3) on it, resulting string represent bar, may reference same char[]. second string have offset 3 bigger originals string , count that's 3 smaller.
all of works because string objects immutable: since no single string ever modify it's char[] in way, it's safe them share it.
this means inspecting string object in debugger might give false impression. therefore safest thing if want inspect string character-for-character call either tochararray() or call charat() in loop.
Comments
Post a Comment