Programming · 30 Aug 2014 · 2 min read
Difference between Java and C programming language
Java is a pure object-oriented programming language, built around classes, objects, inheritance, and polymorphism, and its execution is non-linear — there’s no way to write a Java program without using classes and objects. C, by contrast, is built around structures rather than objects, and isn’t object-oriented at all.
How Java works
- Java’s guiding principle is “write once, run anywhere.” Compiling a Java program produces an intermediate bytecode, which the Java Virtual Machine (JVM) then interprets, translating it into instructions a specific processor understands.
- Because that bytecode has to be interpreted at runtime, Java programs execute somewhat more slowly than natively compiled code.
- Java’s garbage collector manages memory automatically, so the programmer doesn’t have to.
- Variables can be declared anywhere in a Java program, though it’s still good practice to declare them at the start of a block.
- Code reuse happens through inheritance, and class members are private by default.
- When the JVM executes bytecode, it doesn’t load every class in an imported package — it enters the package, runs only the class it needs, and returns the result. That keeps Java’s memory use down.
How C works
- C uses structures, not objects — there’s no object orientation.
- C works directly with pointers; Java has no equivalent.
- Memory management in C is manual, handled through
malloc()andfree(). - Variable declarations need to sit at the start of a block.
- C supports
goto, structs, and unions, none of which Java offers. - C compiles down to a machine’s native instructions, so it runs faster than Java’s interpreted bytecode.
- There’s no built-in code reuse, and members are public by default.
- A compiled C program has a larger footprint than raw machine code, but a smaller one than an equivalent Java program, because C doesn’t need to load a runtime interpreter the way Java needs the JVM.
The bottom line
The main differences between Java and C come down to speed, portability, and object orientation. Java was designed to run unmodified across many kinds of hardware — phones, Macs, PCs, Linux machines — while a C program only runs on the type of machine it was compiled for. Java also lets you build classes that bundle data and behaviour together; C has no equivalent concept.