Closed gbaswath closed 2 years ago
To write a version of the random method without flaws, we have to know a fair amount about pseudorandom number generators, number theory, and two’s complement arithmetic. Luckily, there exists Random.nextInt(int)
. By using a standard library, you take advantage of the knowledge of the experts who wrote it and the experience of those who used it before us.
Ref - 37e086d
As of Java 7, we should no longer use Random
. For most uses, the random number generator of choice is now ThreadLocalRandom
. It produces higher quality random numbers, and it’s very fast. On my machine, it is 3.6 times faster than Random. For fork join pools and parallel streams, use SplittableRandom
.
Ref - 09df54d
To print the contents of a URL specified on the command line (which is roughly what the Linux curl
command does). Prior to Java-9
, this code was a bit tedious, but in Java 9 the transferTo()
method was added to InputStream
.
The libraries are too big to study all the documentation [Java9-api], but every programmer should be familiar with the basics of java.lang
, java.util
, and java.io
, and their subpackages. Several libraries bear special mention. The collections framework and the streams library (Items 45–48) should be part of every programmer’s basic toolkit, as should parts of the concurrency utilities in java.util.concurrent
. This package contains both high-level utilities to simplify the task of multithreaded programming and low-level primitives to allow experts to write their own higher-level concurrent abstractions.
Ref - 954d30d
If we need to do something that seems like it should be reasonably common, there may already be a facility in the libraries that does what we want. If there is, use it; if we don’t know, check. Generally speaking, library code is likely to be better than code that we’d write ourself and is likely to improve over time.
This is no reflection on your abilities as a programmer. Economies of scale dictate that library code receives far more attention than most developers could afford to devote to the same functionality.
Don’t reinvent the wheel.
Highlights