Prev: Facing exception: Invalid byte 2 of 4-byte UTF-8 sequence.
Next: redirect System.out to JTextArea
From: Eric Sosman on 22 Jan 2010 11:06 On 1/22/2010 9:25 AM, Lew wrote: > Mark wrote: >>> I use the following code (exception handling removed for brevity): >>> String text = new String(""); > > GK wrote: >> better use: String text = ""; >> --> your code constructs the empty string and then constructs a new >> empty string using the empty string. > > Nope. The compiler and class initialization construct the empty string, > interning it into the string pool. That code line at execution then > constructs a new empty string based on the already existing interned > empty string. > >> @Roedy: you suggesting using StringBuilder which is never wrong, but >> AFAIK later java [sic] releases do that optimization during compile. > > Not in the scenario of the OP's code: > > text += (char)c; > > Each iteration through the loop creates a new 'String' and assigns it to > 'text'. Decomposing a bit further, each iteration creates - A new StringBuilder, which in turn owns a new char[] - Another new char[] if text.length() > 16 - Yet another new char[] if text.length() is exactly 16, 34, 70, 142, ... - A new String, which in turn owns still another char[]. So: Four to six new objects per iteration. If building an N-char String this way, the total will be 4 * N + (N > 16 ? N-16 : 0) objects, plus one more for each of 34,70,142,... that are <= N. -- Eric Sosman esosman(a)ieee-dot-org.invalid
From: Roedy Green on 22 Jan 2010 22:57 On Thu, 21 Jan 2010 11:33:21 -0800 (PST), Lew <lew(a)lewscanon.com> wrote, quoted or indirectly quoted someone who said : > >Why in the world would you assign to 'null' first, then discard that >initialization immediately? If you use IntelliJ, it quickly slaps your wrist for any unnecessary initialisation. -- Roedy Green Canadian Mind Products http://mindprod.com G�D is REAL untess declared INTEGER. ~ Anonymous In F�RTRAN, variables beginning I..N are implicitly INTEGER, the rest REAL.
From: Roedy Green on 22 Jan 2010 23:02 On Thu, 21 Jan 2010 11:33:21 -0800 (PST), Lew <lew(a)lewscanon.com> wrote, quoted or indirectly quoted someone who said : > >> =A0 =A0 =A0 =A0 =A0 =A0 in =3D new BufferedReader(new InputStreamReader(n= those A0s are s char( 160 ); Agent quietly converted them to ordinary spaces, but I gather's Lew's newsreader converter them to =A0 Newsreaders tend to be pretty flat footed when interpreting text. Best to keep your posts to printable ASCII. -- Roedy Green Canadian Mind Products http://mindprod.com G�D is REAL untess declared INTEGER. ~ Anonymous In F�RTRAN, variables beginning I..N are implicitly INTEGER, the rest REAL.
From: Lew on 22 Jan 2010 23:04 Lew wrote, quoted or indirectly quoted someone who said : >> Why in the world would you assign to 'null' first, then discard that >> initialization immediately? Roedy Green wrote: > If you use IntelliJ, it quickly slaps your wrist for any unnecessary > initialisation. Doesn't FindBugs also report that sort of thing? -- Lew
From: Lew on 22 Jan 2010 23:09
Lew wrote, quoted or indirectly quoted someone who said : >>> =A0 =A0 =A0 =A0 =A0 =A0 in =3D new BufferedReader(new InputStreamReader(n= Roedy Green wrote: > those A0s are s > char( 160 ); > > Agent quietly converted them to ordinary spaces, but I gather's Lew's > newsreader converter them to =A0 > > Newsreaders tend to be pretty flat footed when interpreting text. Best > to keep your posts to printable ASCII. Ummm, the post you seem to be quoting was written in printable ASCII. However, that part you quote was automatically inserted from the post by Mark that I was quoting. You misattributed it. That's what I get for using Google Groups when T-Bird isn't at hand. It seems that GG chose to insert s in the material I cited. Your suggestion to stick to ASCII, however, is off the mark. How should I enter the more outré characters? -- Lew |