Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lessons;
- public class CastOperators
- {
- public static void main(String[] args)
- {
- double num = 2.1;
- int x = (int)num;
- // the (int) is cast operator
- //forces java to convert the data type regardless to fit parameters
- System.out.println("number before conversion is: "+num
- +"\nnumber after conversion: "+x);
- short lilnum;
- int bignum =
- 0;
- lilnum = (short)bignum;
- // does not allow narrowing conversion
- x = (int)3.7;
- int pies = 10 , people = 4;
- double piePperson;
- piePperson = (double)pies / (double)people;
- System.out.println("pies per person is: "+ piePperson);
- // performs int division, also when evaluating two values it always assumes higher type
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement