现在的位置: 首页 > 综合 > 正文

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

2017年12月07日 ⁄ 综合 ⁄ 共 2720字 ⁄ 字号 评论关闭

最近需要使用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 
        v = v | (this.nDigit 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 
            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();

    }

}

抱歉!评论已关闭.