Home Segments Top Top Previous Next

120: Mainline

Java also allows you to define multiple methods with the same name in the same class, as long as each version has a different arrangement of parameter data types. Each arrangement of return and parameter data types is called a method signature.

You can, for example, define one displayMovieRating method that handles integers, and another displayMovieRating method that handles floating-point numbers. Then, you can put both methods to work in the same program:

public class Demonstrate { 
 public static void main (String argv[]) { 
  int intScript = 6, intActing = 9, intDirection = 8; 
  double doubleScript = 6.0, doubleActing = 9.0, doubleDirection = 8.0; 
  displayMovieRating(intScript, intActing, intDirection); 
  displayMovieRating(doubleScript, doubleActing, doubleDirection); 
 } 
 // First, define displayMovieRating with integers: 
 public static void displayMovieRating (int s, int a, int d) { 
  System.out.print("The integer rating of the movie is ");  
  System.out.println(s + a + d);  
  return; 
 }  
 // Next, define displayMovieRating with floating-point numbers: 
 public static void displayMovieRating (double s, double a, double d) { 
  System.out.print("The floating-point rating of the movie is ");  
  System.out.println(s + a + d);  
  return; 
 }  
} 
--- Result --- 
The integer rating of the movie is 23 
The floating-point rating of the movie is 23.0