java传参的值传递,引用传递

最近需要使用java,发现java并未像c++那样专门通过某些符号来明确参数的值传递和引用传递,而是统一采用引用传递。

例如有如下代码:

static void testKFloatChange(KFloat f1, KFloat f2){
    f1.nValue = 25;
    f1.nDigit = 1;
    f1.nUnit = 0;
    
    f2 = f1;
}

public static void main(String[] args) {
    
    KFloat kf1 = new KFloat(3, 2, 0);
    KFloat kf2 = new KFloat(432, 2, 0);
    System.out.println("before test");
    System.out.println(String.format("kf1 = %s, kf2 = %s", kf1.toString(), kf2.toString()));
    testKFloatChange(kf1, kf2);
    System.out.println("after test");
    System.out.println(String.format("kf1 = %s, kf2 = %s", kf1.toString(), kf2.toString()));
    
    
}

打印出的结果如下:

before test
kf1 = 0.03, kf2 = 4.32
after test
kf1 = 2.5, kf2 = 4.32

 

kf1内部被改变,因此值由0.03变为4.32

而kf2虽然被重新赋值,实际上相当于重新被生成了一个KFloat,因此原先的kf2不被改变。

因此,对于内置的变量,例如int,long,byte,显然内部状态是不会被改变的,因此它们看起来像是值传递。

 

经此发现提醒我们,在以类实例为参数进行传递时,需要特别注意其内部状态是否发生了改变,以防不可预料的后果。

 

附KFloat代码:

package com.szkingdom.commons.mobileprotocol.util;
 
/**
 * 

 * Title: 一个整数模拟浮点数的封装
 * 

 * 

 * Description: KJava 平台
 * 

 * 

 * Copyright: Copyright (c) 2003
 * 

 * 

 * Company:
 * 

 * 
 * @author not attributable
 * @version 1.0 备忘:当把KFloat作为计算器时,不能在KFloat变量之间进行赋值‘=’,切记切记!
 */
public class KFloat {
    public final static int NUMBER = 0; // 单位(个)
    public final static int TEN_THOUSAND = 1; // 单位(万)
    public final static int HUNDRED_MILLION = 2; // 单位(亿)
    // public static KFloat sub = new KFloat();
    public long longVlaue;
    public int nValue; // 实际数值
    public int nDigit; // 小数点位数
    public int nUnit; // 单位
    private int format;//
 
    public KFloat() {
        this.nValue = 0;
        this.nDigit = 0;
        this.nUnit = NUMBER;
    }
 
    public KFloat(int nFloat) {
        this.nUnit = (nFloat & 0x00000003);
        this.nDigit = ((nFloat & 0x0000000C) >> 2);
        this.nValue = (nFloat >> 4);
    }
 
    public KFloat init(int nFloat) {
        this.nUnit = (nFloat & 0x00000003);
        this.nDigit = ((nFloat & 0x0000000C) >> 2);
        this.nValue = (nFloat >> 4);
        return this;
    }
 
    public int float2int() {
        int v = (this.nValue << 4) & 0xFFFFFFF0;
        v = v | (this.nDigit << 2) | this.nUnit;
        return v;
    }
 
    /**
     * 
     * @param nValue
     * @param nDigit
     *            小数点位数
     * @param nUnit
     *            单位
     */
    public KFloat(int nValue, int nDigit, int nUnit) {
        this.nValue = nValue;
        this.nDigit = nDigit;
        this.nUnit = nUnit;
    }
 
    public void init(int nValue, int nDigit, int nUnit) {
        this.nValue = nValue;
        this.nDigit = nDigit;
        this.nUnit = nUnit;
        // return this;
    }
 
    public KFloat(KFloat kFloat) {
        this.nValue = kFloat.nValue;
        this.nDigit = kFloat.nDigit;
        this.nUnit = kFloat.nUnit;
    }
 
    public int getDigitBase() {
        int nBase = 1;
        for (int i = 0; i < this.nDigit; i++) {
            nBase *= 10;
        }
        return nBase;
    }
 
    public String toString(String ch) {
        format = ch != null ? 1 : 0;
        String s = toString() + ch;
        format = 0;
        return s;
    }
 
    @Override
    public String toString() {
        if (format == 0 && nValue == 0)
            return "---";
        StringBuffer strRtn = new StringBuffer(12);
        strRtn.append(this.nValue);
        int nLen = strRtn.length();
        int nPos = 0;
        if (this.nValue < 0) {
            nPos = 1;
        }
        if (this.nDigit > 0) {
            if ((nLen - nPos) > this.nDigit)
                strRtn.insert(nLen - this.nDigit, '.');
            else {
                strRtn.insert(nPos, "0.");
                while ((nLen - nPos) < this.nDigit) {
                    strRtn.insert(nPos + 2, '0');
                    nLen++;
                }
            }
        }
        switch (this.nUnit) {
        case TEN_THOUSAND: // 单位(万)
            strRtn.append("万");
            break;
        case HUNDRED_MILLION: // 单位(亿)
            strRtn.append("亿");
            break;
        default:
            break;
        }
        return strRtn.toString();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

永远的麦田

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值