Advertisement
476179

varandprimdatatypes

Sep 24th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. package lessons;
  2.  
  3. public class VariablesAndPrimitiveDataTypes
  4. {
  5.  
  6. public static void main(String[] args)
  7. {
  8. //(int)egers = 3 (can hold up to about 2 billion)
  9. //floating point numbers(32bit) /doubles(64bit) = 1.17 |+-3.4^38
  10. //bytes = 0-128 (only whole numbers)
  11. //short = 0 - 32 767
  12. //boolean = true or false
  13. //chars= single characters
  14. //long = +-2^63
  15. //largest to smallest: double,float,long,int,short,byte,char,boolean
  16.  
  17. //dataType variableName
  18. // variable declaration
  19. byte inches;
  20. short month;
  21. int speed;
  22. long lightYear;
  23. float salesCommission;
  24. double distance;
  25.  
  26.  
  27. System.out.println("Example 1: Integer Data Types");
  28. int checking;
  29. byte miles;
  30. short minutes;
  31. long days;
  32. //the types of data these can hold are whole numbers ONLY
  33.  
  34.  
  35. //variable initialization
  36. checking = -20;
  37. miles = 105;
  38. minutes = 120;
  39. days = 189_000;
  40.  
  41. // + are catination operations. It means to join together.
  42. System.out.println("we have a journey of "+ miles + " miles.");
  43. System.out.println("it took us " + minutes + " minutes");
  44. System.out.println("Our account balance is $"+ checking);
  45. System.out.println("About " + days + " days ago, Columbus"+" stood on this spot.");
  46.  
  47. int length, width, area;
  48. int number = 1_257_649;
  49.  
  50. System.out.println("\n Example 2: Floating Point Data Types");
  51.  
  52. double price, tax, total;
  53.  
  54. price = 29.75;
  55. tax = 1.76;
  56. total = 31.51;
  57.  
  58. System.out.println("The price of the item is " +price);
  59. System.out.println("The tax is "+ tax);
  60. System.out.println("The total is " + total);
  61.  
  62. float numberA;
  63. numberA = 23.5F;
  64.  
  65. System.out.println("\nExample 3: E Notation");
  66.  
  67. double distanceB, mass;
  68. distanceB = 1.495979E11;
  69. // E = times ten to the power of
  70. mass = 1.989E30;
  71.  
  72. System.out.println("The sun is " + distanceB + " meters away");
  73. System.out.println("the suns mass is " + mass + " kilograms.");
  74.  
  75. System.out.println("\n ex4 boolean data type");
  76.  
  77. boolean bool;
  78. bool = true;
  79. System.out.println(bool);
  80. bool = false;
  81. System.out.println(bool);
  82.  
  83. System.out.println("\nEx5 stringls and string concatenation");
  84.  
  85. //strings are complex objects, a string of characters
  86. //'myVar' is an object
  87. String myVar = "Hello World";
  88. System.out.println(myVar);
  89.  
  90. String greeting ="hello how are you ";
  91. String name = "mitchell";
  92. System.out.println(greeting + name);
  93.  
  94.  
  95. System.out.println("peter piper picked a packe of peppers. "
  96. + "if peter pirper had pickeled a pack "
  97. + "of peppers how many pepppeers have peres pcik ");
  98.  
  99. System.out.println("Ex6 Char data type");
  100.  
  101. char letter;
  102. // ALL CHAR MUST BE enclosed in SINGLE quotes, double would be a string
  103. letter = 'A';
  104. System.out.println(letter);
  105. letter = 'b';
  106. System.out.println(letter);
  107.  
  108. System.out.println("Ex7 Variables hold only one value at a time");
  109. int unitsSold;
  110. //when naming variable use a lowercase first words can be capital
  111.  
  112. int lengthA = 20;
  113. // int is java reserved word, lengthA is user def word, 20 is numerical literal
  114. //when stating equivalent variables the var with a value must be right side
  115. int metersB = lengthA;
  116.  
  117. int flightNum = 69, travelTime, departure = 10, destination;
  118. //four var declared only two initialized
  119.  
  120. int x =5;
  121. System.out.println(x);
  122. x = 6;
  123. System.out.println(x);
  124.  
  125. int myNum;
  126. int myNUM;
  127. int mYnum;
  128. // all these variables are different because of use of capital and lowercase
  129. //variables cannot contain spaces, instead use '_' ex. my_name
  130. //Variables cannot START with an number
  131. //Variables cannot contain symbols ECXEPT a $ sign, ex.
  132. int $num = 15; //this is ok
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142. }
  143.  
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement