【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;
}
}
【參考资料】
posted @ 2017-06-30 18:27  yfceshi  阅读(5380)  评论(0编辑  收藏  举报