Thursday, April 2, 2015

Java: Chapter 4: Everything is an object

Chapter 4: Everything is an object
  1. Java is not "pass-by-reference", Java passes on object reference by values.
  2. Java put objects created by using new into heap.
  3. Primitive data types are in the stack.
  4. Wrapper class: allows to make non-primitive object on the heap to represent that primitive type. 
    1. Why wrapper primitives? Shown in later chapter. 
  5. Autoboxing: automatically converts from primitive types to wrapper class, and verse vera. 
  6. BigInteger, BigDecimal: arbitrary-precision integers or fixed-point numbers. Used to more accurate calculations. 
  7. Array in Java is guaranteed to be initialized and cannot be accessed outside of its range. The range checking comes at price of a small amount of memory overhead on each array, as well as verifying the index at run time. But the assumption is that the safety and increased productivity are worth the expense. 
  8. Garbage collector looks at all the objects that were created with new and figures out which ones are not being referenced anymore. Then it releases the memory for those objects, so the memory can be used for new objects. 
  9. A Java class consists of fields and methods. Fields are sometimes called data members. Methods are sometimes called member functions. 
  10. Primitive type as a data member will be always initialized. However, local variables are not automatically initialized. If you do so, you will get a compile-time error. 
  11. Signature: method name and the argument list are called signature of a method because it could uniquely identify that method. 
  12. static keyword in Java:
    1. There are two situations in which the static is needed. 
      1. Static data members: if you wanna have only a single piece of storage for a particular field, regardless of how many objects of that class that created, or even if no objects are created. 
      2. Static methods: if you need a method that isn't associated with any particular object of this class. That is, you need a method that you can call even if no objects are crated. 
    2. static filed (data members): 
class StaticTest {
    static int i = 47;
}
// Access to the data members can be either of the two ways:
StaticTest st1 = new StaticTest();
st1.i++; // OR
StaticTest.i++; // The second way is more preferred.

No comments:

Post a Comment