Saturday, July 15, 2017

Core Java : BrushUp

Point 1 : Always override hashcode() when you override equals()
there is a contract for equals : asset

Point 2 : minimize accessibility of classes and members

Point 3: In public classes, use accessor methods, not public fields

Point 4: Minimize mutability

Point 5: Favor composition over inheritance
  1. One reason of favoring Composition over Inheritance in Java is fact that Java doesn't support multiple inheritance.
  2. Composition offers better test-ability of a class than Inheritance. If one class is composed of another class, you can easily create Mock Object representing composed class for sake of testing.Inheritance doesn't provide this luxury. In order to test derived class, you must need its super class
  3. Though both Composition and Inheritance allows you to reuse code, one of the disadvantage of Inheritance is that it breaks encapsulation. If sub class is depending on super class behavior for its operation, it suddenly becomes fragile. When behavior of super class changes, functionality in sub class may get broken, without any change on its part

Item 6 : Prefer interface over Abstract Class
1) can implement more than one interface
2) abstract class may contain state (data members) and/or implementation (methods)

http://codeofdoom.com/wordpress/2009/02/12/learn-this-when-to-use-an-abstract-class-and-an-interface/
  1. *) Abstract classes allow for default default function definition : want to modify common code and everyone should use new code
  2. *) It is a situation of "Is-A" vs "Can-Do-this". Objects that extends an Abstract class "Is-A" base class. Objects that implement "Can-Do-This".

Item 7 : Why use Enums
  1. If you use enums instead of integers (or String codes), you increase compile-time checking and avoid errors from passing in invalid constants, and you document which values are legal to use.... ex pass an int , then it can take any integer but you have 5 document types then you can make enum of 5 types
  2. switch statement
  3. can use as singleton

Item 8 : Marker Interfaces are used to define types
What purpose does the Cloneable interface serve?

When JVM sees a clone() method being invoked on an object, it first verifies if the underlying class has implemented the 'Cloneable' interface or not. If not, then it throws the exception CloneNotSupportedException.


public Object clone() throws CloneNotSupportedException {
 if (this implements Cloneable)
     return nativeCloneImpl();
 else
     throw new CloneNotSupportedException();
}


Item 9 : Beware the performance of string concatenation

The string concatenation operator (+) is a convenient way to combine a few strings into one. It is fine for generating a single line of output or for constructing the string representation of a small, fixed-size object, but it does not scale. Using the string concatenation operator repeatedly to concatenate n strings requires time quadratic in n. It is an unfortunate consequence of the fact that strings are immutable (Item 15). When two strings are concatenated, the contents of both are copied.

For example, consider the following method that constructs a string representation of a billing statement by repeatedly concatenating a line for each item:

// Inappropriate use of string concatenation - Performs horribly!

public String statement() {
    String result = "";
    for (int i = 0; i < numItems(); i++)
        result += lineForItem(i);  // String concatenation
    return result;
}

This method performs abysmally if the number of items is large. To achieve acceptable performance, use a StringBuilder in place of a String to store the statement under construction. (The StringBuilder class, added in release 1.5, is an unsynchronized replacement for StringBuffer, which is now obsolete.)


public String statement() {
    StringBuilder b = new StringBuilder(numItems() * LINE_WIDTH);
    for (int i = 0; i < numItems(); i++)
        b.append(lineForItem(i));
    return b.toString();
}

The difference in performance is dramatic

Item 10 : Java Parameter
https://stackoverflow.com/questions/3108182/using-parameter-that-implements-multiple-interfaces-pre-generics

Item 11 : difference between T & ?
  • ? is a wildcard and means any sublass of ONEITEMInterface including itself.
  • T is a specific implementation of ONEITEMInterface in this case.
  • Since ? is a wildcard, there is no relation between your ? in the class declaration and the ? in your method declaration hence it won't compile. Just List getONEITEM(); will compile though.

The first scenario means the entire class can handle exactly one type of Bar per instance.


interface Foo<T extends Bar> {
     List<T> get();
}

The second scenario allows each instance to operate on any subtype of Bar


interface Foo {
     List get()
}


No comments: