RU beehive logo ITEC Department logo
ITEC 120
2007spring
ibarland,
jpittges

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

lect10a
public vs private; primitive Wrappers
lect10a

Wrapper classes for primitives

We have seen that java has the primitive types int, long, double, boolean, and char (along with the less-precise numbers: byte, short, float). For each1 of these primitive types, Java also provides a corresponding wrapper class: Integer2, Long, Double, Boolean, and Character, (along with Byte, Short, and Float).

For example, an instance of Double is just an object which contains one lonely field -- a field of type (you guessed it) double.

// Draw picture:
double d1;
d1 = 12.3;

Double dd1;
dd1 = new Double(12.3);
Double dd2 = new Double( 12.3 );
Double dd3 = new Double( d1 );
Double dd4 = dd1;

dd1.toString()
dd1.doubleValue()
dd1.intValue()

d1 = 45.6;
dd3.doubleValue(); // Could dd3's value change, from modifying d1?
                   // If you weren't sure, re-read last week's notes

dd3 = dd2;

dd1 == dd3         // true or false?  Try it out!  Your picture should explain.
dd1.equals(dd3);   // true or false?  Try it out!  Your picture should explain.

dd2 == dd3         // true or false?  Try it out!  Your picture should explain.
dd2.equals(dd3);   // true or false?  Try it out!  Your picture should explain.
You can glance over the Double docs, for more methods on Doubles.

Why do entire classes even exist, when we already have the primitive types, which work just fine? Good question. The reasons why these wrapper classes exist are:

Just to keep things from looking simple (even though they are), Java, as a favor, silently converts between primitive types and the wrapper classes:

dd1 = 3.4;      // "auto-boxing"   -- same as:  dd1 = new Double(3.4);
dd1 + d1        // "auto-unboxing" -- same as:  dd1.doubleValue() + d1
dd1 + dd2       // Does this mean auto-unboxing, or string-concatenation?!?!
dd1 == dd2      // Does this compare-objects, or unbox-and-then-compare-numbers?!?!

public vs. private

Let's look at the hw05 solution documentation.

We realize that some of the methods were simply helper functions for our own code. The method getMinutes (which returns a NumberDisplay) is not really meant for our clients to call. Similarly, wrap1_12 is our own internal function which is just a helper for getTime12. On the other hand, getTime12 and getTime24 and timeTick and setTime are what we want our clients to call.

We can make these assumptions clear: every field and method can be labeled with an adjective public or private:

What this means for us: From now on, every method and field should be declared either public or private.


1There is also class Void, which technically isn't a wrapper class because there aren't any void values to wrap!      

2Integer and Character are the two wrapper classes whose name isn't quite just the capitalized version of its corresponding primitive.      

3A minor lie: it really rewrites it as Double.toString(3.0) + " blind mice", but we haven't yet discussed static methods.
And actually, I'm not sure whether the java compiler is required (or allowed or disallowed) to optimize early, and convert the literal 3.0 into a String at compile time (perhaps even doing the concatenation right then, too?).      

4

In fact, you might wonder if there is some “very-private” modifier which means “The method/field can only be accessed by this object itself”. Java does not have such a concept.

While such a protection-level is a very common design goal, and it makes sense from a pure object-oriented mindset, Java's philosophy is that if a programmer wants to violate privacy expecatations between PizzaServer, and that programmer has access to the source code for class PizzaServer, then that programmer could always do so: they'd just change the modifier from “very-private” to merely private.

If you ever design your own language though, it's worth not taking this for granted. Many features in programs are there to help protect programmers from themselves. (Declaring types in advance is one such feature.)

     

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


©2007, Ian Barland, Radford University
Last modified 2007.Aug.27 (Mon)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme