From 03fa55988af966c0c9f63a7805fe6466eec3940d Mon Sep 17 00:00:00 2001 From: ted10014 <48134466+ResilientSpring@users.noreply.github.com> Date: Thu, 21 Jan 2021 18:28:27 +0800 Subject: [PATCH] Committed 2021/1/21 --- Chapter6/TypeConv2.java | 48 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Chapter6/TypeConv2.java diff --git a/Chapter6/TypeConv2.java b/Chapter6/TypeConv2.java new file mode 100644 index 0000000..1ad7c82 --- /dev/null +++ b/Chapter6/TypeConv2.java @@ -0,0 +1,48 @@ +/* + * In the context of method overload in Java, + * automatic type conversions apply only if there is no direct match between a parameter and an argument. + * + * Textbook page on 226 + * + * */ + +class Overload3{ + + void f(byte x) { + System.out.println("Inside f(byte): " + x); + } + + void f(int x) { + System.out.println("Inside f(int): " + x); + } + + void f(double x) { + System.out.println("Inside f(double): " + x); + } +} + +public class TypeConv2 { + + public static void main(String[] args) { + // TODO Auto-generated method stub + + Overload3 obOverload3 = new Overload3(); + + int i = 10; + double d = 10.1; + + byte b = 99; + short s = 10; + float f = 11.5F; + + obOverload3.f(i); // calls ob,f(int) + obOverload3.f(d); // calls ob.f(double) + + obOverload3.f(b); // now, no type conversion + + obOverload3.f(s); // calls ob.f(int) - type conversion + obOverload3.f(f); // calls ob.f(double) - type conversion + + } + +}