zoukankan      html  css  js  c++  java
  • 方法的参数传递机制

    记录一下 参数传递

    方法的参数传递机制:

    ①形参是基本数据类型

    • 传递数据值

    ②实参是 引用数据类型 包括(类,对象,数组...)

    • 传递地址
    • 特殊的类型:String、包装类 等对象不可变性
     1 public class ParamTest {
     2     public static void main(String[] args) {
     3         int i = 1;
     4         String str = "hello";
     5         Integer num = 200;
     6         int[] arr = { 1, 2, 3, 4, 5 };
     7         MyData my = new MyData();
     8 
     9         change(i, str, num, arr, my); // 这里的i,str,num,arr,my 为实参
    10 
    11         System.out.println("i= " + i);
    12         System.out.println("str= " + str);
    13         System.out.println("num= " + num);
    14         System.out.println("arr= " + Arrays.toString(arr));
    15         System.out.println("my.a= " + my.a);
    16     }
    17 
    18     public static void change(int j, String s, Integer n, int[] a, MyData m) { // 这里的 j,s n,a,m 为形参
    19         j += 1;
    20         s += "world";
    21         n += 1;
    22         a[0] += 1;
    23         m.a += 1;
    24 
    25     }
    26 }
    27 
    28 class MyData {
    29     int a = 10;
    30 }

    先看结果:

    i= 1
    str= hello
    num= 200
    arr= [2, 2, 3, 4, 5]
    my.a= 11

  • 相关阅读:
    【动态规划】合唱团
    【动态规划】抄近路
    【动态规划】机器人军团
    【贪心】赶作业
    【贪心】时空定位I
    【贪心】雷达问题
    【贪心】监测点
    【贪心】闭区间问题
    设计与实现
    Hello World
  • 原文地址:https://www.cnblogs.com/wanjun-top/p/12704152.html
Copyright © 2011-2022 走看看