最近需要使用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();}
}
本文探讨了Java中对象和基本类型参数传递的区别,通过实例演示了Java如何通过引用传递改变对象的状态,而基本类型则保持不变。
1258

被折叠的 条评论
为什么被折叠?



