Drag2ShirtsTopTypes and Type-checking in Java

Types and Type-checking in Java

Java is a statically typed programming language. This means that when identifiers are introduced they must be declared with an associated type and the compiler will check over the program to make sure the type declarations are consistent with the code. Thus it will catch at compile time if you try to associate a string with an identifier whose type is declared to be int.

When you used the dialect rtobjectdraw in Grace, it complained if you did not associate a type with each identifier introduced. However it did not check for consistency at compile time. It eventually will, but that has not yet been implemented. That will hopefully be in place in the next few months.

You will find this early detection of errors will help you locate and fix these errors much faster. Java has both a much faster compiler and will report multiple errors each time you compile your program.

Java differs from Grace in that class names can be used as types. Thus if Line is a class, we can declare a variable to have type Line:

    private Line diagonal = new Line(pt1,pt2,canvas)

Java also supports interfaces. Interfaces are like Grace's types. They include the specification of public methods (and constants), but no implementation information. We will see them used in examples later.


Drag2ShirtsTopTypes and Type-checking in Java