some fragment of thinking in java part1

本文深入讲解了Java编程的基础概念,包括字符串初始化、数组引用、继承关系的定义、静态关键字的作用、构造函数的自动调用机制、方法重载规则以及this关键字的使用场景等核心知识点。

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.

转载于:https://www.cnblogs.com/terminator-LLH/p/4889133.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值