Motivation
Single-file programs -- where the whole program fits in a single source file -- are common in the early stages of learning Java, and when writing small utility programs. In this context, it is pure ceremony to have to compile the program before running it. In addition, a single-file program may declare multiple classes and thus compile to multiple class files, which adds packaging overhead to the simple goal of "run this program". It is desirable to be able to run the program directly from source with the java launcher:
Campbell Ritchie wrote:The array indices are calculated from h & c − 1, which for positive values of h gives the same result as h % c.
Swapnil Mishra wrote:this is what I am asking, can't we avoid this creation of a separate class?
Swapnil Mishra wrote:This above code is not truly multithreading isn't it?
Swapnil Mishra wrote:any other approach? without needing to create multiple classes?
Campbell Ritchie wrote:my memory might be wrong, but in the days when I tried timing, it took more than 100× as long to concatenate 100,000× than 10,000×. Maybe because you are overwhelming your heap space with char[]s (or byte[]s for all‑ASCII text), containing millions of elements, requiring repeated garbage collection runs.
Campbell Ritchie wrote:I remember being told that such code allows the JVM to choose its own optimisation. What Brian Goetz calls “dumb code” is much easier to optimise than if the programmer tries to be too clever.
Campbell Ritchie wrote:
Yes, it does make a difference. Repeated concatenation in a loop runs in worse than On² time complexity.Jan Hoppmann wrote:. . . But this doesn't make a difference since Java 8, does it?
Campbell Ritchie wrote:
At runtime, I think, but not certain.Jan Hoppmann wrote:At compile time, the + will be replaced by calls to a StringBuilder's append method. Or did I get something wrong here?
Tim Holloway wrote:Which is odd, because I'd expect that to have been scooped up into the internal <init> method. But a diffference that makes no difference is no distance.[/url]
Tim Holloway wrote:I wouldn't call the manifest constants in a class file a "pool". More like a list. That is it's a sequence both logical and physical of delimited values.
Tim Holloway wrote:I wouldn't obsess about formatting in samples posted here. we have people of all persuasions here, so there can be no "wrong" option.