C vs.
Java Page 1 of 3
C PROGRAMMING VS. JAVA PROGRAMMING
Thing C Java
type of language function oriented object oriented
basic programming
function class = ADT
unit
portability of
possible with discipline yes
source code
portability of no, recompile for each yes, bytecode is "write once, run
compiled code architecture anywhere"
security limited built-in to language
gcc hello.c creates machine javac Hello.java creates Java virtual
compilation
language code machine language bytecode
linking in the Math
gcc -lm calculate.c no special flags needed
library
gcc main.c helper1.c javac Main.java - any dependent files
joint compilation
helper2.c are automatically re-compiled if needed
a.out loads and executes
execution java Hello interprets byte code
program
#include<stdio.h> public class HelloWorld {
int main(void) { public static void main
printf("Hello\n"); (String[] args) {
hello, world return 0; System.out.println
} ("Hello");
}
}
int usually 32 bit 2's
complement; int is 32 bit 2's complement;
integer types
long usually 32 bit 2's long is 64 bit 2's complement
complement
float is 32 bit IEEE 754 binary floating
float usually 32 bit;
floating point types point;
double usually 64 bit
double is 64 bit IEEE 754
use int: 0 for false, nonzero boolean is its own type - stores value
boolean type
for true 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++)
int *a = malloc(N *
array declarations int[] a = new int[N];
sizeof(*a));
arrays don't know their own
array size a.length
size
http://introcs.cs.princeton.edu/java/faq/c2java.html 8/31/2012
C vs. Java Page 2 of 3
'\0'-terminated character
strings built-in immutable String data type
array
accessing a library #include <stdio.h> import java.io.File;
#include "math.h"
accessing a library x = sqrt(2.2); x = Math.sqrt(2.2);
function all function and variables names functions have different namespaces
are global
printing to System.out.println("sum = " +
printf("sum = %d", x);
standard output x);
printf("avg = %3.2f", System.out.printf("avg = %3.2f",
formatted printing
avg); avg)
Java library support, but easier to use our
reading from stdin scanf("%d", &x); library
int x = StdIn.readInt();
memory address pointer reference
manipulating
*, &, + no direct manipulation permitted
pointers
public static int max(int a, int
functions int max(int a, int b)
b)
primitive data types, structs, all primitive data types and references
pass-by-value and pointers are passed by (which includes arrays), are passed by
value; array decays to pointer value
class - key difference is language
defining a data
struct support for defining methods to
structure
manipulate data
accessing a data a.numerator for instance variables,
a.numerator for elements
structure c = a.plus(b) for methods
pointer chasing x->left->right x.left.right
allocating memory malloc new
de-allocating
free automatic garbage collection
memory
memory allocation
of data structures heap, stack, data, or bss heap
and arrays
segmentation fault, core dump,
buffer overflow checked run-time error exception
unpredicatable program
declaring constants const and #define final
instance variables (and array elements)
variable auto-
not guaranteed initialized to 0, null, or false, compile-
initialization
time error to access uninitialized variables
data hiding opaque pointers and static private
interface method non-static function public method
data type for
void * Object
generic item
http://introcs.cs.princeton.edu/java/faq/c2java.html 8/31/2012
C vs. Java Page 3 of 3
checked exception at run-time or compile-
casting anything goes
time
automatic, but might lose must explicitly cast, e.g., to convert from
demotions
precision long to int
polymorphism union inheritence
overloading no yes for methods, no for operators
Java library support, use our standard
graphics use external libraries
drawing library
null NULL null
enumeration enum typesafe enum
preprocessor yes no
variable
at beginning of a block before you use it
declaration
variable naming
sum_of_squares sumOfSquares
conventions
commenting /* */ /* */ or //
file naming Stack.java - file name matches name of
stack.c, stack.h
conventions class
callbacks pointers to global functions use interfaces for commmand dispatching
variable number of
varargs String ...
arguments
assertions assert assert
exit and return
exit(1) System.exit(1)
value to OS
http://introcs.cs.princeton.edu/java/faq/c2java.html 8/31/2012