Page 449 - Introduction to Programming with Java: A Problem Solving Approach
P. 449

                Stock Average Example
The StockAverage program in Figure 10.27 reads weighted stock values and stores them in an ArrayList. In simplified terms, a weighted stock value is the market price of one stock share times a number that scales that price up or down to reflect the importance of the stock’s company in the overall marketplace. After the StockAverage program stores the weighted stock values in an ArrayList, the program calculates the average of all the entered weighted stock values. Why is an ArrayList appropriate for calculating a stock average? An ArrayList’s size grows as necessary. That works well for stock averages because there are lots of stock averages (also called stock indexes), and they use different numbers of stocks in their calculations. For example, the Dow Jones Industrial Average uses stock values from 30 companies while the Russell 3000 Index uses stock values from 3,000 companies. Because it uses an ArrayList, the StockAv- erage program works well for both situations.
The StockAverage program stores stock values in an ArrayList named stocks. The stock values originate from user input in the form of doubles, like 25.6, 36.0, and so on. As you know, ArrayLists can’t store primitives; they can store references only. So the StockAverage program wraps up the doubles into Double wrapper objects just prior to storing them in the stocks ArrayList. As you might imag- ine, a wrapper object is an instance of a wrapper class, and each wrapper object stores one “wrapped up” primitive value. You don’t have to worry very much about wrapper objects for ArrayLists. For the most part, you can pretend that ArrayLists can hold primitives. Case in point: The following line from the StockAverage program appears to add a primitive (stock) to the stocks ArrayList:
stocks.add(stock);
What actually happens behind the scenes is that the stock primitive gets automatically converted to a Apago PDF Enhancer
wrapper object prior to its being added to the stocks ArrayList. Really, there is just one thing you have to worry about when working with primitives in an ArrayList. When you create an ArrayList object to hold primitive values, the type you specify in the angled brackets must be the wrapped version of the primitive type, that is, Double instead of double, Integer instead of int, and so on. This line from the StockAverage program illustrates what we’re talking about:
ArrayList<Double> stocks = new ArrayList<Double>();
Autoboxing and Unboxing
In most places, it’s legal to use primitive values and wrapper objects interchangeably. The way it works is that the JVM automatically wraps primitive values and unwraps wrapper objects when it’s appropriate to do so. For example, if the JVM sees an int value on the right of an assignment statement and an Integer variable at the left, it thinks to itself, hmmm, to make this work, I need to convert the int value to an Integer wrapper object. It then gets out its Styrofoam packing peanuts and duct tape and wraps up the int value into an Integer wrapper object. That process is called autoboxing. On the other hand, if the JVM sees an Integer wrapper object on the right of an assignment statement and an int variable at the left, it thinks to itself, hmmm, to make this work, I need to extract the int value from the Integer wrapper object. It then proceeds to tear off the Integer wrapper object’s covering, and it gets the int value that’s inside. That process is called unboxing.
More formally, autoboxing is the process of automatically wrapping a primitive value in an appropriate wrapper class whenever there’s an attempt to use a primitive value in a place that expects a reference. Refer to the stocks.add(stock); statement in Figure 10.27. That statement causes autoboxing to occur. The stocks.add method call expects a reference argument. Specifically, it expects the argument to be
10.12 Storing Primitives in an ArrayList 415
























































































   447   448   449   450   451