Домой United States USA — software Java library evolution and other puzzlers

Java library evolution and other puzzlers

469
0
ПОДЕЛИТЬСЯ

Exploring puzzling code to figure out why it doesn’t do what seems obvious is one way to improve your programming skills. Puzzle yourself today!
Exploring puzzling code to figure out why it doesn’t do what seems obvious is one way to improve your programming skills. In this post, I introduce you to various Java-oriented puzzlers from Jens Dietrich, Joshua Bloch and Neal Gafter, and myself.
According to the survey, «Many programmers use build tools with compilation and regression testing at the end of each build cycle. This means that programs are always compiled and tested against the libraries they use at runtime. However, there are some technologies such as OSGi that support partial library upgrades without recompilation. These puzzlers describe situations developers may encounter when they switch between these two modes of deployment.»
There are two versions of the survey: a short version and a long version.
This puzzler asks the following pair of questions:
For the first question, you respond by choosing one of the following options:
For the second question, you respond by choosing one of the following options:
If you would like the answer to this puzzler, check out the Java Puzzlers sampler .
The Integer class maintains an internal cache of unique Integer objects over a small range of values. The low bound of this range is -128 and the high bound defaults to 127. For value 127, a reference to an Integer object containing this value is returned from the cache and assigned to i1 and i2. Hence, the == operator, which compares object references, compares the same reference in i1 and i2, and so true outputs. However, for 30000, a pair of Integer objects containing this value are created and their different references are assigned to i1 and i2. This time, == compares two different references and false outputs.
You can change the cache’s high bound by assigning a different value to system property java.lang. Integer. IntegerCache.high, and you would do so when you run the application. For example, assume that the bytecode equivalent of the previous code fragment was stored in IntegerCompare.class. Execute the following command, which states that all Integer objects containing values ranging from -128 through 30000 are to be cached, so that you observe true instead of false in the second comparison of i1 and i2:
Next time, I launch a three-part series that answers various questions related to the java.lang. Object class and its various methods. In Part 1, I overview Object and examine its clone () and equals () methods.
The following software was used to develop the post’s code:
The post’s code was tested on the following platform (s) :
Point your browser to the BOOKS section of my website to learn more about this Apress book.

Continue reading...