Hedi Attia
Back to Blog
Custom

C vs Java β€” Quick Reference

c java programming comparison

Based on Princeton's "C Programming vs Java Programming" FAQ. Original source

Type of Language

C

Function oriented

vs
Java

Object oriented

Basic Programming Unit

C

function

vs
Java

class = ADT

Portability of Source Code

C

possible with discipline

vs
Java

yes

Portability of Compiled Code

C

no, recompile for each architecture

vs
Java

yes, bytecode is "write once, run anywhere"

Security

C

limited

vs
Java

built-in to language

Compilation

C

gcc hello.c creates machine language code

vs
Java

javac Hello.java creates JVM bytecode

Linking Math Library

C

gcc -lm calculate.c

vs
Java

no special flags needed

Joint Compilation

C

gcc main.c helper1.c helper2.c

vs
Java

javac Main.java β€” dependent files auto-recompiled

Execution

C

a.out loads and executes program

vs
Java

java Hello interprets byte code

Hello, World

C
#include <stdio.h>
int main(void) {
    printf("Hello\n");
    return 0;
}
vs
Java
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello");
    }
}

Integer Types

C

int usually 32 bit; long usually 32 bit

vs
Java

int is 32 bit; long is 64 bit

Floating Point Types

C

float usually 32 bit; double usually 64 bit

vs
Java

float is 32 bit IEEE 754; double is 64 bit IEEE 754

Boolean Type

C

use int: 0 for false, nonzero for true

vs
Java

boolean is its own type β€” true or false

Character Type

C

char is usually 8 bit ASCII

vs
Java

char is 16 bit UNICODE

For Loops

C
for (i = 0; i < N; i++)
vs
Java
for (int i = 0; i < N; i++)

Array Declarations

C
int *a = malloc(N * sizeof(*a));
vs
Java
int[] a = new int[N];

Array Size

C

arrays don't know their own size

vs
Java

a.length

Strings

C

'\0'-terminated character array

vs
Java

built-in immutable String data type

Accessing a Library

C
#include <stdio.h>
vs
Java
import java.io.File;

Printing to stdout

C
printf("sum = %d", x);
vs
Java
System.out.println("sum = " + x);

Formatted Printing

C
printf("avg = %3.2f", avg);
vs
Java
System.out.printf("avg = %3.2f", avg);

Reading from stdin

C
scanf("%d", &x);
vs
Java
StdIn.readInt() (using our library)

Memory Address

C

pointer

vs
Java

reference

Manipulating Pointers

C

*, &, +

vs
Java

no direct manipulation permitted

Functions

C
int max(int a, int b)
vs
Java
public static int max(int a, int b)

Pass-by-Value

C

primitives, structs, and pointers by value; arrays decay to pointer

vs
Java

all primitives and references (including arrays) are passed by value

Defining a Data Structure

C

struct

vs
Java

class β€” with methods to manipulate data

Pointer Chasing

C
x->left->right
vs
Java
x.left.right

Allocating Memory

C
malloc
vs
Java
new

De-allocating Memory

C

free

vs
Java

automatic garbage collection

Buffer Overflow

C

segmentation fault, core dump

vs
Java

checked runtime exception

Declaring Constants

C

const and #define

vs
Java

final

Variable Auto-Initialization

C

not guaranteed

vs
Java

instance vars initialized to 0, null, or false; compile-time error for uninitialized access

Data Hiding

C

opaque pointers and static

vs
Java

private

Interface Method

C

non-static function

vs
Java

public method

Generic Data Type

C

void *

vs
Java

Object

Casting

C

anything goes

vs
Java

checked exception at runtime or compile-time

Polymorphism

C

union

vs
Java

inheritance

Overloading

C

no

vs
Java

yes for methods, no for operators

Graphics

C

use external libraries

vs
Java

Java library support

Null

C

NULL

vs
Java

null

Enumeration

C

enum

vs
Java

typesafe enum

Preprocessor

C

yes

vs
Java

no

Variable Declaration

C

at beginning of a block

vs
Java

before you use it

Naming Conventions

C

sum_of_squares

vs
Java

sumOfSquares

Commenting

C

/* */

vs
Java

/* */ or //

File Naming

C

stack.c, stack.h

vs
Java

Stack.java β€” matches class name

Callbacks

C

pointers to global functions

vs
Java

use interfaces

Assertions

C

assert

vs
Java

assert

Exit & Return Value

C

exit(1)

vs
Java

System.exit(1)