Java学习之值传递
值传递是在一个函数中,调用另外一个函数,其中含有参数传递的情况。往往值传递就是把数据传递到另外一个函数中进行进一步处理,以更清楚地显示函数的表达意思。在函数调用的时候会有数据传递,同样要考虑形参和实参的问题,如下面的一个程序:
public class PassTest
{
float ptValue;
public void changeInt (int value)
{
value = 55;
}
public void changeStr (String value)
{
value = new String ("different");
}
public void changeObjValue (PassTest ref)
{
ref.ptValue = 99f;
}
public static void main (String args[])
{
String str;
intval;
PassTest pt = new PassTest ();
val = 11;
pt.changeInt (val);
System.out.println ("Int value is: " + val);
str = new String ("Hello");
pt.changeStr (str);
System.out.println ("Str value is: " + str);
pt.ptValue = 101f;
pt.changeObjValue (pt);
System.out.println ("Current ptValue is: " + pt.ptValue);
}
}
这个程序最后输出的结果将是:
11
Hello
99f
开始的11和Hello就是遵循参数传递的原则,虽然改变了形参的值,但是实参却不改变,这同C语言中是一样的。那么如果想改变函数中的实参的值,那么应该怎么做呢?我们知道,在C语言中,我们可以采用指针变量,用指针变量作为函数的参数,那么这样,传递的就是参数存放的地址,那么在函数中改变同一地址的参数值的时候,就不会发生错误,可是,在java中没有指针的说法,那么,要实现上述C语言的做法,应该怎么处理呢?我们知道,java中处处都是考虑对象的,句柄是用来操作对象的活动的,那么我们如果将对象的句柄传递到函数中去,就可以使两者指向同一个句柄(也就是地址),这样就可以完成数据的修改了。见上面的程序,我们可以知道,pt是主函数中创建的PassTest的对象,通过传递它,就可以完成地址的传递,子函数中,ref与主函数的pt指向的其实是一个内存单元,那么修改ref中的ptValue的值,也就是修改pt中的ptValue的值,即使没有最后的数据返回,这个数据也是会改变的。
package com.liu.stream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* OutputStream 此抽象类是表示输出字节流的所有类的超类。输出流接受输出字节并将这些字节发送到某个接收器。需要定义OutputStream
* 子类的应用程序必须始终提供至少一种可写入一个输出字节的方法。
*
* void close() 关闭此输出流并释放与此流有关的所有系统资源。 void flush() 刷新此输出流并强制写出所有缓冲的输出字节。 void
* write(byte[] b) 将 b.length 个字节从指定的字节数组写入此输出流。
* void write(byte[] b, int off,int len) 将指定字节数组中从偏移量 off 开始的 len 个字节写入此输出流。
* abstract void write(int b)
* 将指定的字节写入此输出流。
*
* 进行I/O操作时可能会产生I/O例外,属于非运行时例外,应该在程序中处理。如:FileNotFoundException, EOFException,
* IOException等等,下面具体说明操作JAVA字节流的方法。
*
*
* 写文件: 本例用System.in.read(buffer)从键盘输入一行字符,存储在缓冲区buffer中,
* 再以FileOutStream的write(buffer)方法,将buffer中内容写入文件WriteFile.txt中
*
*/
public class WriteFile {
public static void main(String[] args) {
try {
System.out.print("输入要保存文件的内容:");
int count, n = 512;
byte buffer[] = new byte[n];
// 读取标准输入流
count = System.in.read(buffer);
// 创建文件输出流对象
FileOutputStream outStream = new FileOutputStream("f://work//WriteFile.txt");
// 写入输出流
outStream.write(buffer, 0, count);
// 关闭输出流
outStream.close();
System.out.println("已保存到WriteFile.txt!");
} catch (IOException ioe) {
System.out.println(ioe);
} catch (Exception e) {
System.out.println(e);
}
}
}
package com.liu.stream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* OutputStream 此抽象类是表示输出字节流的所有类的超类。输出流接受输出字节并将这些字节发送到某个接收器。需要定义OutputStream
* 子类的应用程序必须始终提供至少一种可写入一个输出字节的方法。
*
* void close() 关闭此输出流并释放与此流有关的所有系统资源。 void flush() 刷新此输出流并强制写出所有缓冲的输出字节。 void
* write(byte[] b) 将 b.length 个字节从指定的字节数组写入此输出流。
* void write(byte[] b, int off,int len) 将指定字节数组中从偏移量 off 开始的 len 个字节写入此输出流。
* abstract void write(int b)
* 将指定的字节写入此输出流。
*
* 进行I/O操作时可能会产生I/O例外,属于非运行时例外,应该在程序中处理。如:FileNotFoundException, EOFException,
* IOException等等,下面具体说明操作JAVA字节流的方法。
*
*
* 写文件: 本例用System.in.read(buffer)从键盘输入一行字符,存储在缓冲区buffer中,
* 再以FileOutStream的write(buffer)方法,将buffer中内容写入文件WriteFile.txt中
*
*/
public class WriteFile {
public static void main(String[] args) {
try {
System.out.print("输入要保存文件的内容:");
int count, n = 512;
byte buffer[] = new byte[n];
// 读取标准输入流
count = System.in.read(buffer);
// 创建文件输出流对象
FileOutputStream outStream = new FileOutputStream("f://work//WriteFile.txt");
// 写入输出流
outStream.write(buffer, 0, count);
// 关闭输出流
outStream.close();
System.out.println("已保存到WriteFile.txt!");
} catch (IOException ioe) {
System.out.println(ioe);
} catch (Exception e) {
System.out.println(e);
}
}
}