This page provides a quick comparison of C to Java Programming.

Areas of Comparison

 

Sample Program Comparison

C Programming

helloworld.c

#include<stdio.h>
int main(void) {
    printf("Hello\n");
    return 0;
}

Java Programming

HelloWorld.java

public class HelloWorld {
   public static void main(String[] args) {
       System.out.println("Hello");
   }
} 

Back to Top


General Comparisons

Language Type

C Programming

function oriented

Java Programming

object oriented

Back to Top


Basic Programming Unit

C Programming

function

Java Programming

class

Back to Top


Portability of Source Code

C Programming

possible with discipline

Java Programming

yes

Back to Top


Portability of Compiled Code

C Programming

no, recompile for each architecture

Java Programming

yes, bytecode is “Write once, run anywhere” (WORA)

Back to Top


Security

C Programming

limited

Java Programming

built-in to language

Back to Top


Compilation

C Programming

creates machine language code

gcc hello.c

Java Programming

creates Java virtual machine language bytecode

javac Hello.java

Linking in the Math Library

C Programming

gcc -lm calculate.c

Java Programming

no special flags needed

Joint Compilation

C Programming

gcc main.c helper1.c helper2.c

Java Programming

any dependent files are automatically re-compiled if needed

javac Main.java

Back to Top


Execution

C Programming

a.out

loads and executes program

Java Programming

java Hello

interprets byte code

Back to Top


memory Address

C Programming

pointer

Java Programming

reference

Back to Top


Data Type Comparisons

Primitive Integer Types

C Programming

int

usually 32 bit 2’s complement

Java Programming

int

is 32 bit 2’s complement

Primitive Long Types

C Programming

long

usually 32 bit 2’s complement

Java Programming

long

is 64 bit 2’s complement

Primitive Float Types

C Programming

float

usually 32 bit

Java Programming

float

is 32 bit IEEE 754 binary floating point

Primitive Double Types

C Programming

double

usually 64 bit

Java Programming

double

is 64 bit IEEE 754

Back to Top


Primitive Boolean Types

C Programming

int

uses 0 for false, nonzero for true

Java Programming

boolean 

is its own type – stores value

true

or

false

Primitive Character Types

C Programming

char

is usually 8 bit ASCII

Java Programming

char

is 16 bit UNICODE

Primitive Character Types

C Programming

char

is usually 8 bit ASCII

Java Programming

char

is 16 bit UNICODE

Back to Top


For Loops

C Programming

for (i = 0; i < N; i++)

Java Programming

for (int i = 0; i < N; i++)

Back to Top


Arrays

Array Declarations

C Programming

int *a = malloc(N * sizeof(*a));

Java Programming

int[] a = new int[N];

Array Size

C Programming

arrays don’t know their own size

Java Programming

a.length

Back to Top


Strings

C Programming

'\0'

-terminated character array

Java Programming

String

built-in immutable data type

Back to Top


Accessing a Library

C Programming

#include &lt;stdio.h&gt;

Java Programming

import java.io.File;

Back to Top


Accessing a Library Function

C Programming

#include "math.h"
x = sqrt(2.2);

all function and variables names are global

Java Programming

x = Math.sqrt(2.2);

functions have different namespaces

Back to Top


Printing

Printing to StdOut

C Programming

printf("sum = %d", x);

Java Programming

System.out.println("sum = " + x);

Formatted Printing to StdOut

C Programming

printf("avg = %3.2f", avg);

Java Programming

System.out.printf("avg = %3.2f", avg)

Back to Top


Reading from StdIn

C Programming

scanf("%d", &x);

Java Programming

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String s = in.readLine();

Back to Top


Manipulating Pointers

C Programming

 *, &, +

Java Programming

no direct manipulation permitted

Back to Top


Functions

C Programming

int max(int a, int b)

Java Programming

public static int max(int a, int b)

Back to Top


Pass by Value

C Programming

primitive data types, structs, and pointers
are passed by value; array decays to pointer

Java Programming

all primitive data types and references (which includes
arrays), are passed by value

Back to Top


Defining a Data Structure

C Programming

struct

Java Programming

class

key difference is language
support for defining methods to manipulate data

Back to Top


Accessing a Data Structure

C Programming

a.numerator

Java Programming

a.numerator

for instance variables,

c = a.plus(b)

for methods

Back to Top


Pointer Chasing

C Programming

x->left->right

Java Programming

x.left.right

Back to Top


Allocating Memory

C Programming

malloc

Java Programming

new

Back to Top


De-Allocating Memory

C Programming

free

Java Programming

automatic garbage collection

Back to Top


Memory Allocation of Data Structures

C Programming

heap, stack, data, or bss

Java Programming

heap, with a little stack use

Back to Top


Buffer Overflow

C Programming

segmentation fault, core dump, unpredicatable program

Java Programming

checked run-time error exception

Back to Top


Declaring Constants

C Programming

const

and

#define

Java Programming

final

Back to Top


Variable Auto-Initialization

C Programming

not guaranteed

Java Programming

instance variables (and array elements) initialized to 0, null, or
false, compile-time error to access uninitialized variables

Back to Top


Data Hiding

C Programming

opaque pointers and

static

Java Programming

private

Back to Top


interface method

non-static function

public method
data type for generic item

void *

Object
casting

anything goes

checked exception at run-time or compile-time
demotions

automatic, but might lose precision

must explicitly cast, e.g., to convert from long to int
polymorphism

union

inheritence
overloading

no

yes for methods, no for operators
graphics

use external libraries

Java library support, use our standard drawing library
null

NULL

null
enumeration

enum

typesafe enum
preprocessor

yes

no
variable declaration

at beginning of a block

before you use it
variable naming conventions

sum_of_squares

sumOfSquares
commenting

/* */

/* */ or //
file naming conventions

stack.c, stack.h

Stack.java – file name matches name of class
callbacks

pointers to global functions

use interfaces for

commmand dispatching

variable number of arguments

varargs

String ...
assertions

assert

assert
exit and return value to OS

exit(1)

System.exit(1)

Original Source: http://introcs.cs.princeton.edu/java/faq/c2java.html

Back to Top