Chapter 3
Identifiers and Interfaces
3.1 Names of Entities
- Programs are made of computational entities.
- To be accessible, each entity is provided a description called an interface
- The entities are provided names, called identifiers, by their interfaces.
- It is customary to have class names which start with a capital letters: Math, String
- Package names might be quantified, using dots: java.lang, java.lang.ref, java.math
3.2 Paths to Identifiers
- Identifiers of members of packages may be pre-quantified by the names of the packages. Similarly, names of members
of classes may be pre-quantified by the names of the classes.
| Math | java.lang.Math |
| Object | java.lang.Object |
| PI | java.lang.Math.PI, Math.PI |
| abs | java.lang.Math.abs |
| String | java.lang.String.String |
- Path specifications have the benefit of showing where the referenced entities come from, and help resolve
ambiguities.
3.3 Fields
The interface of a field includes
- A name
- A type specification, preceding the name
- Optional modifiers, before the specification of the type
| Field Summary |
| static | double | E | Math |
| static | double | PI | Math |
| modifiers | type | name | |
- The name or the type may be followed by an array indicator [].
3.4 Methods
The interface of a method includes
- A name
- A list of parameters in parentheses, following the name
- A return type, preceding the name
- The return type may be preceded by access modifiers: public, private, protected.
| Method Summary |
| static | double | random | () | Math |
| static | double | sqrt | (double a) | Math |
| String | concat | (String str) | String |
| String | substring | (int beginIndex) | String |
| String | substring | (int beginIndex, int endIndex) | String |
| public static | void | main | (String [] args) | |
| modifiers | return type | name | parameters | |
Consecutive parameters are separated by a comma.
3.5 Constructors
The interface of a constructor includes
- A name, identical to its class name
- A list of parameters in parentheses, following the name
- Optional access modifiers, before the name
| Constructor Summary |
| String | () | String |
| String | (char[] value) | String |
| modifiers | name | parameters | |
3.6 Signatures
The signature, of a method or a constructor, is a sub-portion of the interface consisting of
- The name
- The number of parameters
- The type of parameters
| Signatures |
| substring (int beginIndex) | String |
| substring(int beginIndex, int endIndex) | String |
| String () | String |
| String(String original) | String |
| String (char[] value) | String |
3.7 Types
Types may be
- Primitives: int, long, short, byte, boolean, double, float, char
- Class names
- Arrays of the above
- No type: void (a possible substitute for a return type of a method)
| static double random() | Math |
| String substring (int beginIndex) | String |
| static void exit(int status) | System |
| String (char[] value) | String |
| static double PI | Math |
3.8 Modifiers
abstract, final, interface, native, private, protected, public, static, strict, synchronized, transient, volatile.
3.9 Instance and Class Features
Classes have two types of features:
- class features. They carry the ‘static’ modifier.
- instance features. They don’t carry the ‘static’ modifier.
3.10 Parameters
Each parameter consists of
- A name
- A type, preceding the name
- Optional brackets [] after the type or the name
In some contexts, the names of parameters are of no interest.
3.11 Assignment #3: Interfaces
Due: Mon, Apr 7, at class
Consider the following list of interfaces.
- A a
- B a()
- a()
- a(int [] b)
- a(int b)
- int a
- int a()
- static double a(int [] b)
- static int a
- static int a(int b)
Answer the following questions for the above given list of interfaces.
- Which entries can stand for a field.
- Which entries can stand for a method? What are their signatures?
- Which entries can stand for a constructor? What are their signatures?
- What are the primitive data types in use.
- What are the class data types in use.
- Which of the features are class features? instance features?
Please submit your answers on paper.
Q&A