Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Java, PipedWriter/Reader similar to C pointers?

Java, PipedWriter/Reader similar to C pointers?
Thread Tools
Professional Poster
Join Date: Oct 2001
Status: Offline
Reply With Quote
Jul 15, 2004, 04:55 PM
 
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
Reply With Quote
Jul 15, 2004, 05:45 PM
 
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
Reply With Quote
Jul 15, 2004, 06:23 PM
 
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
Reply With Quote
Jul 15, 2004, 09:10 PM
 
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
Reply With Quote
Jul 16, 2004, 05:12 AM
 
@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
Reply With Quote
Jul 16, 2004, 08:40 AM
 
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
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 01:03 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2