Advertisement
Guest User

Untitled

a guest
Aug 26th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. /*Joey Goist
  2. 8/26/16
  3. This program will be a review how to start declaring variables in programs. */
  4.  
  5. public class VariableType {
  6. public static void main(String[] args) {
  7. /*What is an identifier
  8. - It is the variable, parameter, constant, user-defined method, or a defined class.
  9. - You pick the name.
  10. - Cannot start with a number or a symbol
  11. - Cannot be a keyword
  12. - No spaces
  13. - It should desribe what the variable represents
  14. - variables should start with lowercase letter
  15. - Class name should be capital
  16. - Use an uppercase letter to seperate two words (getName)
  17. or use underscore get_name
  18. - Make it specific (age is different ffrom Age)
  19. - Do not repeat variable names
  20.  
  21. Identifiers have a primitive or built-in type:
  22. What are the different primitive types:
  23.  
  24. INTEGERS:
  25. byte
  26. short
  27. int
  28. long
  29.  
  30. DECIMALS:
  31. float
  32. double
  33.  
  34. LETTERS/WORDS:
  35. char
  36. String
  37.  
  38. TRUE/FALSE:
  39. boolean
  40.  
  41. ***IMPORTANT AP EXAM: int, double, boolean, String
  42.  
  43.  
  44. If you put in a number too large for an integer you get an overflow error
  45. Java will give you no warning, you just get a wrong result
  46.  
  47. Why should you not always use a long?
  48. Compile and run time would be faster
  49.  
  50. INTIALIZING VARIABLES: */
  51. int number = 5;
  52. String word = "hello";
  53. double decimal = 5.9;
  54.  
  55. int x;
  56. char grade = 'A';
  57. boolean rerun = false;
  58.  
  59. double d = 1.45667E23 //Scientific Notation
  60.  
  61. /*CASTING:
  62. What is casting?
  63. Converting a data type manually
  64.  
  65. Why would you need to use casting?
  66. We need to watch when dividing. */
  67.  
  68. int total = 10, n =3;
  69. double average = total/n; //INTEGER DIVISION
  70. System.out.println(average);
  71.  
  72. average = (double)total/n;
  73. System.out.println(average);
  74.  
  75. average = total/(double)n;
  76.  
  77. //What would happen here?
  78. average = (double)(total/n); //This prints 3.0 based on Order of Operations
  79.  
  80.  
  81. //ASSIGNING/DECLARING VARIABLES
  82. int sum = 5;
  83. double tot = sum; //This is allowedsince tot is a double which is larger than an int
  84.  
  85. double g = 5.95;
  86. //int intNum = g; ERROR: Loss of Precision Error
  87. //To fix you need to Cast
  88.  
  89. //ROUNDING TO WHOLE NUMBERS:
  90. //We can use Math.round(variableName);
  91. int negAmount = -5.9;
  92. System.out.println(Math.round(negAmount));
  93.  
  94. //Using the strategy of adding or subtracting .5
  95.  
  96. //Math.round returns a double
  97.  
  98. /*FINAL VARIABLES:
  99. What is the final variable?
  100. - This variable cannot be changed (Constant)
  101. - Coding etiquette has the name in all caps
  102. - use _ for two or more words
  103. - Can declare it or we can delcare and intialize
  104. - Common use is for array bounds
  105. - Makes it easier to revise code, by only changing 1 variable */
  106. final double TAX_RATE = 0.6;
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement