: : 3- Templates in C++ are error prone and soo complicated comparing to
: : Java Object type
:
: The Java Object type is not something to brag about. Both C++ and C#
: have better ways.
:
: C++ templates accepts primitive data types, Java Objects don't, you
: need to use real ugly wrapper classes to even be able to pass common
: integers, since Java isn't a pure object-oriented language, unlike
: C# where everything's objects.
:
: As for C++ templates, they are complicated, but way more powerful
: once you have them working. Take this example:
:
:
: ifstream fin("dummy.txt");
: if(!fin.is_open())
: {
: /* error handling */
: }
: istream_iterator<SomeType> fin_it (fin), end;
: ostream_iterator<SomeType> cout_it(cout, "\n");
: copy(fin_it, end, cout_it);
: fin.close();:
:
: This opens a file containing custom "SomeType" objects, reads them
: and prints them all to the screen in a type-specific manner. In 6
: lines of code. Need another i/o source? Change one argument to the
: constructors. Need another type? Change the template type. Add
: another copy() line and you can get the data inside any container
: type of your choise, sorted in any way you like.
:
: Hardly complicated, though the complexity is hidden among operator
: overloading, function objects etc, and those are things that Java
: lacks, for good and bad.
:
:
: : 4- pointer in C++ are not safe as java's pointers
:
: Java references are very much unsafe, since you don't explictly
: write that you are passing the argument by reference. They expect
: that everyone simply knows that, and thinks of it when they are
: writing a function. So if you step in straight from C/C++, Pascal or
: whatever and start writing Java, you will almost certainly trash a
: fair amount of function parameters since they look as if they were
: passed by value. Or forgetting to compare the passed parameter with
: "this" when writing an "equals" function.
:
:
: If you want to preach Java over C++, you should mention inheritage,
: inferfaces, intuitive OO design etc, because that is where Java gets
: the real advantage, being way more clean and logical. And of course
: the platform independance, as well as and the Java applets that can
: run from any modern web browser.
:
Here's the Java object reader and printer, for those who are interested:
ObjectInputStream ois = ObjectInputStream(new FileInputStream("dummy.txt"));
Object o = ois.readObject();
while (o != null) {
System.out.println(o.toString());
}
o.close();
This works for all objects, which implement the Serializable interface.