面试题代码:
1 package 方法的参数传递机制; 2 3 import java.util.Arrays; 4 5 /** 6 * @author zsh 7 * @company wlgzs 8 * @create 2019-03-27 9:37 9 * @Describe 方法的传递机制 10 * (1)形参是基本数据类型的 11 * 传递数据值 12 * (2)形参是引用数据类型的 13 * 传递地址值 14 * 特殊的类型:String、包装类等对象不可变性 15 */ 16 public class Exam4 { 17 public static void main(String[] args) { 18 int i = 1; 19 String str = "hello"; 20 Integer num = 2; 21 int[] arr = {1, 2, 3, 4, 5}; 22 MyData my = new MyData(); 23 change(i, str, num, arr, my); 24 System.out.println("i = " + i); 25 System.out.println("str = " + str); 26 System.out.println("num = " + num); 27 System.out.println("arr = " + Arrays.toString(arr)); 28 System.out.println("my.a = " + my.a); 29 } 30 31 public static void change(int i, String str, Integer num, int[] arr, MyData my) { 32 i += 1; 33 str += "world"; 34 num += 1; 35 arr[0] += 1; 36 my.a += 1; 37 } 38 } 39 40 class MyData { 41 int a = 10; 42 }
运行结果:
过程分析:
课堂重点:
原文地址:https://www.cnblogs.com/zsh-blogs/p/10605582.html
时间: 2024-10-07 05:27:02