【Java】基本類型和引用類型(值傳遞)

【關鍵詞】
【問題】
· 加深對基本類型和引用類型的理解;
【效果圖】

【分析】
  • 參見最後的【參考資料】
    【解決方式】

【代碼】
  
  
  
  
  
public void test() throws Exception { System.out.println("\nint:================="); int i = 2; System.out.println("before:" + i); change1(i); System.out.println("after:" + i);  System.out.println("\nInteger:================="); Integer integer = 3; System.out.println("before:" + integer); change2(integer); System.out.println("after:" + integer);  System.out.println("\nString:================="); String str = new String("xxx"); System.out.println("before:" + str); change3(str); System.out.println("after:" + str);   System.out.println("\nString[]:================="); String[] array = new String[]{"a", "b", "c"}; System.out.println("before:" + array[0]); change4(array); System.out.println("after:" + array[0]);  System.out.println("\nBook:================="); Book book = new Book("book1"); System.out.println("before:" + book.name); change5(book); System.out.println("after:" + book.name);  System.out.println("\nint[]:================="); int[] int_array = new int[]{1, 2, 3}; System.out.println("before:" + int_array[0]); change6(int_array); System.out.println("after:" + int_array[0]); }   public void change1(int i) { i = 4; }  public void change2(Integer integer) { integer = (Integer) 8; }  public void change3(String str) { str = new String("yyy"); }  private void change4(String[] array) { array[0] = "A"; }  private void change5(Book book) { book.name = "book2"; }  private void change6(int[] int_array) { int_array[0] = 10; }  private class Book { public String name;  Book(String name) { this.name = name; } }
【參考資料】
相關文章
相關標籤/搜索