Signatures

Figure 245. Defining type signatures Slide presentation Create comment in forum
boolean  startsWith​(String prefix, int toffset)  

Return type boolean

Arguments among with their respective types and order:

  1. Type String

  2. Type int


Figure 246. Type signature examples Slide presentation Create comment in forum
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 247. Defining method signatures Slide presentation Create comment in forum
boolean startsWith(String prefix, int toffset)  

Method name startsWith.

Number of arguments among with their respective types and order.


Figure 248. Method signature examples Slide presentation Create comment in forum
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. 85

Method signature variants Create comment in forum

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 247, “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", String, int​)
Different method signature: The order of types is being reversed here.
3
boolean startswith​(String prefix, int toffset)
("startswith", int​, String)
Different method signature: For Java identifiers being case sensitive method names startsWith and startswith differ and thus their resulting method signatures.