some fragment of thinking in java part1

1.String s;

System.out.println("s=");

you will get a compile time error because s isn't actually attached to anything, A safer practice, then is always initialize a reference when you creacte it:

String s = new "asdf";

2. When you create an array of objcets, you are really creating an array of references, and each of those references is automatically initialzied to a spacial value with its own key word: null, When Java sees null, it recognizes that the reference in question isn't pointing to an object, You must assigin an object to each reference before you use it, and if you try to use a reference that's is still null, the problem will be reported at run time, Thus, typical array errors are prevented in Java.

3. A test for inheritance is to determine whether you can state the "is-a" relationship about the classes and have it make sense

"A circle is a shape"

another test is "is-like a"

"the heat pump is like a airconditioner"  //air conditioner can only control for cooling, then the heat pump can both heat and cool;

4. static key word.

it is a piece of storage for a particular field, it not about the object and only initialized once, you can refer to it through its class name, something you can't do with a non-static number.

5.Initialization & Cleanup

Java automatically calls that constructor when an object is created, before users can even get their hands on it, So initialization is guaranteed.

The new expression does return a reference to the newly created object, but the constructor itself has no return value.

6. Overloaded Methods

Each methods must take a nuique list of argument types

 the number of the arguments is different.

the type of each arguments is different.

the order of arguments is different. // you don't normally want to take this approach since it produces difficult-to-maintain code;

Overloading With Primitives

if you hava a data type that is smaller than the argument in the method, that data type is promoted, char produces a slightly different effects, since if it doesn't find a exact char match, it is promoted to int.

the mehods take narrower primitive values, if your argument is wider, then you must perform a narrowing conversion with cast, otherwise the compiler will issue an error message.

7. The This Keyword

this can be only used inside a non-static method.

if you are calling a method of your class from within another method of your class, you don't need to use this.

public calss Apricot {

  void pick() {/*  */}

  void pit() {/*  */}

}

the this keyword is used only for those spacial cases in which you need to explicitly use the reference to the current object

pulic class Leaf {

  int i = 0;

  Leaf increament() {

    i++;

    return this;

  }

  void print() {

    System.out.println("i= " + i);

  }

  public static void main(String[] args){

    Leaf x = new Leaf();

    x.increament().increament().inceament().print();

  }

}// Output: i = 3

to pass oneself to the foreign method it must use this.

when you call one constructor from another to avoid duplicating code, you can make such a call by using the this keyword,while you can call one constructor using this, you cannot call two. In addtion, the constructor call must be the first thing you do, or you will get a compiler error message.

posted on 2015-10-18 10:58  terminator-LLH  阅读(167)  评论(0编辑  收藏  举报

导航