Summary
Here is a brief summary of what must be done to convert a Grace program to Java:
- You must put a semicolon at the end of lines in Java. However, never
put a semicolon before a { or after a }. Proper indenting is optional (but required
by your instructors). Use the Format command under the Source
menu in Eclipse.
- Parameterless method declarations and requests must include () even though
there are no parameters. This is how Java tells the difference between a method and an
instance variable.
- Every Java class must be in a separate file.
- Every instance variable, constant, and method must have a visibility
annotation. For now, use one of private and protected. Do not
put these in front of local variables.
- Types are written before the identifiers in declarations of variables
and methods. E.g., int x = 5; and public void onMouseClick(Location point).
- Rather than a single Number type, Java has many. The type int
is a primitive type holding integers, while double refers to numbers with decimal
points. An int can be easily used as a double, but to convert a double
to an int you must use a type cast: (int)myDouble or Math.round(myDouble,
depending on whether you want to truncate the value or round it.
- The Java types int, double, and boolean are primitive types,
which mean that you may not make method requests of them. Object types should
always be written with initial letter capitalized (e.g., FramedRect).
- Java is statically typed, so every identifier introduced must be given a type: either a class or an
interface. Interfaces are like Grace types, with no implementation information.
- Assignments to variables are written with = instead of :=.
- Constants are written in all caps and are declared as static final if they are
the same for all objects of the class. If they depend on parameters to the class or method,
just declare them as final.
- Uninitialized instance variables are given the value 0 (if a primitive type) or
null (if an object type). If you forget to initialize an instance variable you will get
a "null pointer error" when you attempt to access it. Local variables are not automatically initialized. You must
make sure to initialize them.
- In Java we use keyword this in place of self. If this
is the receiver of a method request you may leave out this. If a method
parameter and an instance variable of the same class have the same name, placing
a "this." before the variable makes it refer to the instance variable. A common
idiom is using the same name for a constructor parameter and instance variable and then
using this in front of the instance variable when initializing it.
class C {
...
private String strVal;
...
public C(String strVal) {
this.strVal = strVal; // right side is the parameter,
// left side is instance variable
}
...
}
- You may overload methods and class constructors in Java (i.e., have more than one
constructor or method with the same name). To be legal you must be able to distinguish
between them with the number and types of parameters.
- There are minor differences in the names of methods in the Java objectdraw
library compared to Grace. Keep the objectdraw API documentation open
at
http://www.cs.pomona.edu/classes/cs051/handouts/objectdraw-api.html.