How difficult will making the transition from Pascal to Java be?

Status
Not open for further replies.
At my high school, they teach all the advanced concepts (dynamics, recursion, trees) in Pascal, before allowing you to move on to the higher classes. They do Java and C++, Java at the beginning and end of the year, and C++ in the middle.

Anyway, next week I'm finishing the last Pascal class, and am going to transition to Java.

So I'm wondering, how much of a shock am I in for?

(Also, for taking this class, I'll be able to get Freshman-level CS credit from the local community college.)
 

warmachine

Ars Tribunus Militum
2,479
Subscriptor
As an experienced Delphi (Object Pascal) programmer, I've made the transition from Pascal to Java. Both are imperative languages and concepts like while loops, parameter passing and recursion occur in both. There are a few annoying differences such as no enumerated types, function pointers or pass by reference in Java.

You may have trouble with the switch from procedural design to object-oriented design. It's a change of perspective, though this is more of a problem of experienced programmers.

Other than that, I can't see a problem. You'll probably find it easier as Java has some useful collection classes and better multi-threading support.
 

sroylance

Ars Tribunus Militum
2,156
Originally posted by warmachine:
There are a few annoying differences such as no enumerated types, function pointers or pass by reference in Java.
<honest non-sarcastic questions...>
Why would you need function pointers in such a heavily OOP language?
I thought everything in Java, except for maybe int and char, are already references, so all function calls using object parameters are already pass by ref?

I did Pascal in HS (12 years ago), as well a tiny bit of java more recently. Java was really easy to learn and pascal gives you a really good fundamental graspe of programming, so I don't think you'll have any trouble.
 

warmachine

Ars Tribunus Militum
2,479
Subscriptor
Sroylance wrote:
Why would you need function pointers in such a heavily OOP language?

I'll be more explicit. Member function pointers, which are in Object Pascal, are convenient for callbacks or event notifications. Instead, for Java, it is common to create a custom, inner class just to handle the it. A member function pointer would be less work and IIRC, also appears in C#.

I thought everything in Java, except for maybe int and char, are already references, so all function calls using object parameters are already pass by ref?

Yes, you're right. Primitive types, such as int, float or boolean, or passed by value whereas objects are passed by reference. Never post at 2am. :( Still, I prefer to pass data as primitive types rather than objects for simple data and passing these by reference would be handy.
 

sroylance

Ars Tribunus Militum
2,156
Let's see if I can get the tags right this time...

I'll be more explicit. Member function pointers, which are in Object Pascal, are convenient for callbacks or event notifications. Instead, for Java, it is common to create a custom, inner class just to handle the it. A member function pointer would be less work and IIRC, also appears in C#.
gotcha

Yes, you're right. Primitive types, such as int, float or boolean, or passed by value whereas objects are passed by reference. Never post at 2am. :( Still, I prefer to pass data as primitive types rather than objects for simple data and passing these by reference would be handy.
OK.

Thanks for the explanation. I haven't done java really, and I was curious.

-Steve
 
Why would you need function pointers in such a heavily OOP language?


For the same reason that they're desirable in C++ (or classes with an <tt>operator()</tt> overload (i.e. functors), which the language treats almost identically, and again which Java has no direct equivalent to). A common use of this in C++ is for predicates -- a function returning true or false, to determine if some action should be performed (for instance, <tt>std::list::remove_if()</tt> takes a predicate that's used to determine if an element gets removed from the list or not). The <tt>remove_if()</tt> function takes either a function pointer or a functor (because the syntax for calling through a function pointer is the same as invoking a class's <tt>operator()</tt> the language lets you use them fairly interchangably).




Java can't do either of these things -- it has no function pointers and it has no function-like objects. It could partially redeem itself if it had some kind of IPredicate, with a single Test method taking an Object; instead of a functor class, one would write a class that implements IPredicate, that did something appropriate to the parameter it was passed. It wouldn't be perfect, but it would work. Alas, there's no such thing, and the designers of the Collections API deliberately left it out; instead, they say that people wanting such functionality should built it atop iterators, which is a rather lame cop-out. The point is to express a high-level idea -- "remove elements if this condition is satisfied" -- not to have to iterate through the list manually, removing the elements by hand.




And then there's callbacks. Yes, inner classes are used for many of the same things, but callbacks are often a neater way to do this -- this is reflected, IMO, by the callback interface of javax.security.auth. Callbacks are an effective and familiar way of achieving certain tasks, and again, function pointers and/or functors are the way to do them.



I thought everything in Java, except for maybe int and char, are already references, so all function calls using object parameters are already pass by ref?


No, it doesn't. It has pass-by-value, and pass-by-value alone.




The confusion lies in objects; object variables are created on the heap (with <tt>new</tt>) and the actual variables are pointers. These pointers are also passed by value; in many places, the existence of the pointers is hidden, which gives the illusion of pass-by-reference, but it is not pass-by-reference.


Demonstration of this(for appropriate classes with appropriate methods and constructors):


Code:
public void foo(Bar b)
{
	b = new Bar("quux");
}
Bar bar = new Bar("baz");
bar.print();
foo(bar);
bar.print();
The output will be:
Code:
baz
baz


The languages passes the pointer by value to the function. The function modifies that passed-in copy of the pointer, not the actual pointer itself.




Notice how it works identically to pass-by-pointer in C++:
Code:
void foo(Bar* b)
{
	b = new Bar("quux");
	// ignore the leak
}
Bar* bar = new Bar("baz");
bar->print();
foo(bar);
bar->print();
The output will be:
Code:
baz
baz


because on calling the function the pointer gets copied.




Contrast this with C++'s pass-by-reference:


Code:
void foo(Bar*& b)
{
	b = new Bar("quux");
}
Bar bar = new Bar("baz");
bar->print();
foo(bar);
bar->print();
The output will be:
Code:
baz
quux


This is proper pass-by-reference. Java can't do it. It only ever passes by pointer. It hides those pointers, yes, so you can't do pointer arithmetic or bad casts on them, but they are pointers, not references, which is why they behave the way they behave. The variables that you create are pointers to objects in Java's garbage-collected heap, rather than synonyms for the objects themselves.




This pass-by-value nature is made explicit here:


When the method or constructor is invoked, the values of the actual argument expressions initialize newly created parameter variables, each of the declared Type, before execution of the body of the method or constructor.


(emphasis mine). New pointer variables are created. Now, because they still point at the same objects, calling methods on them will indeed modify the object they're pointing at -- hence the mistaken assumption that it's pass-by-reference. But an assignment will fail to have any side-effects, because it clobbers the *copy* of the pointer, and no more.

 

MikeApp

Ars Centurion
257
Subscriptor
The point is to express a high-level idea -- "remove elements if this condition is satisfied" -- not to have to iterate through the list manually, removing the elements by hand.

I am a little unclear why implementing an iterating comparison is so burdensome. You can easily subclass a collection and implement remove_if() that takes an anonymous class as an argument. Controlling the process of iteration also allows the comparison class to do more sophisticated work (see Josuttis *C++ Standard Library* 8.1.4).
 
Originally posted by MikeApp:
I am a little unclear why implementing an iterating comparison is so burdensome.
I didn't say it was burdensome. I said that it wasn't the idea I wanted to express.

You can easily subclass a collection and implement remove_if() that takes an anonymous class as an argument.
Such guarantees can't generally be made of a language that permits 'final' classes and methods. Whether I can do such a thing is entirely dependent on the whims of the class's creator. It may be true in specific cases, but it isn't true in general.

Controlling the process of iteration also allows the comparison class to do more sophisticated work (see Josuttis *C++ Standard Library* 8.1.4).
What does it permit that a predicate class cannot do? Whilst predicates perhaps ought not contain any state, the specification currently permits this, and it's arguably desirable to permit such a thing.
 
Originally posted by MikeApp:
Josuttis, *The C++ Standard Library*, (1999 Addison-Wesley Longman)
This is pertaining to what?

The Nth example (I don't have the book, but I am aware of Issue #92 of the C++ Standard Library's list of active issues, which was brought up by Nico Josuttis, and which pertains to the copying of predicates, and how the behaviour isn't standardized, which can lead to surprising results, and which uses as an example a predicate that removes the Nth element)?

In any case, this poses no problem, so really, what's the point you're attempting to make? What "more sophisticated work" can be done manually iterating through a collection that can't be done with a predicate functor?
 

MikeApp

Ars Centurion
257
Subscriptor
Given the C++ specification, removing repeated instances, Josuttis' "nth" case, or anything else that relies on object state would have to be considered carefully.

I'm not arguing against functors, I'm simply saying that
#1 the C++ spec isn't perfect and
#2 you can roll your own in Java in many (most? all?) cases


If a class is final and doesn't implement a method you'd like (of any variety), then you are SOL. Write the author and complain :) For code you are writing yourself, add a method! For the collections, subclass. If you really need this with a final collection of some variety, write class with a static remove_if(coll, comparison) that will do the job for you. Again, not very elegant, but not much code and it will work.
 
Originally posted by MikeApp:
Given the C++ specification, removing repeated instances, Josuttis' "nth" case, or anything else that relies on object state would have to be considered carefully.
I suspect the C++ specification will in time be tightened, to make the various algorithms significantly less useful.

Personally, I would sooner have the standard algorithms altered so that once they started using a predicate for predicating they guaranteed that they wouldn't subsequently copy it. It's this, IMO, that is the underlying problem in Josuttis' Nth case (the function makes a copy of the object, uses it, and then makes another copy of the unused object, and uses that). If copies were only made before <tt>operator()</tt> was called, everything would be fine (assuming the objects are copyable, of course).

I'm not arguing against functors, I'm simply saying that
#1 the C++ spec isn't perfect and
#2 you can roll your own in Java in many (most? all?) cases
You might be able to write something mostly equivalent (e.g. iterating through yourself), but you can't roll your own function objects (because Java doesn't let you treat an object like a function).

