zoukankan      html  css  js  c++  java
  • 有关String的引用传递浅析

     1 /以下内容来源于:《java开发实战经典》书中
     2 //首先看一个例子
     3 public class RefDemo02{
     4  public static void main(String args[]){
     5   String str1 = "hello" ;   // 实例化字符串对象
     6   System.out.println("fun()方法调用之前:" + str1) ;
     7   fun(str1) ;      // 调用fun()方法
     8   System.out.println("fun()方法调用之后:" + str1) ;
     9  }
    10  public static void fun(String str2){  // 此处的方法由主方法直接调用
    11   str2 = "MLDN" ;     // 修改字符串内容
    12  }
    13 };
    14 //运行结果://  fun()方法调用之前:hello
    15 //  fun()方法调用之后:hello
    16  

     
    再来看一个例子

    class Demo{
     String temp = "hello" ;  // 此处为了方便,属性暂时不封装
    };
    public class RefDemo03{
     public static void main(String args[]){
      Demo d1 = new Demo() ; // 实例化Demo对象,实例化之后里面的temp=30 
      d1.temp = "world" ;  // 修改temp属性的内容
      System.out.println("fun()方法调用之前:" + d1.temp) ;
      fun(d1) ;
      System.out.println("fun()方法调用之后:" + d1.temp) ;
     }
     public static void fun(Demo d2){  // 此处的方法由主方法直接调用
      d2.temp = "MLDN";      // 修改temp值
     }
    };

  • 相关阅读:
    通过url在两个页面之间传值
    $.ajax数据传输成功却执行失败的回调函数
    5.26小测
    洛谷——AC记
    7.2模拟赛
    6.30模拟赛
    洛谷——每日一题
    洛谷——动态规划
    致创营
    BSGS
  • 原文地址:https://www.cnblogs.com/liwei45212/p/3028582.html
Copyright © 2011-2022 走看看