Skip to main content

Method signature

In Java, a method signature is part of the method declaration. It's the combination of the method name and the parameter list. The reason for the emphasis on just the method name and parameter list is because of overloading. It's the ability to write methods that have the same name but accept different parameters. 

In another word -:

In Java, method signature consist of method names followed by arguments types. return type is not part of method signature in Java.

 E.g.-:

  public static int m(int x, float y){
 
 }

Compiler will use method signature to resolve methods calls.

class Test{
    public static void m1(int x, float y){
    }
    public static int  m2(String str){
    }
    public static int m3(double d){
    }
  public static void main(String... args){
      Test t=new Test();
       t.m1(10,13.4f);
       t.m2("Hello World !");
       t.m3(13.4);
       t.m4(); //compile time error : cannot find symbol : method m4() location in Test
  }
}
                class Test

        m1(int x, float y);

        m2(String str);

        m3(double d);

                method Table

Within a class to method with same signature, not allowed.
E.g.-:

class Test{
  public static void m(int x){
  
  }
  public static int m(int y){
    return y;
  }
public static void main(String... args){
     Test t=new Test();
      t.m(10);
}
}
//compile time error: m(int) already define in class Test

Basic description of overloading-:

Method overloading allows different methods to have the same name, but different signatures, where the signature can differ by the number of input parameters or type of input parameters, or a mixture of both.

Two method said to be overloaded if and only if both methods having same name but different argument types. 

    In C language-:
                               In the c language, method overloading concept is not available, hence we can't declare multiple methods with the same name but different argument types. If there is a change in argument type compulsory we should go for new method name which increases the complexity of programming.

E.g.-:
    abs(int);
    labs(long);
    fabs(float);


  In Java-:
                But in Java we can declare multiple method with same name but different argument types. Such type of methods are called overloaded methods. Having overloading concept in Java reduces complexity of programming. 


E.g.-:
    abs(int);
    abs(float);
    abs(long);


* In overloading method resolution always takes care by compiler based on reference type hence overloading is also consider as compile time polymorphism or static polymorphism or early binding. 

class Test{
  public static void m(int x){
      System.out.print(x);
  }
  public static void m(float y){
    System.out.print(y);
  }
 public static void m(String str){
     System.out.print(str);
  }
  public static void m(double d){
    System.out.print(d);
  }
public static void main(String... args){
     Test t=new Test();
      t.m(10);                                            //output is 10
      t.m(12.3f);                                        //output is 12.3
      t.m(12.3);                                        //output is 12.3
      t.m("Hello World!");                    // Hello World!
}
}


Popular posts from this blog

Java

Codes With Java — Basics Codes With Java Java tutorials & fundamentals About Contact Privacy Basic Fundamentals Java source file structure Import Statement Static Import Packages Data Type Variables Final Variable Declaration and Access Modifier Inner classes applicable modifiers Static Modifier Synchronized Native Transient Volatile Interface Introduction Interface Declaration and Implementation Interface methods and variables Naming Conflicts Interface Marker interface and Ad...

Short Circuite Operators part 4

                                                             Short Circuit Operators In  Java logical operators , if the evaluation of a logical expression exits in between before complete evaluation, then it is known as  Short-circuit . A short circuit happens because the result is clear even before the complete evaluation of the expression, and the result is returned. Short circuit evaluation avoids unnecessary work and leads to efficient processing. 1-: AND(&&) 2-:OR(||) these are exactly same as bitwise operators (&,|) except the following differences. Single Short Circuit Operator(&,|) Both arguments Should be evaluated always. relatively performance is low. Applicable for both boolean and Integral types. Double Short Circuit Operator(...

Final Variable

Final Instance Variable If the value of a variable changes from object to object, such a variable is called an instance variable . For every object, a separate copy of the instance variable will be created. Instance variables do not require explicit initialization; JVM always provides default values. Example: class Test { int x; // instance variable public static void main(String... args) { Test t = new Test(); System.out.println(t.x); // output: 0 } } If the instance variable is declared as final , then explicit initialization is mandatory. JVM will NOT provide default values. Example: class Test { final int x; } Compile-time Error: variable x might not have been initialized. Rule: A final instance variable must be initialized before constructor completion . Possible places for initialization: 1. At the time of declaration class Test { final int x = 10; } 2. Inside an instance blo...