I want to Compare C to Java

Yep, you heard me right. I want to compare C to Java (not C++)

Does anyone have any good references, or opinions they’d like to share?

Thanks

E3

Aside from superficial syntactical similarities, there isn’t much to compare.

Java is an object-oriented language, C is not. (Arguments over what constitutes “true” OO-ness is a topic for GD, but it’s reasonable to say that Java is a lot more OO than C is.)

Java is compiled to run on a virtual machine, C is compiled for specific hardware. (Which may or may not be a virtual machine. Usually not.)

C makes heavy use of pointers, Java does not.

C does not have any built in inherritence, polymorphism, or namespace protection, but these things can be emulated if desired.

C does not have any automatic garbage collection, Java does.

C is my favorite language, and I hate Java with the fire of ten thousand suns.

I program in both C and Java, and I agree with everything friedo said, although I have warmed to Java. I still love C, but I like Java for windows programming better than C++.

I started to write a description of what sorts of applications each of these is best suited for, and wrote probably a good page or two, only to realize that they’re both pretty general. So I deleted that disertation, except to say that if you want to write drivers or other low level stuff, C’s your boy. And for complex web-based apps, J2EE is a pretty good framework and that’s java.

And Yes to everything friedo said (especially the ten thousand suns part).

Although they superficially look similar ( all the {} stuff ) the way you write programs is totally different. Have you written in any other languages? coz you’re trying to compare chalk and chipmunks. What is you want to do?

Oh, and I’m with the ten thousand suns thing too.

Ten thousand suns ? That’s a tad strong.

Also Java is a lot stricter than C, and therefore it’s more difficult to stuff things up. A badly written Java program may hang or exit with an error, a badly written C program can play merry havoc with whatever it can access (which depending on your OS could be anything).

I agree with everyone else except that I quite like Java really, though nowadays I’d probably use C++ for anything remotely complex.

I strongly believe the people shouldn’t be taught using Java because people who learn programming in Java (in my experience) just don’t get pointers and memory usage when swithcing to C. The other way round people just need to learn OO concepts which is easier.

SD

i dunno about that. I started programming in perl and found the object oriented concepts to be kind of difficult to pick up, where as pointers were very easy to understand. I think it all comes down to the individual.

First of all, I’ve been a sofware developer for over 15 years. I started off as an assembler language programmer for a Major Airline in 1984. Since then I’ve programmed in C, C++, VB, Java, and most of the web scripting languages (JavaScript, Perl, VBScript), I’ve also dabbled a little bit in Open Look Widgits, and a whole plethora of other things that I’ve had the opportunity to work on over the last several years.

While I’m aware that I’m comparing apples to horses, it’s necessary because it’s a requirement of a Programming Languages course I’m taking. I was trying to be vague enough where people didn’t feel like they were doing my homework for me, but specific enough to find references that I could peruse.

For example, I’d like to comment on their readability, writability, and reliability in several catagories of the language. See Below for how I’d like to break down my presentation:

Readability
[list=A]
[li]Overall Simplicity[/li][li]Orthogonality[/li][li]Control Statements[/li][li]Data Types and Structures[/li][li]Syntax Considerations[/li][/list]
Writability
[list=A]
[li]Simplicity and Orthogonality[/li][li]Support for Abstraction[/li][li]Expressivity[/li][/list]
Reliability
[list=A]
[li]Type Checking[/li][li]Exception Handling[/li][li]Aliasing[/li][/list]
Cost

Readability is more a feature of the style of the programmer than the language.

Oh, I don’t think that’s true at all. Certainly readability can be enhanced by the programmer, but there’s a lot more to it than that. For example, I could write a program in Java, and the exact same program program in Assembler. No matter what the programmer does, the Java program is going to be easier to read than the assembler one. Terse languages are usually easier to write, when the programmer knows the language well, but more difficult to read. Visual Basic, for example, is easy to read, but is a pain (although simple) to write. It takes forever. Java is the same way. Much better readability than C, but Writability can really get drawn out sometimes.

As long as you have intelligent comments and consistent treatment of variables, spacing, control loops, etc, then your program should be easy to read.

Whether or not you understand the function of the program is another issue that is not related to readability. (In my mind at least). Readability is about using whitespace to help visually seperate code blocks, loops, if statements, etc.

A competent programmer can write programs that are easy to read in any language, relying mainly on the elements that wet marble mentioned, plus descriptive variable names. If you don’t know the language, it may be a little tough to get the exact specifics, but you can still get a general idea of what it’s doing.

On the other hand, you can write completely unintelligible programs in any language, either through malice or neglect.

Addendum: I meant to mention that, IMO, descriptive variable and function names are the real keys to readability. Everything else is just icing on the cake.

[QUOTE]
*Originally posted by ultrafilter *
A competent programmer can write programs that are easy to read in any language, relying mainly on the elements that wet marble mentioned, plus descriptive variable names.

