JAVA中的數據類型有兩大類型:數組
① 基本數據類型:邏輯型(boolean)、文本型(char)、整數型(byte、short、int、long)、浮點型(float、double)ide
② 引用數據類型:類(class)、接口(interface)、數組(array).this
JAVA中方法傳入參數有兩種方式:spa
①值傳遞:當方法中傳入的是基本數據類型, 此時形參傳遞的是副本, 不改變原來的要傳入的參數值指針
②址傳遞:當方法中傳入的是引用數據類型時, 把形參和實參的指針指向了堆中的同一對象, 是址傳遞,會改變原來要傳入的參數code
demo:對象
1. 首先編寫一個類,待會用到.blog
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一個對象, 才能調用此方法.
[注]:方法調用完後,爲這個方法分配的全部局部變量的內存空間消失.