码迷,mamicode.com
首页 > 编程语言 > 详细

【Java】基本类型和引用类型(值传递)

时间:2017-06-30 19:45:22      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:代码   reference   topic   after   http   htm   ott   art   自身   

【关键词】
【问题】
· 加深对基本类型和引用类型的理解;
【效果图】
技术分享
【分析】
  • 參见最后的【參考资料】
    【解决方式】

【代码】
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;
}
}
【參考资料】

【Java】基本类型和引用类型(值传递)

标签:代码   reference   topic   after   http   htm   ott   art   自身   

原文地址:http://www.cnblogs.com/yfceshi/p/7100210.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!