JAVA中的数据类型有两大类型:

      ① 基本数据类型:逻辑型(boolean)、文本型(char)、整数型(byte、short、int、long)、浮点型(float、double)

      ② 引用数据类型:类(class)、接口(interface)、数组(array).

JAVA中方法传入参数有两种方式:

      ①值传递:当方法中传入的是基本数据类型, 此时形参传递的是副本, 不改变原来的要传入的参数值

      ②址传递:当方法中传入的是引用数据类型时, 把形参和实参的指针指向了堆中的同一对象, 是址传递,会改变原来要传入的参数

demo:

1. 首先编写一个类,待会用到.

 1 package www.heima.com;
 2 
 3 public class Birthdate {
 4     
 5     private int day;
 6     private int month;
 7     private int year;
 8     
 9     public Birthdate(int day, int month, int year) {
10         super();
11         this.day = day;
12         this.month = month;
13         this.year = year;
14     }
15     
16     public void setDay(int day){
17         this.day = day;
18     }
19     
20     public void setMonth(int month){
21         this.month = day;
22     }
23     
24     public void setYear(int year){
25         this.year = year;
26     }
27     
28     public int getDay(){
29         return day;
30     }
31     
32     public int getMonth(){
33         return month;
34     }
35     
36     public int getYear(){
37         return year;
38     }
39     @Override
40     public String toString() {
41         return "Birthdate [day=" + day + ", month=" + month + ", year=" + year + "]";
42     }
43 }

2. 具体讲述:

 1 package www.heima.com;
 2 
 3 public class Testdate {
 4     
 5     public void change1(int i){
 6         i = 1234;
 7         System.out.println("i.value = "+i);
 8     }
 9     
10     public void change2(Birthdate b){
11         b = new Birthdate(22, 2, 2004);
12         System.out.println("b = "+b);
13     }
14     
15     public void change3(Birthdate b){
16         b.setDay(300);
17         System.out.println("b.hashCode = "+b.hashCode());
18     }
19     
20     public static void main(String[] args){
21         int date = 9;
22         Birthdate d1 = new Birthdate(1, 1, 2000);
23         Testdate td = new Testdate();
24         td.change1(date);
25         System.out.println("date.value = "+date);
26         td.change3(d1);
27         System.out.println("d1 hashCode = "+d1.hashCode());
28     }
29 }

 运行结果:

i.value = 1234
date.value = 9
b.hashCode = 366712642
b.value = Birthdate [day=300, month=1, year=2000]
d1 hashCode = 366712642
d1.value = Birthdate [day=300, month=1, year=2000]

从上数结果中可以看出:形参b与实参d1具有相同的hashcede, 也即具有相同的值;形参 i与实参date具有不同的值.

[注]:针对不同的方法, 调用方法的作用域不一样.

[注]:如果一个方法前面没有static, 就必须先new一个对象, 才能调用此方法.

[注]:方法调用完后,为这个方法分配的所有局部变量的内存空间消失.

posted on 2017-08-30 19:06  牧羊人的世界  阅读(425)  评论(0编辑  收藏  举报