J2SE5.0 : Should I teach beginners about primitives anymore?
Sat, 3 Jul 2004 09:27 PM GMT

I really like autoboxing and what it does, and I am also a teacher. I am trying to decide if I should even teach primitives anymore. I have been doing some testing on J2SE 5.0 and it seems for my beginning java class I don't have to even cover primitives anymore. But would I be cheating the students if I gave them the examples below without covering primitives.

    Boolean b = false;
    if (!b) System.out.printf("It's false!");
   
    Integer d = 2;
    Integer r = 65;
    System.out.println(d + r);
   
    Integer h = 322 << 2;
    System.out.println(h);
   
    Short w = 44;
    Short x = 77;
    Short y = w + x; // Obviously wrong

    Character zz = 'c';
    Character lk = '\u0055';
    System.out.println(lk);

I am going to be pondering this for quite a while. Anybody out in blogland have any opinions?

12 comments for J2SE5.0 : Should I teach beginners about primitives anymore?
Kevin Burton
Sun, 4 Jul 2004 02:26 AM GMT

Under what timeframe will they be employed . Most organizations won't have 1.5 available for a few years so they will essentially be forced to use primitives for a long time. Also, I haven't benchmarked by my gut is that primitives will be much faster.
Giulio Piancastelli
Sun, 4 Jul 2004 02:48 PM GMT

As a teacher, I think you should also consider a more philosophical point of view: the very nature of the Java language (not a completely object-oriented language by design) and its historical reasons (inspiration taken by the C++ approach, and all of its consequences). Primitive types in Java cannot be magically hidden everywhere because of J2SE 5.0. In fact, your students will surely face some API where a method returns an int, or one of its argument is a float, so you'd better let they know of the existence of primitive types, even from a practical (i.e. not philosophical or historical) point of view. On the other hand, you could encourage your students to have a more polished approach within the language, suggesting not to use primitive types, and to use methods instead of operators wherever it's possible. Let me quote an example which appeared on David Flanagan's blog some time ago.
Integer i = new Integer(2);
Integer j = new Integer(2);
assertTrue(i >= j); // success
assertTrue(i <= j); // success
assertTrue(i == j); // failure!
The last assertion fails because, being i and j objects, the == operator checks for identity, not equality. If i and j are int, or they are assigned a literal number instead of an object, the last assertion succeeds. But, as far as objects are concerned, you should use the equals method to check:
assertTrue(i.equals(j)); // success, at last
Given that, the one and only type with some kind of operator overload is String. A little unfair, since the programmer can't do operator overloading herself, but, oh, you can avoid concatenation there, too, with a more object-oriented (in the very Java sense) approach:
String name = "Jack"
StringBuffer s = new StringBuffer("Hello ").append(name).append(", how are you?");
System.out.println(s.toString());
Your students should also keep in mind how Java still isn't a completely object-oriented language, even with those autoboxing marvels. For such a language, they would have to pick elsewhere... Ruby, for example.
Dan Hinojosa
Sun, 4 Jul 2004 11:45 PM GMT

Very good post Giulio. I didn't quite think about the (i==j) situation. I guess when the time does come that my class will be taught in 5.0, I will still need to cover the differences in primitives and their corresponding wrappers.
Richard
Sun, 4 Jul 2004 11:53 PM GMT

The other thing to consider is if they'd ever want to use C/C++ as well (I know - shocking concept isn't it :) )
Chris Perrin
Tue, 6 Jul 2004 09:17 PM GMT

It all depends on the context of the class. If it's a vocation let's learn to use Java, then you could have an argument (perhaps not a good one) for not teaching primitives. If you are trying to develop well-rounded software engineers, I would go ahead and teach them primitivies. Then again, I am a "purist" and by purist, I seem to mean stuck in the past ;)
Doug
Tue, 6 Jul 2004 10:57 PM GMT

Giulio: Your example is not a good example, because in that particular case "i==j" is true!. The specification for autoboxing includes this:

"If the value p being boxed is true, false, a byte, an ASCII character, or an integer or short number between -128 and 127, then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2."

This was not implemented in the first Beta of Java 1.5, but it is in the second Beta.

Daniel: Yes, of course you still need to teach primitives, because you cannot perform arithmetic operations on wrapper classes. And an "if" statement still requires a boolean and not a Boolean, and a "case" statement still requires an integer type and not an Object. As soon as you get beyond "println()", you quickly need primitives.

Stefan Zobel
Wed, 7 Jul 2004 12:05 AM GMT