If a class is final and doesn't implement a method you'd like (of any variety), then you are SOL. Write the author and complain :)
Indeed -- my point was that in a language where you can do things as irritating as declaring things to be <tt>final</tt>, you can't resort to saying "if you want more functionality, subclass". I've not had the value of <tt>final</tt> classes explained to me; it seems they're a stupid idea.

For code you are writing yourself, add a method! For the collections, subclass. If you really need this with a final collection of some variety, write class with a static remove_if(coll, comparison) that will do the job for you. Again, not very elegant, but not much code and it will work.
Boo. I would sooner use the standard C++ library (and C++, of course) than work around the IMO idiotic decisions that Sun made when designing the Java collections library.
 

newton_meter

Ars Scholae Palatinae
1,424
Wait a cotton-picking minute. C++ programmers use the word "functor"? Give me a break, do we need more overloaded notation?

1954 Category theory uses functor to mean a structure preserving map between categories. Categories are sort of like directed graphs with some extra properties. Think: functor = type constructor. (Think: type constructor = template class used to define a new type).

1984 Standard ML uses functor to mean a structure parameterized over another structure. A structure is a collection of functions, types, exceptions, and values. Functors are (first-order) functions from structures to structures. The SML module language (including functors) gives a way to "program the linker" by specifying how to wire modules together. Think: functor = template class when used solely for encapsulation and modularity.

199? C++ begins to use functor for a class with an overloaded operator(). WTF, isn't that more like a closure than a functor (of either sort)?

The second usage seems derived from (a misunderstanding of) the first; and the third from (a misunderstanding of) the second, perhaps? Cut it out, and I mean now!.

Category theorists, you must use the term "category morphism". ML programmers, you must use the term "parameterized structure". C++ programmers, you must use the term "function object".

Thank you for your time.

As for the poe-tae-toe/poe-tah-toe question, I think it comes down to what you want to make easy. Some languages have an "exception" system that should only be used in exceptional conditions, since it is rather ad hoc and inefficient. Some languages have a "condition" system that has identical semantics with an efficient implementation.

Personally, I like first-classedness and find it dreary to approach problems obliquely because the language requires it. Consider that methods really are first class in Java, if I want to go to the rigmarole of obtaining a java.lang.reflect.Method object, passing it around, and calling invoke(Object, Object[]) on it. The constant up and down-casting of objects makes my head spin; and the exception handling pollutes my program. I do not like it.

kg m²/s²
 
Status
Not open for further replies.