The greeting code in Java

S

Saeed Amrollahi

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Le 19/06/2011 15:44, Saeed Amrollahi a écrit :


Yes, indeed, i did a mistake.
Replace the .print( with a .println(, othewise output is not flushed and
the greeting appear only after output.
Now it OK.

Our Câºâº version *seems* simplest, but concepts behind >> and << are very
complex and can be very dangerous (overriden operator).
As you said, may be the concepts behind them are complex (I don't
agree
with you), but their usage is really simple.
And in your code, I don't no if you know the difference, but std::endl
is very very dangerous. It's not just equal to EOL but also flush the
buffer.
It's not dangerous. but my answer: use '\n' rather than std::endl;
This bad shortcut lead to very poor i/o performances in most of
applications, all outputs flushing continuously buffer instead just send
EOL.

All of those considered, Câºâº code is the shortest but notthe simplest =)
BTW, thank you for your insightful answers.
 
S

Saeed Amrollahi

What language does the beginner wish to learn?

Actually, I am going to prepare a seminar under the title: C++ vs.
Java
I try to compare C++ and Java from point of programming view.
 
S

Saeed Amrollahi

Sun, 19 Jun 2011 06:06:17 -0700 (PDT), /Saeed Amrollahi/:
     public static void main(String[] args) throws IOException {
         System.out.print("Please enter your first name: ");
         String name = new String();
         Reader r = new InputStreamReader(System.in);
         for (char ch; (ch = (char)(r.read())) != '\n'; name += ch) {}
         System.out.println("Hello, " + name);
     }
What are the problems of my code and how can I write
a better one. Please throw some light.

You don't need to initialize 'name' with a 'new String()' - just
assign it with an empty string literal which is a constant:

         String name = "";
You are right.
 
A

Aéris

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Le 19/06/2011 21:06, Saeed Amrollahi a écrit :
I try to compare C++ and Java from point of programming view.

In this case, do not just compare language each others, but also related
tools.

Maven or Graddle are must have in the Java World, or other tools like
Jenkins, Sonar?

Java is not just a language, it's a World. It most important part is
also community and tools.

- --
Aeris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJN/lbIAAoJEK8zQvxDY4P9kyAH/002cDmNtahTv9VsSw0V7W3T
OzUTF9QGHZuuhjiO0dpfStgt0CsQsr+LdueRL1yqDxSBFlMjunvAoF3/2J1fQ10j
LdZkpQ1zYP9X5DXmuzMFmYILhtwCX2Q9HS2Mt+igs2zVrhDyQvSP2wArfxKxTsSD
XTrQ1j+pgMuOuVejxHOr6/oWeLjhCDKV4taMcm+F9uwp5Ze7q2T5ZnOi6tF5sMvB
9WYemCtfni2TPDtmklqwyPMBsMmzUJT10G23unlmXGxmh33XMglxEkgNltQk11AF
acceSLsNI9YMeTvvEkejCxae1Ok57nruEPl2jkWrAagjc5/OxEW3rLjXLxc+m18=
=sq/+
-----END PGP SIGNATURE-----
 
J

Joshua Cranmer

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!
 
S

Stefan Ram

Joshua Cranmer said:
C++ stack constructed-objects, references, #include (trust me, beginners
can get very confused about #include, as simple as it looks), and

Well, in my own classes, the explanation I give is this:

1. When a program uses certain names, it also needs to
have a corresponding #include line at the start.
For example, when a program uses »::std::cout«,
»#include <iostream>« is required.

2. Here is a list of names and the #include-lines needed:

::std:setprecision #include <iomanip>
::std:fmtflags #include <ios>
::std:boolalpha #include <ios>
::std:streamsize #include <ios>
::std:eek:stream:precision #include <ios>
And, naturally, any talk of C++ being simpler becomes hard to stomach

To explain the program, one would need to explain how the
operator << can be combined to the expression with two <<'s
(this has to do with the value of the operation, but then
one also needs to explain while in »if( ::std::cout < 2 )«
it has a totally different value), but that there is no
sequence points, so that one has to be careful with side
effects and how actually the operator << is resolved
although it is not defined in the global namespace, which is
only made possible by ADL, which many experts have
difficulties to explain, because its rules are quite
complicated, then also, how << is overloaded for different
argument types, and why the statement
::std::cout << 4<2 << '\n';
gives and error, but why the expressions
4 << 2
is possible, although »4« is no stream at all.

The new C++ standard has more than 1300 pages, but this is
base on the C standard with more than 500 pages. These are
nearly 1900 pages of a text in a condensed technical
language, yet the language does not allow to access a
directory of the filesystem or a socket of the network.
 
J

Joshua Cranmer

The new C++ standard has more than 1300 pages, but this is
base on the C standard with more than 500 pages. These are
nearly 1900 pages of a text in a condensed technical
language, yet the language does not allow to access a
directory of the filesystem or a socket of the network.

In all fairness, the first 200 pages or so of the C standard are
language semantics, which C++ redefines for itself anyways. It's the
library and a few of the annexes that are important for C++ (so about
300 pages, give or take). Of course, you might want to add for some more
information specs like IEEE 754 (~70 pages) if you want to know more
information about floating points.

The JLS itself is 684 pages, compared to the ~450 pages that C++ takes
to explain its language. The C++0x library takes around 800 pages to
explain the rough equivalent to large hunks of java.lang and java.util.

On the other hand, the JLS is a lot more explicit and burns through
space with examples, so it is relatively low in information density for
a specification. The standard Java API is also probably the best example
of API documentation in widespread usage.
 
S

Stefan Ram

Supersedes: <[email protected]>

Joshua Cranmer said:
C++ stack constructed-objects, references, #include (trust me, beginners
can get very confused about #include, as simple as it looks), and

Well, in my own classes, the explanation I give is this:

1. When a program uses certain names, it also needs to
have a corresponding #include line at the start.
For example, when a program uses »::std::cout«,
»#include <iostream>« is required.

2. Here is a list of names and the #include-lines needed:

::std::setprecision #include <iomanip>
::std::fmtflags #include <ios>
::std::boolalpha #include <ios>
::std::streamsize #include <ios>
::std::eek:stream:precision #include <ios>
And, naturally, any talk of C++ being simpler becomes hard to stomach

To explain the program, one would need to explain how the
operator << can be combined to the expression with two <<'s
(this has to do with the value of the operation, but then
one also needs to explain while in »if( ::std::cout << 2 )«
it has a totally different value), but that there is no
sequence points, so that one has to be careful with side
effects and how actually the operator << is resolved
although it is not defined in the global namespace, which is
only made possible by ADL, which many experts have
difficulties to explain, because its rules are quite
complicated, then also, how << is overloaded for different
argument types, and why the statement
::std::cout << 4<2 << '\n';
gives and error, but why the expressions
4 << 2
is possible, although »4« is no stream at all.

The new C++ standard has more than 1300 pages, but this is
base on the C standard with more than 500 pages. These are
nearly 1900 pages of a text in a condensed technical
language, yet the language does not allow to access a
directory of the filesystem or a socket of the network.
 
N

Nasser M. Abbasi

Nasser M. Abbasi said:
name = InputString["Name?"]
Print["Hello "<>name];

... and to do this in JEE, just follow precisely these
50 pages:

http://download.oracle.com/javaee/6/firstcup/doc/firstcup.pdf

(Ok, actually it reads the birthday date of the user and
then outputs two lines of info about the age of the user,
but this is still similar in spirit to the above program.)

Well, I guess the above is a good example of why you Java
programmers get the big bucks !

--Nasser
 
S

Saeed Amrollahi

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Le 19/06/2011 21:06, Saeed Amrollahi a écrit :


In this case, do not just compare language each others, but also related
tools.

Maven or Graddle are must have in the Java World, or other tools like
Jenkins, Sonar?

Java is not just a language, it's a World. It most important part is
also community and tools.
Actually C++ is just a language not a complete system.
Java is a complete system not just a language.
For this reason, in Java we have to mention the name tools,
BTW, thank you for the name of some tools, except Maven I hadn't
hear about others.
-- Saeed Amrollahi
 
S

Stefan Ram

Joshua Cranmer said:
In the hq9ni language, it's even easier:

I'd say that the language must have had either a published
specification or a published implementation before this here
thread started.

(Otherwise, one can define every Java program to be the
/name/ of a programming language, which has only one
program, the empty program of length 0, whose behavior is
defined to be as its name interpreted as a Java program.)
 
R

RedGrittyBrick

I am going to prepare a seminar under the title: C++ vs.
Java
I try to compare C++ and Java from point of programming view.

Most people don't write "Hello" programs, so comparing languages by
counting lines of source code for "Hello" - doesn't really tell you much.

perl -e 'print "Hello $ARGV[0]\n"' Fred
Hello Fred

ksh -c 'print Hello $0' Fred
Hello Fred

I hope you would not think it relevant to conclude that Goldman Sachs,
Microsoft and Notch writing share-trading systems, word processors and
video-games will find it easier to learn Korn shell and not Java, C or C++?
 
J

Jeff Higgins

Actually, I am going to prepare a seminar under the title: C++ vs.
Java
I try to compare C++ and Java from point of programming view.

You seem sincere but I can't help wonder:
Who is the intended audience? What is the goal?
Hopefully it's not a usenet seminar posted to both
C++ and Java groups. That's been done, and is a messy format.
 
J

Jeff Higgins

You seem sincere but I can't help wonder:
Who is the intended audience? What is the goal?

I can almost see a serial type format:

Java: Accounts of My Explorations.
1. Hello
2. Crossing Streams
3. Buildings
....
 
B

blmblm

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Le 19/06/2011 15:44, Saeed Amrollahi a écrit :

[ snip ]
And in your code, I don't no if you know the difference, but std::endl
is very very dangerous. It's not just equal to EOL but also flush the
buffer.
This bad shortcut lead to very poor i/o performances in most of
applications, all outputs flushing continuously buffer instead just send
EOL.

Huh. There's actually an explicit "flush" in the implementation?
Interesting. How did you find this out? I'm not a C++ expert but
had not come across a mention of this detail.

Of course, if writing prompts to stdout with the implicit assumption
that the program is being run interactively, flushing the buffer is
what one wants.

Just sayin'.
 
B

blmblm

For a beginner, learning to write C in Java is unhelpful.

Even in C one would not be likely to input a string by reading one
charaacter at a time -- in a beginner program at least.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top