/**
*
* 说明:本程序演示了值传递 结果为10 基本类型 内存中存的是值
*/
public class TestValue1 {
public static void main(String args[]){
int a = 10;
int b = a;
b++;
System.out.println(a);
}
}
//答案:10
//TestValue2.java
package chp6.ex09;
/**
*
* 说明:本程序演示了值传递 结果为21 对象类型 内存中存储的是地址
*/
public class TestValue2 {
public static void main(String args[]){
Student s1 = new Student();
Student s2 = s1;//s2和s1指向的是同一地址
s2.age = 21;
System.out.println(s1.age);
}
}
class Student{
int age = 20;
}
//答案:21
//TestValue3.java
package chp6.ex10;
/**
*
* 说明:本程序演示了方法中的值传递 结果为10 基本类型 内存中存的是值
*/
public class TestValue3 {
public static void main(String args[]){
int a = 10;
m1(a);
System.out.println(a);
}
public static void m1(int a){
a ++;
}
}
//答案:10
//TestValue4.java
package chp6.ex11;
/**
*
* 说明:本程序演示了的方法中的值传递 结果为21 对象类型 内存中存储的是地址
*/
public class TestValue4 {
public static void main(String args[]){
Student s = new Student();
m2(s);
System.out.println(s.age);
}
public static void m2(Student stu){
stu.age++;
}
}
class Student{
int age = 20;
}
//答案:21
this关键字
1. this关键字的概念
this是一种特殊的引用,指向当前对象
2. this的作用
a) 如果方法局部变量与实例变量命名冲突时,可以通过this属性的方式区分实例变量和局部变量
b) 如果发生一个构造方法中需要调用另一个构造方法,可以通过this()的方法调用,但this()必须书写在第一行