View this PageEdit this PageAttachments to this PageHistory of this PageHomeRecent ChangesSearch the SwikiHelp Guide
Hotspots: Admin Pages | Turn-in Site |
Current Links: Cases Final Project Summer 2007

Discussion 4 - Shaun Duncan

The one thing that I really wanted was a Java -> Smalltalk guide for people like me, who like structure and deifinitions. That's why I've designed this. I wanted to be able to look at something like a Java textbook for Squeak.

Variables

So if were a seasoned Java developer, I might already know statements like:
int x = 7;
double y = 5;
char c = 'c';
boolean tf = false;
String s = "I am a String";
When working in Squeak, you have to remember that everything is an object. What does this mean? Well this means that there are no type declarations. And why should there be? Everything is an object, therefore there exact types are determined at runtime, and therefore pushes the responsibility of code using incorrect objects to the VM. So what do the previous variable declarations look like in Squeak? Well, let's get rid of the type modifier, first of all:
x = 7;
y = 5;
c = 'c';
tf = false;
s = "I am a String";
Now, all we have to do is correct a few language-specifics of squeak. These include assignment operators (= in java, := in squeak), string indicators ("" in java, '' in squeak), and single line terminators (what says "that's all the code on this line"... ; in java, . in squeak). Now we're at:
x := 7.
y := 5.
c := 'c'.
tf := false.
s := 'I am a String'.
And there we go!. Now, the key thing to remember is that this can be typed and executed from anywhere. All one has to do is type CMD+d or CMD+p to do it or print it out, respectively.

Speaking of printing out, how do we "print" something in squeak? In java it was just System.out.println("STRING")? Well in squeak, there's no access to a standard output stream. All printing must be done internally. How does one do that? Well, the object in squeak that takes the place of the System.out.println item is essentially the Transcript. To print, or debug, a certain value, invoke the following:
Transcript show: 'You can print here',aVar,'...commas concatenate strings here'.

Writing Classes

So any java programmer has written a class in java, there's no way around it. Chnaces are, it probably looked something like this:
public class Foo {
private int x = 7;
private String name = "foobar";

public Foo(int x, String name) {
this.x = x;
this.name = name;
}

public String getName() {
return name;
}

public void setInt(int x) {
this.x = x;
}
}

Link to this Page