 |
Java - the C/C++ inheritance
Posted by rags on June 28, 2004 at 05:05 PM | Comments (3)
Interestingly, in many of my JavaOnes, this was the first time
I attended the chat. I was disappointed that there was no fire anywhere.
However, the energy level was very high and I enjoyed the discussions.
The JavaOne Alumni is a very robust and influential community.
Of all the questions, the one that struck me most, was the
intertwining of Java and the C/C++ language. Obviously, many
of us in the Java community did take the C/C++ route and
sometimes wish for some of the esoteric feautures of C/C++
that we miss.
When I joined Sun, I had a discussion with James Gosling
about the fact that I loved STL and when would templates be
incorporated in Java. I am glad that a much
improved version of templates i.e. generics is in Java.
However, it also reminded me of some of the pitfalls. I
quickly typed in a piece of C/C++ code.
#include <stdio.h>
main()
{
if ( (unsigned) 0 < -2)
printf("C is falling apart\n");
}
If you're able to guess the answer for this and realize
why, you may probably agree with me that the unsigned
is abused more than used appropriately.
I hope that Java continues to be relatively small, elegant and harder
to commit mistakes. Thoughts?
Rags
Bookmark blog post: del.icio.us Digg DZone Furl Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment
-
perfect example
This is, unfortunately, a perfect example of the tradeoff between the java mindset and the c++ mindset. I'm not saying one is better than the other, and maybe Java's is 'good enough' compared to c++, but regardless:
Unintended unsigned comparisons are a pitfall, yes, but they are also one of the most important optimizations known to man. One of the most common mathematical comparisons in computer science is the is-between comparison.
eg. if (x>=0 && x=0 && x<20)
then something something;
Posted by: cypherpunks on June 29, 2004 at 11:09 AM
-
unsigned will;
The example code ought to be something that a compiler should be able to test for easily. Indeed...
[tackline@localhost scratch]$ g++ -Wall unsigned.c
unsigned.c:3: warning: ISO C++ forbids declaration of `main' with no type
unsigned.c: In function `int main()':
unsigned.c:4: warning: comparison between signed and unsigned integer
expressions
For my money overflows are far worse than dubious conversions. Signed variables help in that you tend to get obviously incorrect (negative) values, even for small test data. I don't like unsigned in C/C++, but signed isn't a cure all. Within AWT/Swing, for instance, there are odd-looking expressions designed to stop wrapping. There's even cases where there is known to be a problem, but a comment states the error wont be fixed until reported.
J2SE 5.0 javac has gained -Xlint, which helps for a few common problems. I'd like to see that extended to cover the likes of x && y || z. Does that mean x && (y || z) or (x && y) || z. I'm not an SCJP, so wouldn't know.
Posted by: tackline on June 29, 2004 at 04:17 PM
-
perfect example
> That's not a small optimization, in my book
it is in mine :)
remember: hardware is cheap, and is only going to get cheaper - micro optimizations are for the JVM, not for the programmer
it possibly makes sense in the embedded assembler or C domain but in general I think the world has moved on, and even within the embedded world I know that there is some debate on the importance of microoptimizations like this (I recently went on a course where the instructor himself went over the saving a register by decrementing a value to zero, rather than incrementing up to a constant trick, and then said he didn't think it really mattered any more - this is with an ARM chip)
since moving to java integers have become a lot less useful in my code. They still appear as leaves in complex datatypes but I have many source files in which they don't appear at all
"oh but its confusing" is a valid criticism in my book too - it would be nice to say that post-millenium programming doesn't assume anything about how integers are represented inside the machine - that they are purely a mathematical abstraction and that bitwise operators are no longer allowed
its the way the world is headed imho
Posted by: asjf on June 30, 2004 at 05:56 AM
|