 |
 |
Java, PipedWriter/Reader similar to C pointers?
|
 |
|
 |
|
Professional Poster
Join Date: Oct 2001
Status:
Offline
|
|
I'm slowly making progress in learning Java, and have come to a point in the tutorial where I'm a little confused.
Java passes all objects/variables by value (creating a new copy), except for arrays and static variables. But I'm here at a tutorial that uses PipedWriter/Reader to manipulate text between threads without making extra copies. These pipes are first wrapped in classes like PrinterWriter that normally are just passed by value. Then this PrintWriter object is shipped off to a new location.
So are all these "pipes" simply Java's way of using pointers? And do they have to be used between threads, or can by also be used between methods?
(Last edited by itistoday; Jul 15, 2004 at 05:15 PM.
)
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Feb 2004
Status:
Offline
|
|
Originally posted by itistoday:
Java passes all objects/variables by value (creating a new copy), except for arrays and static variables.
Java passes only primitive types (byte, short, int, long, float and double - have I forgotten something?) by value and everything else (i.e. Objects) by reference (even primitive arrays are Objects*).
Code:
public void method1() {
ArrayList list = new ArrayList();
helloworld(list);
System.out.println(list.toString());
}
public void helloworld(ArrayList list) {
list.add("Hello World");
}
You can certainly use Pipes between methods.
*check this out, a C pointer simulation.
Code:
public void doSomething() {
int[] result = new int[1]; // <- Pointer!
int a = 5;
int b = 9;
add(a, b, result);
System.out.println(a + " + " + b + " = " + result[0]);
}
public void add(int a, int b, int[] result) {
result[0] = a + b;
}
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Oct 2001
Status:
Offline
|
|
Heh, that's pretty cool. I guess I forgot that Objects are passed by reference, that's a pretty important thing to know there.. *doh!*
So I guess I'm confused as to the reason for using the PipedReader/Writer classes?
|
|
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
Originally posted by bone666:
(byte, short, int, long, float and double - have I forgotten something?)
There's also char and boolean. Just for completeness.
|
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Feb 2004
Status:
Offline
|
|
@Chuckit: thanks
Pipes are very rarely used, forget them and when you need them you'll understand why! I had once to write a control system for a märklin model railway at the Uni which was connected to a PC via a serial port and needless to say I had neither a model railway at home nor a serial port on my Mac. I wrote a virtual serial port based on SUN's javacomm API and Pipes, a Task which simulated the model railway and connected the Input- and OutputStreams with the virtual serial port with each other. The control system thought all the time it communicates with a real serial port... The only downside was that reality and simulation sometimes did not match, it was fortunately not a real railway.
Code:
public class Pipes {
public static void main(String[] args) throws java.io.IOException {
java.io.PipedReader in = new java.io.PipedReader();
java.io.PipedWriter out = new java.io.PipedWriter();
out.connect(in);
for(int i = 0; i < 255; i++) {
out.write(i); // write into one side
System.out.print((char)in.read()); // and read from the other side
}
System.out.println();
}
}
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Oct 2001
Status:
Offline
|
|
Originally posted by bone666:
@Chuckit: thanks
Pipes are very rarely used, forget them and when you need them you'll understand why! I had once to write a control system for a märklin model railway at the Uni which was connected to a PC via a serial port and needless to say I had neither a model railway at home nor a serial port on my Mac. I wrote a virtual serial port based on SUN's javacomm API and Pipes, a Task which simulated the model railway and connected the Input- and OutputStreams with the virtual serial port with each other. The control system thought all the time it communicates with a real serial port... The only downside was that reality and simulation sometimes did not match, it was fortunately not a real railway.
Code:
public class Pipes {
public static void main(String[] args) throws java.io.IOException {
java.io.PipedReader in = new java.io.PipedReader();
java.io.PipedWriter out = new java.io.PipedWriter();
out.connect(in);
for(int i = 0; i < 255; i++) {
out.write(i); // write into one side
System.out.print((char)in.read()); // and read from the other side
}
System.out.println();
}
}
Ok thanks, that's good to know. One less thing to worry about 
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|