Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Teaching without primitives  RSS feed

 
Ariel Ortiz
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java 5.0, the auto-boxing and auto-unboxing facilities allow you to have code like this:

Specically, I'm not using any int primitive variables. In teaching Java to students who are learning the language for the first time, I think it's much simpler not having to explain things like the duality in the Java type system, the usage of wrappers and primitive types, etc. Yet, in some of the Java 5.0 literature, several authors discourage programming as in the above example.

As teachers, do any of you have any comments in this regard?

...Ariel
 
James Chegwidden
Author
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ariel,

I would mention auto-boxing but not until later.
Numbers are not objects (thus name primative)- to me this is a hold over from C, C++, and not to scare students. You would have to show students the concept of an object first- and I am not an "early objects proponent".

Now, in C# numbers are objects- they are basically references to objects . Java may eventually follow the C# route here.

JC
 
James Chegwidden
Author
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ariel,

What level will you be teaching Java?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ariel --

To offer an opposing viewpoint:

C++ education has really changed in the last five years. These days, newbies don't see "char *" or bare arrays until well into the course. Instead, they work with basic_string<char> and vector<>, and only once they've become quite knowledgeable do they learn how these things are implemented. Doing things this way leads to the students becoming productive faster, and helps them concentrate on concepts rather than piddly details.

The autoboxing code you're showing is a similar approach to teaching Java. I like it, and I predict that we'll be seeing more and more of that as time goes on.
 
Ariel Ortiz
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What level will you be teaching Java?


I've been teaching advanced undergraduate courses using Java since 1997. By advanced I mean things like: Compiler Construction, Web Component Development, Mobile Application Development, and Software Design and Architecture. But for the first time I'm going to teach Java to freshmen starting next August. We are required to start using Alice ( www.alice.org ) for the first half of the semester, and then move to "real" Java programming for the remainder of the course. We will probably use BlueJ for the second half.

...Ariel
 
Ariel Ortiz
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now, in C# numbers are objects- they are basically references to objects. Java may eventually follow the C# route here.


I'm a bit confused here. C# has the concept of "value types" as well as "reference types". Thus it has a type duality just like Java. Actually, I first saw the concept of auto-boxing and auto-unboxing in C#.

The C# programmer's reference says that:
Boxing and unboxing enable value types to be treated as objects. Value types, including both struct types and built-in types, such as int, can be converted to and from the type object.

Further more:
An unboxing operation consists of first checking that the object instance is a boxed value of the given value-type, and then copying the value out of the instance.

Referring to the imaginary boxing class described in the previous section, an unboxing conversion of an object box to a value-type T consists of executing the expression ((T_Box)box).value. Thus, the statements

object box = 123;
int i = (int)box;
conceptually correspond to

object box = new int_Box(123);
int i = ((int_Box)box).value;

For an unboxing conversion to a given value-type to succeed at run-time, the value of the source operand must be a reference to an object that was previously created by boxing a value of that value-type.


As far as I understand, the way Java 5.0 works is not significantly different from C# in this regard. I know that the CLR actually has some bytecodes that perform these operations probably more efficiently then the JVM, but at least from the programmer's perspective it is fairly similar.

Now if we want to think of numbers as real objects, well we can always consider Eiffel, Smalltalk or Ruby. Yet I'll stick to Java because that's what my University mandates.

...Ariel
 
Ariel Ortiz
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The autoboxing code you're showing is a similar approach to teaching Java. I like it, and I predict that we'll be seeing more and more of that as time goes on.


Thanks Ernest, it's good to know that other people agree with this approach.

...Ariel
 
James Chegwidden
Author
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I was trying to say about C# was that simple types like-int is actually an alias for the System.Int32 type in the .net CTS. Thus, each of the intrinsic or simple data types in C# are an alias- for a class in the System namespace.

This is not how primatives behave in Java- What are the equivalent alias for Java primatives. I would not consider Wrapper classes that.

From C# Language Specification:
Each of the predefined types is shorthand for a system-provided type. For example, the keyword int refers to the struct System.Int32. As a matter of style, use of the keyword is favored over use of the complete system type name.

Now, as for auto boxing- I am not saying it is wrong. It is not commonly procticed and used intro to Java programming course- when dealing with simple data types.

What you choose to do is up to you. I met a Java instructor that insisted to teach GUI Java programming from the beginning. He was so frustrated that there were no books that teach that approach. I told him that is because is not a very common one- I never said he was wrong.

What text are you going to use for the course? I think this is important in an intro course as well.

James C.
 
Ariel Ortiz
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I was trying to say about C# was that simple types like-int is actually an alias for the System.Int32 type in the .net CTS. Thus, each of the intrinsic or simple data types in C# are an alias- for a class in the System namespace.

Mr. C, I apologize for misinterpreting your original statement.

This is not how primatives behave in Java- What are the equivalent alias for Java primatives. I would not consider Wrapper classes that.

I agree.

Now, as for auto boxing- I am not saying it is wrong. It is not commonly procticed and used intro to Java programming course- when dealing with simple data types.

I am aware that this is not a common practice. Java 5.0 was released in September 2004, and there are not many early adopters, especially not in beginner courses. But I believe this simplifies considerably the amount of things a student has to learn at the very start.

What text are you going to use for the course? I think this is important in an intro course as well.

The first half of the course uses Alice, so we're using "Learning to Program with Alice" by Wanda Dann. We are still considering the text for the second half (Java specific) of the course. I'm not aware of any book that uses the auto-boxing/unboxing approach I suggest at the begining of this thread for simple types like ints.

...Ariel
 
Ariel Ortiz
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kathy, as instructor and author of great books like HFJ, your opinion in this regard would be most enlightening.

...Ariel
 
James Chegwidden
Author
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first half of the course uses Alice, so we're using "Learning to Program with Alice" by Wanda Dann. We are still considering the text for the second half (Java specific) of the course. I'm not aware of any book that uses the auto-boxing/unboxing approach I suggest at the begining of this thread for simple types like ints.[/qoute]

Yep, your right,

Liang, Deitel, Savitch, Horstmann, Gaddis, Koffman and there new 5.0 books do have auto boxing but not the way you want to do it.

You may have to supplement the material. Hmm. sounds like a good publishing oppportunity.

JC
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!