- When the data type of the parameter passed to the function represents a range that is smaller than the parameter type of the function parameters, follow these guidelines:
- Char type is special and translates directly to Int:char->int->long->float->double
- Other basic data types follow this rule: byte->short->int->long->float->double
- If the integer constant: int->long->float->double
- Floating-point constants: such as 0.5 do not explicitly indicate what type of constant is. Processed directly into a double type instead of float
- If the hope is a definite type, the last show is given, such as 0.5f,0.5f.0.5d,1l and so on.
- When the parameter type of the pass-through function represents a range greater than the parameter type of the function parameters, the narrowing conversion must be displayed
Package Basetype;public class Overloadtest {void F1 (byte x) {System.out.println ("F1 (Byte)");} void F1 (char x) {System.out.println ("F1 (char)");} void F1 (short x) {System.out.println ("F1 (short)");} void F1 (int x) {System.out.println ("F1 (int)");} void F1 (float x) {System.out.println ("F1 (float)");} void F1 (Double x) {System.out.println ("F1 (Double)");} void F1 (long x) {System.out.println ("F1 (Long)");} void F2 (Byte x) {System.out.println ("F2 (Byte)");} void F2 (short x) {System.out.println ("F2 (short)");} void F2 (int x) {System.out.println ("F2 (int)");} void F2 (float x) {System.out.println ("F2 (float)");} void F2 (Double x) {System.out.println ("F2 (Double)");} void F2 (long x) {System.out.println ("F2 (Long)");} void F3 (short x) {System.out.println ("F3 (short)");} void F3 (int x) {System.out.println ("F3 (int)");} void F3 (float x) {System.out.println ("F3 (float)");} void F3 (Double x) {System.out.println ("F3 (Double)");} void F3 (Long x) {System.out.println ("F3 (Long)");} void f4 (int x) {System.out.println ("F4 (int)");} voidF4 (float x) {System.out.println ("F4 (float)");} void F4 (Double x) {System.out.println ("F4 (Double)");} void F4 (long x) {System.out.println ("F4 (Long)");} void F5 (float x) {System.out.println ("F5 (float)");} void F5 (Double x) {System.out.println ("F5 (Double)");} void F5 (long x) {System.out.println ("F5 (Long)");} void F6 (float x) {System.out.println ("F6 (float)");} void f6 (Double x) {System.out.println ("F6 (Double)");} void F7 (Double x) {System.out.println ("F6 (Double)"); }void F8 (char x) {System.out.println ("F8 (char)");} void F8 (Byte x) {System.out.println ("F8 (byte)");} void F8 (int x) {System.out.println ("F8 (int)");} void F8 (float x) {System.out.println ("F8 (float)");} void F8 (Double x) {System.out.println ("F8 (double)");} void F8 (long x) {System.out.println ("F8 (Long)");} void F9 (float x) {System.out.println ("F9 float");} void Testconstval () {System.out.println ("5:"); F1 (5); F2 (5); F3 (5); F4 (5); F5 (5); F6 (5); F7 (5); System.out.println ();} void Testchar () {char x = ' x '; System.out.println ("char x:"); F1 (x); F2(x); F3 (x); F4 (x); F5 (x); F6 (x); F7 (x); System.out.println ();} void Testbyte () {byte x = 0; System.out.println ("byte x:"); F1 (x); F2 (x); F3 (x); F4 (x); F5 (x); F6 (x); F7 (x); System.out.println ();} void Testshort () {short x = 0; System.out.println ("Short x:"); F1 (x); F2 (x); F3 (x); F4 (x); F5 (x); F6 (x); F7 (x); System.out.println ();} void Testint () {int x = 0; SYSTEM.OUT.PRINTLN ("int x:"); F1 (x); F2 (x); F3 (x); F4 (x); F5 (x); F6 (x); F7 (x); System.out.println ();} void Testlong () {long x = 0; System.out.println ("long x:"); F1 (x); F2 (x); F3 (x); F4 (x); F5 (x); F6 (x); F7 (x); System.out.println ();} void TestFloat () {float x = 0; System.out.println ("float x:"); F1 (x); F2 (x); F3 (x); F4 (x); F5 (x); F6 (x); F7 (x); System.out.println ();} void Testdouble () {double x = 0; System.out.println ("Double x:"); F1 (x); F2 (x); F3 (x); F4 (x); F5 (x); F6 (x); F7 (x); System.out.println ();} public static void Main (String args[]) {overloadtest t = new overloadtest (); T.testconstval (); T.testbyte (); T.testchar (); T.testshort (); t.tEstint (); T.testlong (); T.testfloat (); t.testdouble ();d ouble d = 0.9d; T.f9 (d); It is wrong to write this t.f9 ((float) d); Must be shown to be narrowing conversions}}
The code is from "Thingking in Java".
"Thinking in Java" notes
Automatic type conversions for basic types in Java overloads