home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in Java / c04 / PrimitiveOverloading.java < prev    next >
Encoding:
Java Source  |  2000-05-25  |  4.8 KB  |  143 lines

  1. //: PrimitiveOverloading.java
  2. //////////////////////////////////////////////////
  3. // Copyright (c) Bruce Eckel, 1998
  4. // Source code file from the book "Thinking in Java"
  5. // All rights reserved EXCEPT as allowed by the
  6. // following statements: You can freely use this file
  7. // for your own work (personal or commercial),
  8. // including modifications and distribution in
  9. // executable form only. Permission is granted to use
  10. // this file in classroom situations, including its
  11. // use in presentation materials, as long as the book
  12. // "Thinking in Java" is cited as the source. 
  13. // Except in classroom situations, you cannot copy
  14. // and distribute this code; instead, the sole
  15. // distribution point is http://www.BruceEckel.com 
  16. // (and official mirror sites) where it is
  17. // freely available. You cannot remove this
  18. // copyright and notice. You cannot distribute
  19. // modified versions of the source code in this
  20. // package. You cannot use this file in printed
  21. // media without the express permission of the
  22. // author. Bruce Eckel makes no representation about
  23. // the suitability of this software for any purpose.
  24. // It is provided "as is" without express or implied
  25. // warranty of any kind, including any implied
  26. // warranty of merchantability, fitness for a
  27. // particular purpose or non-infringement. The entire
  28. // risk as to the quality and performance of the
  29. // software is with you. Bruce Eckel and the
  30. // publisher shall not be liable for any damages
  31. // suffered by you or any third party as a result of
  32. // using or distributing software. In no event will
  33. // Bruce Eckel or the publisher be liable for any
  34. // lost revenue, profit, or data, or for direct,
  35. // indirect, special, consequential, incidental, or
  36. // punitive damages, however caused and regardless of
  37. // the theory of liability, arising out of the use of
  38. // or inability to use software, even if Bruce Eckel
  39. // and the publisher have been advised of the
  40. // possibility of such damages. Should the software
  41. // prove defective, you assume the cost of all
  42. // necessary servicing, repair, or correction. If you
  43. // think you've found an error, please email all
  44. // modified files with clearly commented changes to:
  45. // Bruce@EckelObjects.com. (Please use the same
  46. // address for non-code errors found in the book.)
  47. /////////////////////////////////////////////////
  48.  
  49. // Promotion of primitives and overloading
  50.  
  51. public class PrimitiveOverloading {
  52.   // boolean can't be automatically converted
  53.   static void prt(String s) { 
  54.     System.out.println(s); 
  55.   }
  56.  
  57.   void f1(char x) { prt("f1(char)"); }
  58.   void f1(byte x) { prt("f1(byte)"); }
  59.   void f1(short x) { prt("f1(short)"); }
  60.   void f1(int x) { prt("f1(int)"); }
  61.   void f1(long x) { prt("f1(long)"); }
  62.   void f1(float x) { prt("f1(float)"); }
  63.   void f1(double x) { prt("f1(double)"); }
  64.  
  65.   void f2(byte x) { prt("f2(byte)"); }
  66.   void f2(short x) { prt("f2(short)"); }
  67.   void f2(int x) { prt("f2(int)"); }
  68.   void f2(long x) { prt("f2(long)"); }
  69.   void f2(float x) { prt("f2(float)"); }
  70.   void f2(double x) { prt("f2(double)"); }
  71.  
  72.   void f3(short x) { prt("f3(short)"); }
  73.   void f3(int x) { prt("f3(int)"); }
  74.   void f3(long x) { prt("f3(long)"); }
  75.   void f3(float x) { prt("f3(float)"); }
  76.   void f3(double x) { prt("f3(double)"); }
  77.  
  78.   void f4(int x) { prt("f4(int)"); }
  79.   void f4(long x) { prt("f4(long)"); }
  80.   void f4(float x) { prt("f4(float)"); }
  81.   void f4(double x) { prt("f4(double)"); }
  82.  
  83.   void f5(long x) { prt("f5(long)"); }
  84.   void f5(float x) { prt("f5(float)"); }
  85.   void f5(double x) { prt("f5(double)"); }
  86.  
  87.   void f6(float x) { prt("f6(float)"); }
  88.   void f6(double x) { prt("f6(double)"); }
  89.  
  90.   void f7(double x) { prt("f7(double)"); }
  91.  
  92.   void testConstVal() {
  93.     prt("Testing with 5");
  94.     f1(5);f2(5);f3(5);f4(5);f5(5);f6(5);f7(5);
  95.   }
  96.   void testChar() {
  97.     char x = 'x';
  98.     prt("char argument:");
  99.     f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
  100.   }
  101.   void testByte() {
  102.     byte x = 0;
  103.     prt("byte argument:");
  104.     f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
  105.   }
  106.   void testShort() {
  107.     short x = 0;
  108.     prt("short argument:");
  109.     f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
  110.   }
  111.   void testInt() {
  112.     int x = 0;
  113.     prt("int argument:");
  114.     f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
  115.   }
  116.   void testLong() {
  117.     long x = 0;
  118.     prt("long argument:");
  119.     f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
  120.   }
  121.   void testFloat() {
  122.     float x = 0;
  123.     prt("float argument:");
  124.     f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
  125.   }
  126.   void testDouble() {
  127.     double x = 0;
  128.     prt("double argument:");
  129.     f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
  130.   }
  131.   public static void main(String[] args) {
  132.     PrimitiveOverloading p = 
  133.       new PrimitiveOverloading();
  134.     p.testConstVal();
  135.     p.testChar();
  136.     p.testByte();
  137.     p.testShort();
  138.     p.testInt();
  139.     p.testLong();
  140.     p.testFloat();
  141.     p.testDouble();
  142.   }
  143. } ///:~