ChiMu  
 
Menu Edge About   Products   Services   Projects   Publications  
  Publications                 

Java and Smalltalk syntax compared

This is a table comparing elements of Java syntax to Smalltalk syntax [This originated from a request by Doug Edmunds <dae@paclink.com>]. An entry of <null> and <nil> mean the feature does not exist in the corresponding language, although there will frequently be alternatives that are similar. If a column is blank it means that the other column entry goes with the line above (usually as an example alternative with close semantics).

Java syntax is 1.1. I picked Smalltalk-80 syntax (some other current Smalltalk's have some features ST-80 doesn't have). Also, this is not meant to be exaustive of either language, but it has enough to get a feel of each (although I think it is easier if you see a translated piece of code). Some of the following use my own naming convention and libraries.

Java Smalltalk  

This and That
null nil  
this self  
super super  
<null> self class [1]
this.getClass() [2]

Basic Statement Structure
; .  
/*Comment*/ "Comment"  
//Comment <nil>  

Message Sends
anObject.foo() anObject foo  
foo() self foo [3]
anObject.foo(a,b) anObject foo: a with: b  
aDict.atKey_put(key,value) aDict atKey: key put:value [4]
<null> anObject a; b [9]

Instance Variables
x x  
this.x x [5]
anObject.x <nil> [6]
     

Control Structures
if (isTrue) {…} (isTrue) ifTrue: […]  
if (...) {…} else {…} (...) ifTrue: […] ifFalse: […]  
switch (...) <nil> [7]
while (isTrue) [] whileTrue: []  
do {} while ()    
for (...;...;...) <nil> [8]
aCollection do:, collect:, etc.
anInterval do:
(number) timesRepeat: []
try {} catch (Exception e) {} Exception handle: [:e | ...] do: [...]  

Declaring Classes and Interfaces
class Foo { Object subclass: #Foo [10]
class Bar extends Foo { Bar subclass: #Foo  
interface Foo { <nil> [11]
interface Foo extends Foo { <nil>  
class Bar implements Foo { <nil>  

Typing
Object anObject; | anObject |  
String aString; <nil>  
Object aString; | aString |  
int anInt; <nil> [12]
Object foo() foo  
String foo() <nil>  
void foo() <nil>  
foo [13]

Casts
(Bar) x <nil> [14]
x isKindOf: Bar
x respondsTo: #messageForABar
(...DoesNotUnderstand...) handle: [:e | ...] do: [x messageForABar]

Access Control
public ... foo() foo [15]
protected foo() <nil> [16]
public x <nil> [17]
protected x x [18]
private x <nil> [19]

Blocks and Anonymous Classes
new Function2Arg() {public Object valueWith_with (Object a, Object b) { return (String) a + (String) b; }} [:a :b | a,b] [20]
<null> [ | ^'This breaks out of everything']  
new Function2Arg() {public Object valueWith_with (Object a, Object b) { return c + (String) a + (String) b; }} [:a :b | c,a,b] [21]

Miscellaneous
"\"" '"'  
"'" ''''  
"Hi\tthe\u0032re\n" <nil> [22]
"hi/there" withCrs

Notes

  1. Java has no class object for each class (Class has no subclasses)
  2. But it does have a meta object (basically the protocol of 'Class' itself)
  3. Although the java message could be a static function
  4. My Java method naming convention
  5. The 'this' allows you to get to a shadowed iv
  6. Smalltalk does not allow public access to another object's ivs
  7. In most Smalltalk's but see SmalltalkAgents for example
  8. No direct equivalence is ST, but many alternatives
  9. No message cascades in Java
  10. Pseudo Smalltalk for the rest of the declaring section (to avoid long message sends). Also see Allen Wirfs-Brock's OOPSLA'96 presentation on declarative Smalltalk:
  11. Smalltalk does not have explicit interfaces, but implicit ones by what messages an object/class responds to
  12. No non-object types in Smalltalk, so no int,long,boolean primitive types
  13. Just ignore the return value in Smalltalk
  14. But sort of equivalent to x isKindOf: Bar or one of the other alternatives
  15. Smalltalk methods are public
  16. By convention this would be in a 'private' protocol
  17. Smalltalk does not have exposed instance variables
  18. 'protected' is approximately equivalent to standard Smalltalk private. Approximately because in Java 'protected' allows objects of the same class to access an object's protected areas. Java is encapsulated by Class, Smalltalk is encapsulated by object.
  19. Not visible to subclasses
  20. Ignoring the typing differences. Note that I also have a Java preprocessor that converts:
        [String a, String b | return a + b;]
    
    into the highly noisy
        new Function2Arg() {public Object valueWith_with(Object a, Object b) {
            return (String) a + (String) b
        }};
    
    so there is hope for Java having something close to (and actually in many ways better than) blocks.
  21. Note that c must be "final": the variable must only be allow to reference one object in a given context execution
  22. Some because of unicode, some are available as characters but not in literal strings

I didn't include the static or package stuff in this table. Partially I didn't get to it, and partially it would all map to <nil> and <null> between Smalltalk and Java. Java's static methods are not even close to a Class object's methods (static methods aren't even methods, they are functions or procedures bound at compile time) and there is no real reason to map between the two. Most Smalltalk's don't have any modular structure for the name space, so there is no equivalent to a Java package.

What is Java?

Java is a lightly-statically-typed, simple version of Smalltalk with the syntax of the 'C' family.
 
Publications