Hotspots: Admin Pages | Turn-in Site |
Current Links: Cases Final Project Summer 2007
Discussion 4 - Robert Hutson
Java to Smalltalk guide
Most programmers learn a language like Java when they are introducted to an object-oriented paradigm. On the other hand, most programmers may have only heard of Smalltalk but aren't familiar with it in the slightest. I was one of these, so now I will share with you some things I've picked up on that could help a Java programmer transition easier.
First of all, one of the most important things that can be said on this matter is that in Smalltalk, EVERYTHING is an object, and there are no primitives, as in Java, such as int, long, and char. Just as in Java, you make these objects do things by defining methods. However, methods work differently in Smalltalk. In Smalltalk, messages are sent from objects to other objects, and then when received, it invokes the corresponding method. For example, to do basic addition, you send a + object. So when you say 9 + 5, what happens is the SmallInteger 9 receives a plus message with SmallInteger 5 as a parameter (in a sense). That is, 9 and 5 here are actual objects, not predefined constants or primitives as one might think. Also, in the Smalltalk environment, a virtual machine is executing code in the image, so everything that you open up within a Smalltalk environment is actual Smalltalk code being executed. In other words, all the windows, menus, pop-ups are all defined objects in Smalltalk that you can poke around with. There's an inspector tool that can be used to query state information about a particular object that's created. This can be helpful for debugging and a number of other things. One problem I've found with Smalltalk that is particularly hard to overcome is the fact that there is no documentation (or at least very little of it). All the code for Smalltalk (in Java terms, the API) is actually in the system, and you as a programmer are free to chnage things in it, and most of the coding you will do is within one of these 'system browsers'. A system browser lists all classes and their methods and attributes and even categorizes them in the system. The class categories can be thought of as packages in Java. Here is where you define classes and write the bulk of the code. Some code can be run from a workspace as well. Sometimes you may find this more convenient if you just want to test something really quickly without having to write a whole class for it. This can be thought of as a 'main method' is in Java. This is where you actually execute stuff. For example, you may write a class called ListSorter that contains several attributes and methods for sorting a generic list of objects. The code is there, but nothing will actually happen unless you invoke it right? So you open up a new workspace, and create a new instance of this class. Then you can call any methods on the new object from the workspace, and when you're done, highlight the portion you want to be executed, and select 'doit' from the world menu (or just Alt+D). In java, often times you want to print stuff to a console, when a gui seems unnecessary. In Smalltalk, this is almost equivalent to a Transcript. So System.out.println("something") in java is equivalent to Transcript show: 'something';cr. Let's walk through this short example. What is happening here? Transcript is a global variable ( a class basically...) that is receiving a 'show' message with the String 'something' as a parameter. Almost every Smalltalk command is of this form: : . The ';' in this example is a cascade. This can be used as a shorthand for sending several messages to the same object at once. Also, basically conditional statements, like if and else in java, still work in Smalltalk, but the syntax is different. Here,the receiving object would be a Boolean (such as: (x = 2)) that evaluates to true of false, the message would be something like ifTrue: or ifFalse:, and the parameter would be a block that gets executed upon passing the conditional. One simple thing I forget to mention thus far is assignment operators. In java, you say something = something_else, but in Smalltalk you use a ':='. Using '=' actually tests for equality in Smalltalk. On a final note, one thing I find kind of nifty about Smalltalk is that you can use a message like '+', '-', '', or '=' on other kinds of objects besides just numbers. For example, you can add two points together like this: (3@3)+(4@6), and the result would as expected be: (7@9). This will save you some coding if you can master doing this with objects, instead of in a more functional way that would be more permissible in Java. Well, that about wraps up some of the basics. Just remember these few things, and other things should come easier to you. I hope this was somewhat helpful.
Links to this Page