java中变量的介绍

 变量分为两种:primitive 主数据类型和引用

实例变量,非静态域,对象通过实例变量来保存他们的状态

class Bicycle {

       int cadence = 0;
       int speed = 0;
       int gear = 1;

       void changeCadence(int newValue) {
            cadence = newValue;
       }

       void changeGear(int newValue) {
            gear = newValue;
       }

}

类变量(静态域)声明为static的变量实质上就是全局变量。当声明一个对象时,并不产生static变量的拷贝,而是该类所有的实例变量共用同一个static变量。

static int numGears = 6;

关键字"final"保证 类变量的值不会变

局部变量:声明在方法中的变量,只能在声明他的方法内可见,局部变量需要赋初值

参数:The important thing to remember is that parameters are always classified as "variables" not "fields". This applies to other parameter-accepting constructs as well

java很注重类型,不能把长颈鹿类型的变量放到兔子类型的变量中

Rabbit h=new Giraffe(); 类型不匹配错误

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/index.html 

http://www.edongcn.com/blog/article.asp?id=102

你可能感兴趣的:(java,基础)