Re: The greeting code in Java
On 06/19/2011 10:17 AM, Saeed Amrollahi wrote:
Well, in the case of C++ code, I have to
explain cin, cout, string and two overloaded operators:>> and<<
in the case of java one: I have to
explain, the stream class hierarchy, InputStreamReader,
BufferedReader,
in, string, new operator, final, the meaning of buffered,
readLine, ...
If that is what you're explaining in Java, you also need to explain for
C++ stack constructed-objects, references, #include (trust me, beginners
can get very confused about #include, as simple as it looks), and
probably the entire iostream interfaces if you're talking about Java's
stream class hierarchy.
I believe the C++ code is simpler for a beginner.
If it's simpler, it's only superficially so. C++'s interfaces heavily
optimize for the most common case and render the development of the more
critical components inordinately more difficult. For example, a fair
amount of my work has to worry about the use of international characters
and multiple charsets (it's not unimaginable that I have to use one
stream to write in two simultaneous charsets (i.e., the control protocol
is in UTF-8 but I send 8-bit MIME payloads of ISO 8859-1)). In Java, I
can keep both the raw Stream and a controlling Reader/Writer around and
use the character-based one for control information and the octet-based
one for data payloads.
And, naturally, any talk of C++ being simpler becomes hard to stomach
once you introduce templates, i.e., any use of the STL. std::string
isn't actually a class, it's actually std::basic_string<char,
std::char_traits<char>, std::allocator<char> >, as every error message
involving std::set<std::string> will helpfully inform you. I mean
std::set<std::basic_string<char, std::char_traits<char>,
std::allocator<char> >, std::less<std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >,
std::allocator<std::basic_string<char, std::char_traits<char>,
std::allocator<char> > > >. Have fun debugging your template errors!
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth