[1579] in java-interest
RE: Java learning materials
daemon@ATHENA.MIT.EDU (Nelson Yu)
Fri Sep 8 10:16:09 1995
From: Nelson Yu <nyu@gpu.srv.ualberta.ca>
To: "'java-interest@java.sun.com'" <java-interest@java.sun.com>
Date: Fri, 8 Sep 1995 05:13:06 -0600
Todd,
>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;
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 and popular compilers such =
as g++(and many, many others) aren't even close to ANSI C++, so the =
point maybe moot.
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
}
}
// I wonder if this is legal? ReadUser(os).ReadUser(os).ReadUser(os);
public void WriteUser(OutputStream os)
{
os.println(strName);
// more code
}
StringBuffer strName;
// more data here
}
Note: '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.=20
Things I've noticed:
1) I use the classes in same manner
2) Similar design for both C++ and Java classes
3) Most C++ classes(I've tried) port painlessly over to Java. (i.e =
Environment, UserLog, HTMLPage, etc..)
My half baked Forms classes looks like there won't be any problems in =
port either.
// I bet the following is not the way to set a string to a new value - =
Note: strServerName has already been
// initialized in the constructor
class Environment
{
public void Initialize()
{
// Server related
strServerName.insert(0,System.getenv("SERVER_NAME"));
nServerPort.parseInt(System.getenv("SERVER_PORT"));
// more code here
}
}
// versus...
void Environment::Initialize()
{
// Server related
strServerName =3D getenv("SERVER_NAME");
nServerPort =3D atoi(getenv("SERVER_PORT"));
}
4) As you see most of my code is just removing C++'s overloaded =
operators and replacing it by methods per se, its just not always =
obvious to me being fairly new to Java.
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 esoteric features of the STL *extensively* might run into =
problems. The STL heavily relies on templated functions(generic =
algorithms), overload operators(iterators) but since Java does not =
support functions nor usually have suitable alternatives for STL's data =
structures and algorithms, porting requires you to write your own.
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com