C vs Java β Quick Reference
Based on Princeton's "C Programming vs Java Programming" FAQ. Original source
| Thing | C | Java |
|---|---|---|
| Type of Language | Function oriented | Object oriented |
| Basic Programming Unit | function | class = ADT |
| Portability of Source Code | possible with discipline | yes |
| Portability of Compiled Code | no, recompile for each architecture | yes, bytecode is "write once, run anywhere" |
| Security | limited | built-in to language |
| Compilation | gcc hello.c creates machine language code | javac Hello.java creates JVM bytecode |
| Linking Math Library | gcc -lm calculate.c | no special flags needed |
| Joint Compilation | gcc main.c helper1.c helper2.c | javac Main.java β dependent files auto-recompiled |
| Execution | a.out loads and executes program | java Hello interprets byte code |
| Hello, World | | |
| Integer Types | int usually 32 bit; long usually 32 bit | int is 32 bit; long is 64 bit |
| Floating Point Types | float usually 32 bit; double usually 64 bit | float is 32 bit IEEE 754; double is 64 bit IEEE 754 |
| Boolean Type | use int: 0 for false, nonzero for true | boolean is its own type β true or false |
| Character Type | char is usually 8 bit ASCII | char is 16 bit UNICODE |
| For Loops | | |
| Array Declarations | | |
| Array Size | arrays don't know their own size | a.length |
| Strings | '\0'-terminated character array | built-in immutable String data type |
| Accessing a Library | | |
| Printing to stdout | | |
| Formatted Printing | | |
| Reading from stdin | | |
| Memory Address | pointer | reference |
| Manipulating Pointers | *, &, + | no direct manipulation permitted |
| Functions | | |
| Pass-by-Value | primitives, structs, and pointers by value; arrays decay to pointer | all primitives and references (including arrays) are passed by value |
| Defining a Data Structure | struct | class β with methods to manipulate data |
| Pointer Chasing | | |
| Allocating Memory | | |
| De-allocating Memory | free | automatic garbage collection |
| Buffer Overflow | segmentation fault, core dump | checked runtime exception |
| Declaring Constants | const and #define | final |
| Variable Auto-Initialization | not guaranteed | instance vars initialized to 0, null, or false; compile-time error for uninitialized access |
| Data Hiding | opaque pointers and static | private |
| Interface Method | non-static function | public method |
| Generic Data Type | void * | Object |
| Casting | anything goes | checked exception at runtime or compile-time |
| Polymorphism | union | inheritance |
| Overloading | no | yes for methods, no for operators |
| Graphics | use external libraries | Java library support |
| Null | NULL | null |
| Enumeration | enum | typesafe enum |
| Preprocessor | yes | no |
| Variable Declaration | at beginning of a block | before you use it |
| Naming Conventions | sum_of_squares | sumOfSquares |
| Commenting | /* */ | /* */ or // |
| File Naming | stack.c, stack.h | Stack.java β matches class name |
| Callbacks | pointers to global functions | use interfaces |
| Assertions | assert | assert |
| Exit & Return Value | exit(1) | System.exit(1) |
Type of Language
Function oriented
Object oriented
Basic Programming Unit
function
class = ADT
Portability of Source Code
possible with discipline
yes
Portability of Compiled Code
no, recompile for each architecture
yes, bytecode is "write once, run anywhere"
Security
limited
built-in to language
Compilation
gcc hello.c creates machine language code
javac Hello.java creates JVM bytecode
Linking Math Library
gcc -lm calculate.c
no special flags needed
Joint Compilation
gcc main.c helper1.c helper2.c
javac Main.java β dependent files auto-recompiled
Execution
a.out loads and executes program
java Hello interprets byte code
Hello, World
#include <stdio.h>
int main(void) {
printf("Hello\n");
return 0;
}public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello");
}
}Integer Types
int usually 32 bit; long usually 32 bit
int is 32 bit; long is 64 bit
Floating Point Types
float usually 32 bit; double usually 64 bit
float is 32 bit IEEE 754; double is 64 bit IEEE 754
Boolean Type
use int: 0 for false, nonzero for true
boolean is its own type β true or false
Character Type
char is usually 8 bit ASCII
char is 16 bit UNICODE
For Loops
for (i = 0; i < N; i++)for (int i = 0; i < N; i++)Array Declarations
int *a = malloc(N * sizeof(*a));int[] a = new int[N];Array Size
arrays don't know their own size
a.length
Strings
'\0'-terminated character array
built-in immutable String data type
Accessing a Library
#include <stdio.h>import java.io.File;Printing to stdout
printf("sum = %d", x);System.out.println("sum = " + x);Formatted Printing
printf("avg = %3.2f", avg);System.out.printf("avg = %3.2f", avg);Reading from stdin
scanf("%d", &x);StdIn.readInt() (using our library)Memory Address
pointer
reference
Manipulating Pointers
*, &, +
no direct manipulation permitted
Functions
int max(int a, int b)public static int max(int a, int b)Pass-by-Value
primitives, structs, and pointers by value; arrays decay to pointer
all primitives and references (including arrays) are passed by value
Defining a Data Structure
struct
class β with methods to manipulate data
Pointer Chasing
x->left->rightx.left.rightAllocating Memory
mallocnewDe-allocating Memory
free
automatic garbage collection
Buffer Overflow
segmentation fault, core dump
checked runtime exception
Declaring Constants
const and #define
final
Variable Auto-Initialization
not guaranteed
instance vars initialized to 0, null, or false; compile-time error for uninitialized access
Data Hiding
opaque pointers and static
private
Interface Method
non-static function
public method
Generic Data Type
void *
Object
Casting
anything goes
checked exception at runtime or compile-time
Polymorphism
union
inheritance
Overloading
no
yes for methods, no for operators
Graphics
use external libraries
Java library support
Null
NULL
null
Enumeration
enum
typesafe enum
Preprocessor
yes
no
Variable Declaration
at beginning of a block
before you use it
Naming Conventions
sum_of_squares
sumOfSquares
Commenting
/* */
/* */ or //
File Naming
stack.c, stack.h
Stack.java β matches class name
Callbacks
pointers to global functions
use interfaces
Assertions
assert
assert
Exit & Return Value
exit(1)
System.exit(1)
