Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**Basic primitive and String casting review.
- *
- * @author Meredith
- *
- */
- public class CastingDemo {
- public CastingDemo() {
- int i = 012; //octal 0+1*8+2=10 (octal)
- System.out.println((i == 10) ? "true" : "false" + i);
- //not allowed: i=10.0; becomes 10.0d, which requires cast to float
- //float still requires explicit cast to int i=10.0f;
- float f = 10.51f;
- int i2 = (int) f; //should truncate to 10.0 ( (int)float truncates; it doesn't round.)
- System.out.println((i2 == 10) ? "true" : "false" + i2);
- int i3 = 0b1010; //1*8+0*4+1*2+0*1=10 (binary)
- System.out.println((i3 == 10) ? "true" : "false:" + i3);
- int i4 = 0xA; //10*1 =10 (hexadecimal)
- System.out.println((i4 == 10) ? "true" : "false:" + i4);
- int i5 = 0x2A; //2*16+10=42
- System.out.println((i5 == 42) ? "true" : "false:" + i5);
- //now, how about operations and comparisons?
- int i6 = 0x2A; //42 (int from octal)
- //comparing it to a double (still works.)
- System.out.println((i6 == 42.00) ? "true" : "false:" + i6);
- //trying to store result of double*int into an int will fail.
- int i7 = 0x2A;
- //int i8=i7*1.00; //no; result will be cast to double.
- //(result always = cast to widest precision in operation.)
- int i8 = (int) (i7 * 1.00);
- System.out.println((i8 == 42) ? "true" : "false:" + i8);
- String s1 = "true";
- boolean b1 = true;
- //won't work:
- //boolean b2=(boolean)s1; (cannot cast from String to boolean, even explicitly.)
- //Also, cannot perform operations on the two: System.out.println((b1==s1)?"true":"false:");
- char c1 = 'a';
- //not allowed: String s2=c1;
- //still not allowed: String s2=(String)c1; (can't even explicitly cast char to String.)
- String s2 = String.format("%s", c1); //this one is allowed.
- System.out.println((s2 == "a") ? "true" : "false:" + s2); //false! String == checks pointers!
- System.out.println((s2.equals("a")) ? "true" : "false:" + s2); //true (lexicographical comparison.)
- String s3 = "1.05";
- float f1 = 1.05f;
- String s4 = String.format("%f", f1);
- System.out.println((s3.equals(s4)) ? "true" : "false:" + s4); //false (lexicographical comparison, but float doesn't autotruncate.)
- //so "1.050000" does not .equals() "1.05", even though
- //the two are numerically equivalent.
- String s5 = String.format("%.2f", f1);
- System.out.println((s3.equals(s5)) ? "true" : "false:" + s5); //these two are equal.
- //boolean b2 = (boolean)"true"; //won't work, but
- String sb2 = String.format("%b", "true"); //does? yes and no.
- //You're still just getting the string "true"
- //somehow, it's accepted by the boolean formatter, though.
- System.out.println((sb2.equals(true)) ? "true" : "false:" + sb2); //String "true" does not .equals(boolean true)
- //in fact, String.equals(non-String primitive) will take any primitive,
- //as an argument, but won't return true for any of them.
- int i9 = 1;
- System.out.println(("1".equals(i9)) ? "true" : "false"); //thanks to autoboxing, "1".equals() works.
- //but you'll still get false for equality to int i.
- }
- public static void main(String[] args) {
- CastingDemo cd = new CastingDemo();
- }
- }
- /**Program output:
- true
- true
- true
- true
- true
- true
- true
- false:a
- true
- false:1.050000
- true
- false:true
- false
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement