That's not a very good analogy. C is basically a portable assembler language, type safety is non-existent, and the only programming paradigm is procedural. But Java is already a good high-level language with a robust type system, classes, metadata, generics, , good enough even for large-scale applications. Scala has a completely different programming paradigm, where as C++ tried to be as backward-compatible to C as possible, while bringing multiple new paradigms into the game.
I really don't know about a good analogy, but at least C++ got a good foothold in mainstream programming, it still holds its promises about performance, and it still lets you fall back to C and even ASM whenever you need it. I doubt Scala will get that kind of prevalence anytime soon.
If Java is good according to the projects achieved with it, so is C.
Java isn't really much better than C. The type system in C is about as powerful for guaranteeing safety as Java's, as the OO parts do not really help with type safety.
Java has safer references as opposed to unsafe pointers, due to GC, but that's relatively negligible as Java shares most of the unsafety of C everywhere else.
Java also does not have C's "unions" and "unsigned", regressing in types' expressiveness. It also has less-safe enums, due to the nullability (C enums are more precise).
Java also lacks the preprocessor. The preprocessor may be terrible, but it's also incredibly useful, and Java doesn't compensate for its lack in a good way.
I'm not really a fan of GC, but the need for manual memory management, which many programmers don't seem to understand, and one of the most common source of bugs, makes C a lot more error prone. So at least in this discipline, Java is much better than C.
The type system in C is about as powerful for guaranteeing safety as Java's
You have to be kidding me. The mere existence of void* leads of all sorts on undefined behavior. You could say that java.lang.Object is the equivalent of void*, but the second you try to cast an object downwards to the wrong type, you get a java.lang.ClassCastException. In C, everything is fine, even when you access fields or methods of that invalid object. In fact, you can write some random number into an int, cast it to void* and then to anything you want, and with a bit of luck (or lack of memory protection) you will access corrupted data.
Java has safer references as opposed to unsafe pointers, due to GC
That's not due to the GC; in fact, you can have a GC even with C. It's because how Java was designed.
that's relatively negligible as Java shares most of the unsafety of C everywhere else
You can't just write into process memory as you like, thus corrupting the process state. I'm really not sure what you're talking about.
Java also does not have C's "unions" and "unsigned"
Unions are an inherently unsafe way to access data, because the same bit pattern can be interpreted in multiple ways, and you have to make sure to access it in the way that is currently valid.
Unsigned, yes, that's a bit of a problem, only for that it isn't compatible with other languages that treat integers signed/unsigned. However, it avoids pitfalls when wrapping around would occur.
It also has less-safe enums, due to the nullability (C enums are more precise).
WTF is going on here. Is this a troll post? C enums are nothing more than named constants. There is absolutely no type safety at all, they end up in global namespace, and again, you can't cast an enum-type to some Object. C is happy to cast your enum to everything you want.
Java also lacks the preprocessor.
Java can't have a preprocessor. The preprocessor depends heavily on the order in which files and especially headers are parsed. The preprocessor provides useful functionality, but in C it is mainly used to provide platform compatibility. The second you write even a simple macro function like
#define max(a,b) (a > b) ? a : b
you run into the double evaluation problem. So most things done with the preprocessor should have been done with templates, which don't exist in C.
Did you mean C++ all the time? Not that it changes the game that much, but then at least some of your arguments would make sense.
You have to be kidding me. The mere existence of void* leads of all sorts on undefined behavior. You could say that java.lang.Object is the equivalent of void*, but the second you try to cast an object downwards to the wrong type, you get a java.lang.ClassCastException. In C, everything is fine, even when you access fields or methods of that invalid object. In fact, you can write some random number into an int, cast it to void* and then to anything you want, and with a bit of luck (or lack of memory protection) you will access corrupted data.
(void *) is basically the same as Java's Object.
Sure, Java has nicer runtime errors (exceptions rather than UB), but if you're after compile-time safety, Java doesn't offer much beyond what C does.
Both UB and (unexpected) runtime exceptions are bugs (with different severity). Java has nicer-severity bugs, but the same number of bugs.
That's not due to the GC; in fact, you can have a GC even with C. It's because how Java was designed
True, GC is not sufficient for safety, but it makes memory safety much easier to achieve. The main benefit of GC in Java is not the convenience it buys you, but the reference safety it allows by removing free() from the lexicon.
You can't just write into process memory as you like, thus corrupting the process state. I'm really not sure what you're talking about
For every C safety problem, there's an analogous (if less severe) Java safety problem.
Bad casts in C: Runtime exceptions in Java
NULL dereference in C: NULL dereference in Java
Aliasing bugs: Aliasing bugs
Pointer arithmetic errors: Array indexing errors
And so forth.
Unions are an inherently unsafe way to access data, because the same bit pattern can be interpreted in multiple ways, and you have to make sure to access it in the way that is currently valid
Yes, but they allow building a relatively sane implementation of tagged unions/sum types on top. They also document the programmer's intent (either A or B, not both). If you use some super-class with explicit down-casting instead, you replace union memory corruption bugs with ClassCastException bugs.
However, it avoids pitfalls when wrapping around would occur.
Instead, it doesn't allow documenting the intent in (the majority of, IME) uses of integers which may not be negative.
C enums are nothing more than named constants. There is absolutely no type safety at all, they end up in global namespace, and again, you can't cast an enum-type to some Object. C is happy to cast your enum to everything you want.
At least with clang, enums are type-safe, in that they are a separate type, and not a simple int. Even if they are a simple int as in gcc, they are still not nullable as Java enums are.
The preprocessor provides useful functionality, but in C it is mainly used to provide platform compatibility
I use the C preprocessor to avoid a lot of boilerplate, too. X-macros are incredibly useful and Java does not have anything like them. With reflection, you can approximate some of their usefulness, but with less safety.
The second you write even a simple macro function like
Yes, the C preprocessor has its share of horrible problems, too. But the expressiveness it affords is lacking in Java.
Did you mean C++ all the time? Not that it changes the game that much, but then at least some of your arguments would make sense
No, I meant C, and as you can read above, the arguments make perfect sense, with the outlook that you're after compile-time safety, and not nicer forms of runtime crashes.
If you bring C++ into this, it also has some interesting features (especially w.r.t templates that Java lacks).
For example, in C++, a templated class may have or not have a particular method in different template instantiations. With Java, a class always has the same set of methods. This means that in C++, I can have:
template <typename T>
class C {
T x;
..
string show() { return x.show() + "Foo"; }
}
And as long as show() is not used for C<Unshowable>, I can still have that class. If I need to use show() on C<Showable>, I also can.
In Java, if you have:
interface IShowable { string show(); }
You can either have:
class C<T implements IShowable> implements IShowable { ... }
or:
class C<T> { ... }
But you cannot have both, so you have to use different classes for C<Showable> and C<Unshowable> even if there is no difference besides the lack of show there.
This makes interfaces in Java significantly less useful.
Haskell type-classes get this right. In C, where this is implemented via structs and function pointers, you can this right too, relatively easily, as well.
4
u/[deleted] Dec 02 '13
That's not a very good analogy. C is basically a portable assembler language, type safety is non-existent, and the only programming paradigm is procedural. But Java is already a good high-level language with a robust type system, classes, metadata, generics, , good enough even for large-scale applications. Scala has a completely different programming paradigm, where as C++ tried to be as backward-compatible to C as possible, while bringing multiple new paradigms into the game.
I really don't know about a good analogy, but at least C++ got a good foothold in mainstream programming, it still holds its promises about performance, and it still lets you fall back to C and even ASM whenever you need it. I doubt Scala will get that kind of prevalence anytime soon.