Guest User

Untitled

a guest
Nov 18th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. public class PrimitiveAssignments {
  2.  
  3. public static void main(String[] args) {
  4.  
  5. short sh = 300; // by default integer literals are int
  6. short sh1 = 127 * 5; // By default a result of an expression is an int
  7. //short sh2 = 127 * 1000; Does not compile because the int returned does not fit in a range of short
  8.  
  9. short sh3 = 5;
  10. short sh4 = 127;
  11. //short sh5 = sh3 * sh4; //Does not compile because the value of a variable can change at runtime, needs a cast
  12. short sh5 = (short)(sh3 * sh4); // Works fine using casting.
  13.  
  14. }
  15. }
Add Comment
Please, Sign In to add comment