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
- Java has no class object for each class (Class has no subclasses)
- But it does have a meta object (basically the protocol of 'Class' itself)
- Although the java message could be a static function
- My Java method naming convention
- The 'this' allows you to get to a shadowed iv
- Smalltalk does not allow public access to another object's ivs
- In most Smalltalk's but see SmalltalkAgents for example
- No direct equivalence is ST, but many alternatives
- No message cascades in Java
- 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:
- Smalltalk does not have explicit interfaces, but implicit ones
by what messages an object/class responds to
- No non-object types in Smalltalk, so no int,long,boolean primitive
types
- Just ignore the return value in Smalltalk
- But sort of equivalent to x isKindOf: Bar or one of the other alternatives
- Smalltalk methods are public
- By convention this would be in a 'private' protocol
- Smalltalk does not have exposed instance variables
- '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.
- Not visible to subclasses
- 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.
- Note that c must be "final": the variable must only be allow to
reference one object in a given context execution
- 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.
|