private static void readLine(byte line[])throws IOException //这里就在读phone和name啊
解决方案 »
- 如何使 JTable 充满 JScrollPane
- 奇怪的数组对象输出。
- 求java中关于数据库操作的类及方法
- 这是什么错误 the literal octal 08 (digit 8) of type int out of range
- java一个简单问题。我是新手
- 一些关于JTable应用的问题。请大家帮忙来看看
- 怎样装ORACEL的JDBC驱动.我用的是JAVA 的SDK包在调程序.
- 对面的牛人看过来,看过来,这里一个paint小事件,小时间
- 请问JAVA的开发环境是什么? 那里有下载?
- Java-Iterator的三个方法
- 涉及到“生产者-消费者”的 数据库存取 问题设计!(有结果比给分)
- 一个Applet简单问题,快来抢分啊!
readLine(name),这时因为name在内存中是唯一的static值,所以在传name进去的时候,穿进去的也是它的唯一引用,自然readLine(byte line[])中对line数组赋值的时候,也就是对name赋值,那么name和phone也就有了值
byte[] name=new byte[lineLength];在静态的readLine中对byte[]的改变,直接反映在Reference上面
class phones{
static FileOutputStream fos;
public static final int lineLength=81;
byte[] phone=new byte[lineLength];
byte[] name=new byte[lineLength];
public static void main(String args[])throws IOException{
int i;
fos=new FileOutputStream("phone.numbers");
phones p = new phones();
while(true){
System.err.println("Enter a name(enter 'done' to quit)");
p.readLine(p.name);
if ("done".equalsIgnoreCase(new String(p.name,0,0,4))){
break;
}
System.err.println("Enter the phone number");
p.readLine(p.phone);
for (i=0;p.phone[i]!=0;i++){
fos.write(p.phone[i]);
}
fos.write(',');
for (i=0;p.name[i]!=0;i++){
fos.write(p.name[i]);
}
System.out.println(new String(p.name));
fos.write('\n');
}
fos.close();
}
private void readLine(byte line[])throws IOException{
int i=0,b=0;
while((i<(lineLength-1))&&((b=System.in.read())!='\n')){
line[i++]=(byte)b;
}
//line[i]=(byte)(0);
}
}
我把代码改成这样以后,运行结果和以前一样
import java.io.*;
class phones{
static FileOutputStream fos;
public static final int lineLength=81;
byte phone ;
byte name;
public static void main(String args[])throws IOException{
int i;
fos=new FileOutputStream("phone.numbers");
phones p = new phones();
while(true){
System.err.println("Enter a name(enter 'done' to quit)");
p.readLine(p.name);
if (1 == p.name){
break;
}
System.err.println("Enter the phone number");
p.readLine(p.phone);
// for (i=0;p.phone[i]!=0;i++){
// fos.write(p.phone[i]);
// }
System.out.println(p.phone); }
fos.close();
}
private void readLine(byte line)throws IOException{
int i=0,b=0;
while((i<(lineLength-1))&&((b=System.in.read())!='\n')){
line=(byte)b;
}
//line[i]=(byte)(0);
}
}
假如你使用byte的话,你永远也不能退出了
那对象引用呢,比如ClassA a = new ClassA()
ClassA b = new ClassA();a和b也是同一个地址空间吗
ClassA b = new ClassA();这个不是同一地址空间,new的时间a和b分配的不同