476179

ConversionBetweenPrimitiveDataTypes

Oct 3rd, 2019
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. package lessons;
  2.  
  3. public class ConversionBetweenPrimitiveDataTypes
  4. {
  5.  
  6. public static void main(String[] args)
  7. {
  8. //ex 1
  9. int x;
  10. double y = 2.5;
  11. //x = y;
  12.  
  13. //ex 2
  14. int a ;
  15. short b = 2;
  16. a = b;
  17. // can go bec 2 is also an int, int and shorts are both whole, ints can hold more
  18.  
  19. // highest to lowest: double, float long int short byte
  20. // can only go from lower ranks to higher
  21. //lower to higher rank conversion is widening conversion
  22. //higher to lower rank conversion is narrowing conversion
  23.  
  24. //widening conversion
  25. double k;
  26. int m = 10;
  27. k = m;
  28.  
  29.  
  30. //strictly typed language: the data type inserted in values must be the same data type as variable
  31.  
  32.  
  33.  
  34. }
  35.  
  36. }
Add Comment
Please, Sign In to add comment