Advertisement
476179

cast operators

Oct 3rd, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. package lessons;
  2.  
  3. public class CastOperators
  4. {
  5.  
  6. public static void main(String[] args)
  7. {
  8. double num = 2.1;
  9. int x = (int)num;
  10. // the (int) is cast operator
  11. //forces java to convert the data type regardless to fit parameters
  12. System.out.println("number before conversion is: "+num
  13. +"\nnumber after conversion: "+x);
  14.  
  15.  
  16. short lilnum;
  17. int bignum =
  18. 0;
  19. lilnum = (short)bignum;
  20. // does not allow narrowing conversion
  21.  
  22. x = (int)3.7;
  23.  
  24.  
  25. int pies = 10 , people = 4;
  26. double piePperson;
  27. piePperson = (double)pies / (double)people;
  28. System.out.println("pies per person is: "+ piePperson);
  29. // performs int division, also when evaluating two values it always assumes higher type
  30.  
  31. }
  32.  
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement