Signatures

Figure 219. Defining type signatures Slide presentation
boolean  startsWith​(String prefix, int toffset)  

Return type boolean

Arguments among with their respective types and order:

  1. Type String

  2. Type int


Figure 220. Type signature examples Slide presentation
Method Return type Argument type list
void print() void (void)
int add (int a, int b) int (int, int)
int max (int a, int b) int (int, int)
void print (int a, float b) void (int, float)
void display (float a, int b) void (float, int)

Figure 221. Defining method signatures Slide presentation
boolean startsWith(String prefix, int toffset)  

Method name startsWith.

Number of arguments among with their respective types and order.


Figure 222. Method signature examples Slide presentation
Method Method name Method signature
void print() print (void)
int add (int a, int b) add (int, int)
int max (int a, int b) max (int, int)
void print (int a, float b) print (int, float)
void display(float a, int b) display (float, int)

exercise No. 89

Method signature variants

Q:

Consider the following method definitions:

  1. boolean startsWith​(String value, int position)  
  2. boolean startsWith​(int toffset, String prefix)  
  3. boolean startswith​(String prefix, int toffset)  

Which of these have the same method signature as Figure 221, “Defining method signatures ”?

A:

We start by the original method:

boolean startsWith​(String prefix, int toffset)  

Its signature may be represented by:

{"startsWith", String, int​}

This describes the method's name and all its argument types among with their respective order. We now cover the different definitions:

Method Method signature
Original
boolean startsWith​(String prefix, int toffset)
("startsWith", String, int​)
1
boolean startsWith​(String value, int position)
("startsWith", String, int​)
Same method signature: Formal parameter names do not matter, only their types are important.
2
boolean startsWith​(int toffset, String prefix)
("startsWith", int, String​)
Different method signature: The order of types is being reversed here.
3
boolean startswith​(String prefix, int toffset)
("startswith", String, int​)
Different method signature: For Java identifiers being case sensitive method names startsWith and startswith differ and thus their resulting method signatures.