[1577] in java-interest
RE: Java learning materials
daemon@ATHENA.MIT.EDU (Nelson Yu)
Fri Sep 8 09:14:06 1995
From: Nelson Yu <nyu@gpu.srv.ualberta.ca>
To: "'java-interest@java.Eng.Sun.COM'" <java-interest@java.Eng.Sun.COM>
Date: Thu, 7 Sep 1995 21:14:32 -0600
>This is interesting, could you talk about this a little more?
>What idioms must you use in java?
The following is a _simplified_ form of the User class for sake of =
discussion. Note >> and << have been
defined for strings.
class User
{
public:
friend class UserLog;
friend ostream& operator <<(ostream &out, User& user);
friend istream& operator >>(istream &in, User& user);
private:
string strName;
string strID;
string strHost;
string strIP;
string strHTTPAgent;
string strTime;
};
ostream& operator <<(ostream& out, User& user)
{
out << user.strName << endl;
// more code here
return out;
};
istream& operator >>(istream& in, User& user)
{
char *temp =3D new char[100];
in.getline(temp,100);
user.strName =3D temp;
// A fudge since I don't know iostream that well and rather lazy to =
find out how to grab a line via
// '>>' operator
return in;
};
If you don't grab every line and just read in strings separated by =
whitespaces you get...
---- Dump ---
Unknown
Unidentified
name.somewhere.there.domain
000.000.000.000
Unknown
Browser
Thu
Sep
07
18:42:10
1995
Unknown
----------------
Also you see I just can do this..
User user;
ofstream MyFile("Notyours.log");
cout << user; OR with chaining like .. cout << user << user1 << user2;
MyFile << user AND MyFile << user << user1 << user3;
MyFile >> user;
Under Java, instead of overloaded operators(<< and >>) and the =
polymorphic ostream, I'm might need to use _just_ polymorphism.
class User
{
public void ReadUser(InputStream is)
{
strName =3D new StringBuffer();
// Read a line(better way? probably!)
if (is.Read() !=3D '\n') // Reads a byte? Half a unicode character?
{
// Reads character into the string
}
}
public void WriteUser(OutputStream os)
{
os.println(strName);
// more code
}
StringBuffer strName;
// more data here
}
Notice: 'friend' and other C++ access specifies aren't needed. I find =
C++'s style natural(oxymoron?) and Java's reverts me back to the =
low-level days of using C for string handling.
I wonder if this is legal? ReadUser(os).ReadUser(os).ReadUser(os);
ANSI C++ has 'wout' and 'win'(word out and in) in addition to 'cout' and =
'cin'(character out and in). This C++ CGI could support Unicode with a =
flick of the switch. Of course sub-standard compilers such as g++ aren't =
even close to ANSI C++, so the point maybe moot.
I don't see ANY immediate differences in programming styles and thinking =
patterns between C++ GUI libraries and Java's AWT. IMHO most templated =
C++ code could be ported to Java by using polymorphism with Object =
instead of templates, so could stuff using I/O streams, string classes, =
exception handling, Run-Time Type Information(RTTI), but applications =
using the STL *extensively* might run into problems. The STL heavily =
relies on templated functions(generic algorithms), overload =
operators(iterators) since Java does not support functions, porting =
requires you to write your own data structures and algorithms.
This link "http://www.ualberta.ca/~nyu/stl/stl.html" will provide you =
with more information about the STL if needed.
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com