Overload of the main type

Source: Internet
Author: User

The

Primary (data) type can automatically be converted from a "smaller" type to a "larger" type. This can cause a bit of confusion when it comes to overloading. The following example shows what happens when a main type is passed to an overloaded method:

 

: Primitiveoverloading.java//promotion of primitives and overloading public class Primitiveoverloading {//Boolea 
  N can ' t be automatically converted static void Prt (String s) {System.out.println (s);
  } void F1 (char x) {PRT ("F1 (char)");}
  void F1 (Byte x) {PRT ("F1 (Byte)");}
  void F1 (short x) {prt (' F1 (short) ');}
  void F1 (int x) {prt ("F1 (int)");}
  void F1 (long x) {PRT ("F1 (Long)");}
  void F1 (float x) {prt ("F1 (float)");}

  void F1 (Double x) {PRT ("F1 (Double)");}
  void F2 (Byte x) {PRT ("F2 (Byte)");}
  void F2 (short x) {prt (' F2 (short) ');}
  void F2 (int x) {prt ("F2 (int)");}
  void F2 (long x) {PRT ("F2 (Long)");}
  void F2 (float x) {prt ("F2 (float)");}

  void F2 (Double x) {PRT ("F2 (Double)");}
  void F3 (short x) {prt (' F3 (short) ');}
  void F3 (int x) {prt ("F3 (int)");}
  void F3 (Long x) {PRT ("F3 (Long)");}
  void F3 (float x) {prt ("F3 (float)");}

  void F3 (Double x) {PRT ("F3 (Double)");}
  void f4 (int x) {prt ("F4 (int)");} void F4 (long x) { PRT ("F4 (Long)");
  } void F4 (float x) {prt ("F4 (float)");}

  void F4 (Double x) {PRT ("F4 (Double)");}
  void F5 (long x) {prt ("F5 (Long)");}
  void F5 (float x) {prt ("F5 (float)");}

  void F5 (Double x) {prt ("F5 (Double)");}
  void F6 (float x) {prt ("F6 (float)");}

  void f6 (Double x) {PRT ("F6 (Double)");}

  void F7 (Double x) {PRT ("F7 (double)");}
    void Testconstval () {PRT ("Testing with 5");
  F1 (5); F2 (5); F3 (5); F4 (5); F5 (5); F6 (5); F7 (5);
    } void Testchar () {char x = ' x ';
    PRT ("char argument:");
  F1 (x); F2 (x); F3 (x); F4 (x); F5 (x); F6 (x); F7 (x);
    } void Testbyte () {byte x = 0;
    PRT ("Byte argument:");
  F1 (x); F2 (x); F3 (x); F4 (x); F5 (x); F6 (x); F7 (x);
    } void Testshort () {short x = 0;
    PRT ("Short argument:");
  F1 (x); F2 (x); F3 (x); F4 (x); F5 (x); F6 (x); F7 (x);
    } void Testint () {int x = 0;
    PRT ("int argument:");
  F1 (x); F2 (x); F3 (x); F4 (x); F5 (x); F6 (x); F7 (x);
    } void Testlong () {long x = 0;
    PRT ("Long argument:"); F1 (x); F2 (x); F3 (x); F4 (x); F5 (x); F6 (x); F7 (x);
    } void TestFloat () {float x = 0;
    PRT ("float argument:");
  F1 (x); F2 (x); F3 (x); F4 (x); F5 (x); F6 (x); F7 (x);
    } void Testdouble () {double x = 0;
    PRT ("Double argument:");
  F1 (x); F2 (x); F3 (x); F4 (x); F5 (x); F6 (x); F7 (x);
    public static void Main (string[] args) {primitiveoverloading p = new primitiveoverloading ();
    P.testconstval ();
    P.testchar ();
    P.testbyte ();
    P.testshort ();
    P.testint ();
    P.testlong ();
    P.testfloat ();
  P.testdouble (); }
} ///:~


If you look at the output of this program, you will find that the constant value 5 is treated as an int value. So if you can use an overloaded method, you can get the int value it uses. In all other cases, if our data type is "less than" the argument used in the method, the data type will be "transformed". The effect of char is slightly different because it does not find an exact char match, and it transitions to int.
What happens if our arguments are greater than the independent variables expected by the overload method? A modification of the aforementioned program reveals the answer:

 

: Demotion.java//demotion of primitives and overloading public class demotion {static void prt (String s) {s 
  Ystem.out.println (s);
  } void F1 (char x) {PRT ("F1 (char)");}
  void F1 (Byte x) {PRT ("F1 (Byte)");}
  void F1 (short x) {prt (' F1 (short) ');}
  void F1 (int x) {prt ("F1 (int)");}
  void F1 (long x) {PRT ("F1 (Long)");}
  void F1 (float x) {prt ("F1 (float)");}

  void F1 (Double x) {PRT ("F1 (Double)");}
  void F2 (char x) {PRT ("F2 (char)");}
  void F2 (Byte x) {PRT ("F2 (Byte)");}
  void F2 (short x) {prt (' F2 (short) ');}
  void F2 (int x) {prt ("F2 (int)");}
  void F2 (long x) {PRT ("F2 (Long)");}

  void F2 (float x) {prt ("F2 (float)");}
  void F3 (char x) {PRT ("F3 (char)");}
  void F3 (Byte x) {PRT ("F3 (Byte)");}
  void F3 (short x) {prt (' F3 (short) ');}
  void F3 (int x) {prt ("F3 (int)");}

  void F3 (Long x) {PRT ("F3 (Long)");}
  void F4 (char x) {PRT ("F4 (char)");}
  void F4 (Byte x) {PRT ("F4 (Byte)");}
  void F4 (short x) {prt (' F4 (short) ');} void f4 (int x) { PRT ("F4 (int)");
  } void F5 (char x) {prt ("F5 (char)");}
  void F5 (Byte x) {prt ("F5 (Byte)");}

  void F5 (short x) {prt (' F5 (short) ');}
  void F6 (char x) {PRT ("F6 (char)");}

  void F6 (Byte x) {PRT ("F6 (Byte)");}

  void F7 (char x) {PRT ("F7 (char)");}
    void Testdouble () {double x = 0;
    PRT ("Double argument:");
    F1 (x); F2 ((float) x); F3 ((long) x); F4 ((int) x);
  F5 ((short) x); F6 ((byte) x); F7 ((char) x);
    public static void Main (string[] args) {demotion p = new demotion ();
  P.testdouble (); }
} ///:~

Here, the method employs a smaller, narrower range of main type values. If our argument range is wider than it is, we must use the type name in parentheses to convert it to the appropriate type. If you do not do so, the compiler will report an error.
You can note that this is a "narrowing conversion". In other words, some information may be lost in the modelling or transformation process. That's why the compiler forces us to define clearly--we need to express our desire to change.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.