Advertisement
Guest User

Untitled

a guest
Oct 10th, 2015
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. package invariant;
  2.  
  3. /**
  4. * Static Typing and Java
  5. *
  6. * Here are some of the purported POSITIVES of STATIC TYPING:
  7. * <UL>
  8. * <LI>PREVENT and DETECT ERRORS (checked at compile type) (i.e. CORRECTNESS)
  9. * <LI>easier to REASON about the PROGRAM / METHOD
  10. * <LI>easier to DEBUG
  11. * <LI>better DOCUMENTATION
  12. * <LI>easier COMPILER OPTIMIZATIONS (e.g. replacing virtual calls by direct calls when the exact type of the receiver is known statically)
  13. * <LI>better RUNTIME EFFICIENCY
  14. * </UL>
  15. *
  16. * ...there are ALSO some NEGATIVES to STATIC TYPING
  17. * <UL>
  18. * <LI>Too RIGID (as compared to dynamic typing)
  19. * <LI>LESS REUSEABLE CODE (leads to Code Duplication )
  20. * <LI>Too HARD TO ADAPT to changing /dynamic behavior
  21. * </UL>
  22. *
  23. * This Gist illustrates one ACTUAL use case and describes how the code
  24. * attempts to navigate the positives and negatives of static typing in
  25. * Java's type system.
  26. *
  27. * There are some inconsistencies and sometimes Java makes ASSUMPTIONS
  28. * ABOUT PROGRAMMER INTENT in an attempt to allow the reuse of code to either
  29. * POSITIVE or NEGATIVE EFFECT. Other times Java requires the code be EXPLICIT
  30. * to VERIFY
  31. *
  32. * @author M. Eric DeFazio eric@typefra.me
  33. */
  34. public class StaticTypeChecking_2
  35. {
  36. enum ANumber { ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE };
  37.  
  38. public static void main (String[] args)
  39. {
  40. squareRoot( 9L ); // 1) type checker verifies that 9L is valid
  41. // for the squareRoot() method "function domain"
  42. // prints 3.0
  43.  
  44. squareRoot( 9 ); // 2) type checker does an implicit int-> long conversion
  45. // sensing the programmer "intent" and allowing
  46. // the sqaureRoot method to be reused for (int, short, byte, char)
  47. // primitive types.
  48. // prints 3.0
  49.  
  50. squareRoot ( (short)9 ); // 3) static typing does short -> long conversion
  51. // prints 3.0
  52.  
  53. squareRoot ( (byte)9 ); // 3) static typing does byte -> long conversion
  54. // prints 3.0
  55.  
  56. //this number literal 123982901299 is not within 32-bit int range
  57. //and therefore this wont compile since Java assumes all literals are 32-bit ints
  58. squareRoot ( 123982901299 ); //Note: Above the method (at runtime) did a int -> long
  59. // conversion when i called sqaureRoot with an int.
  60. // ...BUT in this case, Java requires that you
  61. // are explicit when declaring a literal
  62. // even though we KNOW the literal is being passed
  63. // into a method that accepts a long value.
  64. // COMPILATION ERROR
  65.  
  66. squareRoot ( 123982901299L );// By being explicit with a postfix 'L' to the literal
  67. // the compiler knows to represent 123982901299
  68. // as a 64-bit long.
  69. // prints 352112.0578722064
  70.  
  71.  
  72. squareRoot( '9' ); //3) '9' is a char, it will compile and run
  73. // ...BUT all chars are ALSO an unsigned 16-bit numbers [0, 65535]
  74. // prints 7.54983443527075
  75. // (BUT WE MIGHT EXPECT it to print 3.0, even though Java assumed
  76. // we wanted to evaluate the char as a 16-bit unsigned number)
  77.  
  78. System.out.println ( (int)'9' ); //...BECAUSE '9' == 57 (as an unsigned 16-bit number)
  79. // and the squareRoot( 57 ); == 7.54983443527075
  80.  
  81. //this won't compile because of static type checking ("String" is not compatible with int/long)
  82. //even though what is represented by "9" is a single char...
  83. squareRoot("9"); // even though a String is "really" a sequence of characters
  84. // COMPILATION ERROR
  85.  
  86. squareRoot ( Long.parseLong( "9" ) ); // we must explicitly use this conversion on
  87. // a String to convert it to a Long
  88. // prints 3.0
  89.  
  90. squareRoot ( ANumber.NINE ); // Java does not automatically ASSUME / convert an ENUM
  91. // to its ordinal value
  92.  
  93. squareRoot ( ANumber.NINE.ordinal() ); // so can Explicitly do this to circumvent the problem
  94.  
  95.  
  96.  
  97. }
  98.  
  99. /**
  100. * this is a simple method to illustrate static typing
  101. * in Java
  102. * @param longParameter
  103. */
  104. public static void squareRoot ( long longParameter )
  105. {
  106. //Math.sqrt accepts double
  107. //Java does an (implicit cast) from long -> double
  108. //BEFORE calling Math.sqrt()
  109. System.out.println ( Math.sqrt( longParameter ) );
  110. }
  111.  
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement