Crenox

Head First Java NOTES

Dec 25th, 2013
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.43 KB | None | 0 0
  1. Head First Java NOTES:
  2.  
  3. A try/catch is when you try to do something and if you it works,
  4. it prints out the try part but if it doesn't work, it prints out
  5. the catch part of the code.
  6.  
  7. Java is an object-oriented (OO) langauge. It's not like the old
  8. days when you had steam-driven compilers and wrote on monolithic
  9. source file with a pile of procedures.
  10.  
  11. A class is a blueprint for an object, and that nearly everything
  12. in Java is an object.
  13.  
  14. When you design a class, think about the objects that will be created
  15. from that class type:
  16. - things the object knows
  17. - things the object does
  18. For example, a shopping cart (class name = ShoppingCart):
  19. What the object KNOWS is...
  20. cartContents
  21. What it DOES is...
  22. addToCart()
  23. removeFromCart()
  24. checkOut()
  25. Another example is an alarm (class name = Alarm):
  26. What the object KNOWS is...
  27. alarmTime
  28. alarmMode
  29. What the object DOES is...
  30. setAlarmTime()
  31. getAlarmTime()
  32. isAlarmSet()
  33. snooze()
  34.  
  35. What the object knows about itself is it's instance variables (state)
  36. What the object can do are called methods (behvaior)
  37. For example, a Song class' instance variables or
  38. what it knows would be (class name = Song):
  39. title
  40. artist
  41. What it'd know how to do, it's methods, would be:
  42. setTitle()
  43. setArtist()
  44. play()
  45.  
  46. The instance variables represent an object's state (the data),
  47. and can have unique values for each object of that type.
  48. Think of instance as another way of saying object.
  49.  
  50. A tester class is where you put the main method, in that main()
  51. method you create and access objects of your new class type.
  52.  
  53. Here's an example:
  54.  
  55. STEPS:
  56. 1. Write your class
  57.  
  58. public class chair
  59. {
  60. int size; <----- Instance variables
  61. String brand; <----- Instance variables
  62. String type; <----- Instance variables
  63.  
  64. public void sit() <----- a method
  65. {
  66.  
  67. }
  68. }
  69.  
  70. 2. Write a tester
  71. public class chairTester
  72. {
  73. public static void main(String args[])
  74. {
  75. // Chair test code goes here
  76. }
  77. }
  78.  
  79. 3. In your tester, make an object and access
  80. the object's variables and methods
  81.  
  82. public class chairTester
  83. {
  84. public static void main(String args[])
  85. {
  86. chair c = new chair();
  87.  
  88. c.size = 40;
  89. c.brand = "kohl's";
  90. c.type = "wood";
  91. }
  92. }
  93.  
  94. The two uses of main:
  95. - to test your real class
  96. - to launch/start your Java application
  97.  
  98. ---------------------------------------------------------------------------------------------------
  99. WHO AM I?
  100. I am compiled from a .java file = class
  101. My instance variable values can be different from my buddy's values = object
  102. I behave like a template = class
  103. I like to do stuff = object, method
  104. I can have many methods = class, object
  105. I represent 'state' = instance variable
  106. I have behaviors = object, class
  107. I am located in objects = method, instance variable
  108. I live on the heap = object
  109. I am used to create object instances = class
  110. My state can change = object, instance variable
  111. I declare methods = class
  112. I can change at runtime = object, instance variable
  113. ---------------------------------------------------------------------------------------------------
  114.  
  115. There are two types of variables: primitive and reference.
  116.  
  117. Primitive is with the eight primitive main types which is:
  118. boolean char byte short int long float double
  119.  
  120. Reference is:
  121. Dog myDog = new Dog();
  122.  
  123. Arrays are always objects, whether they're declared to hold
  124. primitives or object references.
  125.  
  126. Remember: a class describes what an object knows and what an
  127. object does.
  128.  
  129. Arguments or parameters are values passed into the method.
  130. You should remember it like this:
  131. A method uses parameters. A caller passes arguments.
  132.  
  133. Arguments are the things you pass into the methods.
  134. Parameters are nothing more than a local variable. A variable
  135. with a type and a name, that can be used inside the body
  136. of the method. But here's the important part:
  137. If a method takes a parameter, you must pass it something. And
  138. that something must be a value of the appropriate type.
  139.  
  140. If you declare a method to return a value, you must return
  141. a value of the declared type (Or a value that is compatible
  142. with the declared type)!
  143.  
  144. EXAMPLE:
  145. ----------------------------------------
  146. int theSecret = life.giveSecret();
  147.  
  148. int giveSecret()
  149. {
  150. return 42;
  151. }
  152. ----------------------------------------
  153.  
  154. Methods can have multiple parameters. Most importantly, if
  155. a method has parameters, you must pass arguments of the
  156. right type and order.
  157.  
  158. Calling a two-parameter method, and sending it two arguments.
  159. EXAMPLE:
  160. ----------------------------------------
  161. void go()
  162. {
  163. TestStuff t = new TestStuff();
  164. t.takeTwo(12, 34);
  165. }
  166.  
  167. void takeTwo(int x, int y)
  168. {
  169. int z = x + y;
  170. System.out.println("Total is " + z)
  171. }
  172. ----------------------------------------
  173.  
  174. You can pass variables into a method, as long as the
  175. variable type matches the parameter type.
  176. EXAMPLE:
  177. ----------------------------------------
  178. void go()
  179. {
  180. int foo = 7;
  181. int bar = 3;
  182. t.takeTwo(foo, bar);
  183. }
  184.  
  185. void takeTwo(int x, int y)
  186. {
  187. int z = x + y;
  188. System.out.println("Total is " + z)
  189. }
  190. ----------------------------------------
  191.  
  192. BULLET POINTS:
  193. ----------------------------------------------------------------
  194. Classes define what an object knows and what an object does.
  195. Things an object knows are it's instance variables (state).
  196. Things an object does are its methods (behavior).
  197. Methods can use instance variables so that objects of the same
  198. type can behave differently.
  199. A method can have parameters, which means you can pass one or
  200. more values in to the method.
  201. The number and type of values you pass in must match the order
  202. and type of the parameters declared by the method.
  203. Values passed in and out of methods can be implicitly promoted
  204. to a larger type of explicitly cast to a smaller type.
  205. The value you pass as an argument to a method can be a literal
  206. value (2, 'c', etc.) or a variable of the declared parameter
  207. type (for example, x where x is an int variable). (There are
  208. other things as well that you can pass as arguments).
  209. A method must declare a return type. A void return type means
  210. the method doesn't return anything.
  211. If a method declares a non-void return type, it must return a
  212. value compatible with the declared return type.
  213. ----------------------------------------------------------------
  214.  
  215. Getters and Setters let you get and set things. Instance variables
  216. usually. A Getter's sole purpose in life is to send back, as a
  217. return value, the value of whatever it is that particular Getter
  218. is supposed to be Getting. And by now, it's probably no surprise
  219. that a Setter lives and breathes for the chance to take an
  220. argument value and use it to set the value of an instance variable.
  221.  
  222. Mark instance variables private. Mark getters and setters public.
  223.  
  224. Encapsulation in a sense puts a force-field around the instance variables
  225. so no one does anything to them. Most instance variables are coded
  226. with certain assumptions about the boundaries of the values. Like, think
  227. of all the things that would break if negative numbers were allowed.
  228. Number of bathrooms in an office. Velocity of an airplane. Birthdays.
  229. Barbell weight. Cell phone numbers. Microwave oven power.
  230.  
  231. Encapsulation lets you set boundaries by forcing other code to go through
  232. setter methods. That way, the setter method can validate the parameter and
  233. decide if it's do-able. Maybe the method will reject it and do nothing, or
  234. maybe it'll throw an Exception, or maybe the method will round the parameter
  235. sent in to the nearest acceptable value. The point is, you can do whatever
  236. you want in the setter method, whereas you can't do anything if your
  237. instance variables are public.
  238.  
  239. The point to setters and getters too, is that you can change your mind
  240. later, without breaking anybody else's code! Imagine if half the people
  241. in your company used your class with public instance variables, and one
  242. day you suddenly realized, "Oops-there's something I didn't plan for
  243. with that value, I'm going to have to switch to a setter method." You
  244. break everyone's code. The cool thing about encapsulation is that you
  245. get to change your mind. And nobody gets hurt. The performance gain from
  246. using variables directly is so minuscule and would rarely, if ever, be
  247. worth it.
  248.  
  249. Instance variables always get a default value. If you don't explicitly
  250. assign a value to an instance variable, or you don't call a setter
  251. method, the instance variable still has a value.
  252.  
  253. Local variables do NOT get a default value! The compiler complains if you
  254. try to use a local variable before the variable is initialized.
  255.  
  256. Instance variables are declared inside a class but not within a method.
  257. Local variables are declared within a method.
  258. Local variables MUST be initialized before use!
  259.  
  260. Method parameters are virtually the same as local variables --- they're declared inside the method (well, technically they're declared in the argument list of the method rather than within the body of the method, but they're still local variables as opposed to Instance variables). But method parameters will never be uninitialized, so you'll never get a compiler error telling you that a parameter variable might not have been initialized. But that's because the compiler will give you an error if you try to invoke a method without sending arguments that the method needs.So parameters are ALWAYS initialized, because the compiler guarantees that methods are always called with arguments that match the parameters declared for the method, and the arguments are assigned (automatically) to the parameters.
Advertisement
Add Comment
Please, Sign In to add comment