As a practicing programmer I'm quite shocked:

Have you guys ever conceived of a situation where your students might have to do some real programming work in their future career?

Primitives where build into Java quite purposefully (performance). I read an interview with James Gosling last year, where he defended that design decision, and, in effect, said that he would do it again that way.

I remember myself having a teacher (quite some time ago now) who told us that everything should be an object and we should we able to say something like "5.add(2)" (as you could already do in .NET or in most Smalltalk dialects).

That's a fine, respectable and very clean standpoint - but a completely impractical one for most commercial SW development (even nowadays). Primitives will be with us for quite some time to come. And your students will be better of if they know about them.

Yust my 2 cents,
Stefan
Suchak
Wed, 7 Jul 2004 12:56 PM GMT

Giulio
Nope it still does not work in beta 2.

Here is the code

Integer i = new Integer(1);
Integer J = new Integer(1);
if(i==J){
System.out.println("Yess !!!!!!!");
}else{
System.out.println("No !!!!!!!");
}


Here is the result
C:\JDK1.5test\src>java -version
java version "1.5.0-beta2" Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta2-b51) Java HotSpot(TM) Client VM (build 1.5.0-beta2-b51, mixed mode, sharing)
C:\JDK1.5test\src>javac -source 1.5 Test.java
Note: Test.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.
C:\JDK1.5test\src>java Test
Hello
No !!!!!!!
Doug
Wed, 7 Jul 2004 05:04 PM GMT

Giulio and Suchak: My mistake. I didn't look closely at the code in Giulio's original comment. Since the topic of the article had been about autoboxing, I made the (incorrect) assumption that it was illustrating autoboxing.

Using "new Integer()" does indeed give distinct objects; that's a fundamental requirement of "new". Autoboxing or valueOf() will use shared objects for small values.

Maybe next time I'll read more closely before commenting :-)

Giulio Piancastelli
Thu, 8 Jul 2004 02:27 AM GMT

Doug, my example does illustrate autoboxing, just from a different point of view: in fact, you couldn't use operators like '>=' on Integer objects without autoboxing. (I'm talking about that particular feature of autoboxing which is auto-unboxing, of course.) Indeed, you fans of the so-called "documented behaviour" should have a look at the first three chapters in The Hitchhiker's Guide to the Galaxy by Douglas Adams. ;)

Stefan: try Ruby for a taste of a completely object-oriented language. It lets you write the following, where you don't even need a weird string substitute for a symbolic operator.

5 + 2 # returns 7
5.+(2) # returns 7, again

I like the approach. On the very same note, I like also the following Java approach towards String literals.

String name = "Jack";
String s = "Hello " + name + ", how are you?";

Which, since before autoboxing was the only operator overloading example contained in the Java language, pretty much works on the same principle; you just can't directly call the method behind the '+' operator.

On the other hand, performance could be an issue, as you asserted. What about the following cycle, picked up in a comment to another post on the same autoboxing topic?

for (Integer i = 0; i < 100000; i++)
    // do something...

I've read elsewhere that in his Effective Java, Joshua Block advised people to use StringBuffer's capabilities to concatenate strings because of poor performance of concatenation of string literals. But that was in 2001. Anyway, performance issues concerning autoboxing are definetely something to check out.

So, another approach to the problem of autoboxing potential confusion, an approach completely different from the one I was advocating in my previous comment, could be to use primitive types everywhere, and let autoboxing deal with code using collections:

int one = 1;
ArrayList list = new ArrayList();
list.add(one + 1); // automatically converted to an Integer object
int two = list.get(0); // automatically converted to an int

which, by the way, I think was the main reason behind the introduction of autoboxing in the Java language. After all, as I have already said, Java is still not a completely object-oriented language despite autoboxing magic, so why should we pretend that it is?

Giulio Piancastelli
Thu, 8 Jul 2004 02:42 AM GMT

I like also the following Java approach towards String literals.
String name = "Jack";
String s = "Hello " + name + ", how are you?";
Which, since before autoboxing was the only operator overloading example contained in the Java language, pretty much works on the same principle; you just can't directly call the method behind the '+' operator.

Oh, well, you can. It is called concat:

String s = "Hello".concat(name).concat(" how are you?");

Sorry, my mistake.

Anonymous
Thu, 30 Sep 2004 07:32 PM GMT

Perhaps your students never write quicksort when they gradulate, but do they need to know quicksort (hash,queue,stack,...)? If your student never need to use primitives when they gradulate, do they need to know primitives? Perhaps, I may ask, who are your students?