[QUOTE]
I agree in general, but I’d like to point out that python code is really difficult to follow if you’re not a python programmer. The idea that the indent level actually has a functional meaning is really difficult to get used to.

scratch that last one. failed to preview.

I agree in general, but I’d like to point out that python code is really difficult to follow if you’re not a python programmer. The idea that the indent level actually has a functional meaning is really difficult to get used to.

Readability is the ease in which a program can be read and understood. It’s not about whitespace. As another example, using COBOL for an application that has lots of science formulas would make a program that is difficult to read.

“Readability must be considered in the context of the problem domain. For example, if a program that describes a compuation was written in a language not designed for such use, the program may be unnatural and convoluted, making it unusually difficult to read.” - Robert Sebesta

E3

On readability:

It depends on the programmer and how well the reader knows the language. If the programmer wrote in assembly (any kind, tho some CPUs support generally more readable assembly (VAX, 68000) than others (8086, PDP-8)) and the reader has no knowledge of assembly, it will be very slow going until the reader has learned what all of the opcodes do and how memory addressing works. If the programmer has written in regex-heavy Perl and the reader doesn’t know regexes, the code will look like line noise until the reader learns the regex language Perl uses.

Of course, the programmer can be as incompetent as the reader. If the programmer doesn’t indent well (a problem Python has all but fixed) or uses unreadable variable names or badly-designed algorithms (something no language can fix), the code will be worthless to even seasoned programmers. Look at the International Obfuscated C Code Competition winners for examples of this (Google IOCCC): Good programmers doing bad things with a language that can be very readable.

Is obfuscation harder in Python? Only certain kinds. If you get well-formatted spaghetti, with brainded variable names (preferably Hungarian notiation monsters whose Hungarian notation has been made obsolete through changes to the program design) and no sense of structure, it will be just as unreadable as C code that suffers from the same ills. Or assembly. Or COBOL. Or Java.

Is obfuscation easier in COBOL? Again, only certain kinds. COBOL doesn’t support algebraic notation, so you end up with multi-line mathematical expressions and a lot of temporary variables. Additionally, COBOL does not support local variables: Everything is a global. Subroutines cannot be passed or return values, and the only looping constructs are a simple counted loop and a simple conditional loop. On the other hand, COBOL supports the idea of associating record types with file handles, so writing formatted output is near-automatic. In C, you’d need to create a printf statement (which would be less-than-readable to a C newbie) and remember to use that exact statement whenever you wrote to a specific file handle (perhaps by creating a subroutine or a macro). So COBOL, losing on so many other fronts, wins when you have to do simple things to record-oriented files.

So, in the final analysis, readability is a function of the programmer with few exceptions (migrating from C to Brainfuck will make your code less readable and your programmers less sane, for example).

I think it is more instructive to compare C, C++ and Java. I have about 20 years experience with C, 10 with C++, and 4 with Java.

What I use depends on what I am trying to do. Horses for courses.

For small things and low-level stuff, I prefer C. For big things, I prefer Java. C++ is hell-spawn.

The advantage of C is that it is simple. Everything else (portability, general purpose, etc) derives from this point. You can write a C compiler in a few thousand lines of code. The disadvantages of C are that it is difficult to write a good optimizing compiler due to aliasing, and it is quite easy to be careless and shoot yourself in the foot.

The advantage of C++ is that it is an object-oriented language that is backwards mostly-compatible to C. Disadvantages include complexity (Brian Kernighan visited our offices about 10 years ago, and when asked about C++, his comment was “Don’t use it, too complicated”), lack of garbage collection (try holding an object as a void*, then delete’ing it (or is that delete?)), horrible “features” like operator overloading, and a pathetic standard library. Oh yeah, and it is trivial to blow your legs off.

Java is what C++ should have been. C-like syntax, but with full OO features built in and a decent library. I wish that it had unsigned types, the Integer/int stuff was better integrated, and things like raw I/O were built-in, but I’m pretty happy with it. It is easy to stub your toe in Java.

Sounds like you’d like C#.

I have two objections:

  1. It’s single-vendor. I don’t like begin tied down. You can try to make a similar argument against Java, but it doesn’t hold water. There are a lot of big vendors on board with Java - see www.jcp.org for details.

  2. It only runs on Microsoft Windows. OK enough for the desktop, but not very scalable on the server. Java works on anything from a PDA to a big hunk of iron.

Having said that, C# is essentially Microsoft’s answer to Java, and it shows. From my little playing around with it, there are a few things that I like (enums, better intrinsic type integrtion), and a few things I hate (operator overloading, no “throws”). (side note: many Java programmers complain about throws, but it forces you to think about error handling)

If I did development exclusively on Microsoft, C# would be a reasonable choice.