变量和它的作用范围•在Java中,每个变量都有类型•在使用变量之前,必须先声明变量的类型。类型可以是简单类型,也可以是引用类型•变量总是在一个类中声明。但一个变量可能和整个类相关联,也可能只在方法体中或某个程序块中起作用标识符、关键字、数据类型(下)•成员变量/局部变量•值传递•Java编码规范变量声明的例子(局部变量)publicclasstest{……publicvoidaMethod(intj){intm,n,k;Stringa=“aaaa”,b=“bbbb”;m=j;k=100;System.out.println(m);System.out.println(n);//ErrorSystem.out.println(k);}}变量声明的例子(全局变量)(案例3-6)publicclasstest{inti=10,p=20;floatf,j;f=100.0fStrings1,s2;……}变量的初始化•局部变量在使用之前,必须先初始化。•全局变量如果没有初始化就拿来使用,系统将会自动给它一个默认的初值。全局变量的默认初值(案例3-7)TypeDefaultValueTypeDefaultValuebyte0short0int0long0Lfloat0.0fdouble0.0dchar‘\u0000’booleanfalseobjectrefnull传值(案例3-8/3-9)•Java在参数传递时,只使用值传递•当对象实例作为参数传递给方法时,这个参数的值是对象的引用,而不是对象本身0x1a4afb“Lisa”“male”1namesexgrade18ages10x1a4afbs20x1a4afbs2=s1实例(1)定义如下类:classBirthDate{privateintday;privateintmonth;privateintyear;publicBirthDate(intd,intm,inty){day=d;month=m;year=y;}publicvoidsetDay(intd){day=d;}publicvoidsetMonth(intm){month=m;}publicvoidsetYear(inty){year=y;}publicintgetDay(){returnday;}publicintgetMonth(){returnmonth;}publicintgetYear(){returnyear;}publicvoiddisplay(){System.out.println(day+-+month+-+year);}实例(2)运行如下程序:publicclassTest{publicstaticvoidmain(Stringargs[]){Testtest=newTest();intdate=9;BirthDated1=newBirthDate(7,7,1970);BirthDated2=newBirthDate(1,1,2000);test.change1(date);test.change2(d1);test.change3(d2);System.out.println(date=+date);d1.display();d2.display();}publicvoidchange1(inti){i=1234;}publicvoidchange2(BirthDateb){b=newBirthDate(22,2,2004);}publicvoidchange3(BirthDateb){b.setDay(22);}}调用过程演示(1)堆内存1109259587934354752200011197077Testtest=newTest();intdate=9;BirthDated1=newBirthDate(7,7,1970);BirthDated2=newBirthDate(1,1,2000);test.change1(date);test.change2(d1);test.change3(d2);………publicvoidchange1(inti){i=1234;}publicvoidchange2(BirthDateb){b=newBirthDate(22,2,2004);}publicvoidchange3(BirthDateb){b.setDay(22);}testdated1d2main栈内存9调用过程演示(2)堆内存1109259587934testdated1354752d2200011197077Testtest=newTest();intdate=9;BirthDated1=newBirthDate(7,7,1970);BirthDated2=newBirthDate(1,1,2000);test.change1(date);test.change2(d1);test.change3(d2);………publicvoidchange1(inti){i=1234;}publicvoidchange2(BirthDateb){b=newBirthDate(22,2,2004);}publicvoidchange3(BirthDateb){b.setDay(22);}mainchange1i栈内存调用过程演示(3)1234堆内存1109259587934testdated1354752d2200011197077Testtest=newTest();intdate=9;BirthDated1=newBirthDate(7,7,1970);BirthDated2=newBirthDate(1,1,2000);test.change1(date);test.change2(d1);test.change3(d2);………publicvoidchange1(inti){i=1234;}publicvoidchange2(BirthDateb){b=newBirthDate(22,2,2004);}publicvoidchange3(BirthDateb){b.setDay(22);}mainchange1i栈内存调用过程演示(4)堆内存1109259587934354752200011197077Testtest=newTest();intdate=9;BirthDated1=newBirthDate(7,7,1970);BirthDated2=newBirthDate(1,1,2000);test.change1(date);test.change2(d1);test.change3(d2);………publicvoidchange1(inti){i=1234;}publicvoidchange2(BirthDateb){b=newBirthDate(22,2,2004);}publicvoidchange3(BirthDateb){b.setDay(22);}testdated1d2main栈内存调用过程演示(5)587934堆内存1109259587934testdated1354752d2200011197077Testtest=newTest();intdate=9;BirthDated1=newBirthDate(7,7,1970);BirthDated2=newBirthDate(1,1,2000);test.change1(date);test.change2(d1);test.change3(d2);………publicvoidchange1(inti){i=1234;}publicvoidchange2(BirthDateb){b=newBirthDate(22,2,2004);}publicvoidchange3(BirthDateb){b.setDay(22);}mainchange2b栈内存调用过程演示(6)587934堆内存1109259587934testdated1354752d2200011197077Testtest=newTest();intdate=9;BirthDated1=newBirthDate(7,7,1970);BirthDated2=newBirthDate(1,1,2000);test.change1(date);test.change2(d1);test.change3(d2);………publicvoidchange1(inti){i=1234;}publicvoidchange2(BirthDateb){b=newBirthDate(22,2,2004);}publicvoidchange3(BirthDateb){b.setDay(22);}mainchange2b2004222栈内存调用过程演示(7)堆内存1109259587934354752200011197077Testtest=newTest();intdate=9;BirthDated1=newBirthDate(7,7,1970);BirthDated2=newBirthDate(1,1,2000);test.change1(date);test.change2(d1);test.change3(d2);………publicvoidchange1(inti){i=1234;}publicvoidchange2(BirthDateb){b=newBirthDate(22,2,2004);}publicvoidchange3(BirthDateb){b.setDay(22);}testdated1d2main栈内存调用过程演示(8)354752堆内存1109259587934testdated1354752d2200011197077Testtest=newTest();intdate=9;BirthDated1=newBirthDate(7,7,1970);BirthDated2=newBirthDate(1,1,2000);test.change1(date);test.change2(d1);test.change3(d2);………publicvoidchange1(inti){i=1234;}publicvoidchange2(BirthDateb){b=newBirthDate(22,2,2004);}publicvoidchange3(BirthDateb){b.setDay(22);}mainbchange3栈内存调用过程演示(9)354752堆内存1109259587934testdated1354752d22000221197077Testtest=newTest();intdate=9;BirthDated1=newBirthDate(7,7,1970);BirthDated2=newBirthDate(1,1,2000);test.change1(date);test.change2(d1);test.change3(d2);………publicvoidchange1(inti){i=1234;}publicvoidchange2(BirthDateb){b=newBirthDate(22,2,2004);}publicvoidchange3(BirthDateb){b.setDay(22);}mainbchange3栈内存调用过程演示(10)堆内存1109259587934354752200011197077Testtest=newTest();intdate=9;BirthDated1=newBirthDate(7,7,1970);BirthDated2=newBirthDate(1,1,2000);test.change1(date);test.change2(d1);test.change3(d2);………publicvoidchange1(inti){i=1234;}publicvoidchange2(BirthDateb){b=new