Advertisement
MerAll

Primitive Casting Review

Aug 30th, 2014
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.27 KB | None | 0 0
  1. /**Basic primitive and String casting review.
  2.  *
  3.  * @author Meredith
  4.  *
  5.  */
  6. public class CastingDemo {
  7.  
  8.     public CastingDemo() {
  9.  
  10.         int i = 012; //octal 0+1*8+2=10  (octal)
  11.         System.out.println((i == 10) ? "true" : "false" + i);
  12.  
  13.         //not allowed: i=10.0;  becomes 10.0d, which requires cast to float
  14.  
  15.         //float still requires explicit cast to int   i=10.0f;
  16.         float f = 10.51f;
  17.         int i2 = (int) f; //should truncate to 10.0 ( (int)float truncates; it doesn't round.)
  18.         System.out.println((i2 == 10) ? "true" : "false" + i2);
  19.  
  20.         int i3 = 0b1010; //1*8+0*4+1*2+0*1=10  (binary)
  21.         System.out.println((i3 == 10) ? "true" : "false:" + i3);
  22.  
  23.         int i4 = 0xA; //10*1 =10 (hexadecimal)
  24.         System.out.println((i4 == 10) ? "true" : "false:" + i4);
  25.  
  26.         int i5 = 0x2A; //2*16+10=42
  27.         System.out.println((i5 == 42) ? "true" : "false:" + i5);
  28.  
  29.         //now, how about operations and comparisons?
  30.  
  31.         int i6 = 0x2A; //42 (int from octal)
  32.         //comparing it to a double (still works.)
  33.         System.out.println((i6 == 42.00) ? "true" : "false:" + i6);
  34.  
  35.         //trying to store result of double*int into an int will fail.
  36.         int i7 = 0x2A;
  37.         //int i8=i7*1.00; //no; result will be cast to double.
  38.         //(result always = cast to widest precision in operation.)
  39.         int i8 = (int) (i7 * 1.00);
  40.         System.out.println((i8 == 42) ? "true" : "false:" + i8);
  41.  
  42.         String s1 = "true";
  43.         boolean b1 = true;
  44.         //won't work:  
  45.         //boolean b2=(boolean)s1; (cannot cast from String to boolean, even explicitly.)
  46.         //Also, cannot perform operations on the two: System.out.println((b1==s1)?"true":"false:");
  47.  
  48.         char c1 = 'a';
  49.         //not allowed: String s2=c1;
  50.         //still not allowed: String s2=(String)c1; (can't even explicitly cast char to String.)
  51.         String s2 = String.format("%s", c1); //this one is allowed.
  52.         System.out.println((s2 == "a") ? "true" : "false:" + s2); //false!  String == checks pointers!
  53.         System.out.println((s2.equals("a")) ? "true" : "false:" + s2); //true (lexicographical comparison.)
  54.  
  55.         String s3 = "1.05";
  56.         float f1 = 1.05f;
  57.         String s4 = String.format("%f", f1);
  58.         System.out.println((s3.equals(s4)) ? "true" : "false:" + s4); //false (lexicographical comparison, but float doesn't autotruncate.)
  59.         //so "1.050000" does not .equals() "1.05", even though
  60.         //the two are numerically equivalent.
  61.  
  62.         String s5 = String.format("%.2f", f1);
  63.         System.out.println((s3.equals(s5)) ? "true" : "false:" + s5); //these two are equal.
  64.  
  65.         //boolean b2 = (boolean)"true"; //won't work, but
  66.         String sb2 = String.format("%b", "true"); //does?  yes and no.  
  67.         //You're still just getting the string "true"
  68.         //somehow, it's accepted by the boolean formatter, though.
  69.         System.out.println((sb2.equals(true)) ? "true" : "false:" + sb2); //String "true" does not .equals(boolean true)
  70.         //in fact, String.equals(non-String primitive) will take any primitive,
  71.         //as an argument, but won't return true for any of them.
  72.  
  73.         int i9 = 1;
  74.         System.out.println(("1".equals(i9)) ? "true" : "false"); //thanks to autoboxing, "1".equals() works.
  75.         //but you'll still get false for equality to int i.
  76.     }
  77.  
  78.     public static void main(String[] args) {
  79.         CastingDemo cd = new CastingDemo();
  80.  
  81.     }
  82.  
  83. }
  84.  
  85. /**Program output:
  86. true
  87. true
  88. true
  89. true
  90. true
  91. true
  92. true
  93. false:a
  94. true
  95. false:1.050000
  96. true
  97. false:true
  98. false
  99. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement