My experience on my daily works... helping others ease each other

Thursday, January 12, 2012

C versus Java - A technical comparison





C Programming vs. Java Programming





ThingCJava
type of languagefunction orientedobject oriented
basic programming unitfunctionclass = ADT
portability of source codepossible with disciplineyes
portability of compiled codeno, recompile for each architectureyes, bytecode is "write once, run anywhere"
securitylimitedbuilt-in to language
compilationgcc hello.c creates machine language codejavac Hello.java creates Java virtual machine language bytecode
linking in the Math librarygcc -lm calculate.cno special flags needed
joint compilationgcc main.c helper1.c helper2.cjavac Main.java - any dependent files are automatically re-compiled if needed
executiona.out loads and executes programjava Hello interprets byte code
hello, world#include
int main(void) {
   printf("Hello\n");
   return 0;
}
public class HelloWorld {
   public static void main(String[] args) {
       System.out.println("Hello");
   }
}
integer typesint usually 32 bit 2's complement;
long usually 32 bit 2's complement
int is 32 bit 2's complement;
long is 64 bit 2's complement
floating point typesfloat usually 32 bit;
double usually 64 bit
float is 32 bit IEEE 754 binary floating point;
double is 64 bit IEEE 754
boolean typeuse int: 0 for false, nonzero for trueboolean is its own type - stores value true or false
character typechar is usually 8 bit ASCIIchar is 16 bit UNICODE
for loopsfor (i = 0; i < N; i++)for (int i = 0; i < N; i++)
array declarationsint *a = malloc(N * sizeof(*a));int[] a = new int[N];
array sizearrays don't know their own sizea.length
strings'\0'-terminated character arraybuilt-in immutable String data type
accessing a library#include import java.io.File;
accessing a library function#include "math.h"
x = sqrt(2.2);

all function and variables names are global
x = Math.sqrt(2.2);
functions have different namespaces
printing to standard outputprintf("sum = %d", x);System.out.println("sum = " + x);
formatted printingprintf("avg = %3.2f", avg);System.out.printf("avg = %3.2f", avg)
reading from stdinscanf("%d", &x);Java library support, but easier to use our library
int x = StdIn.readInt();
memory addresspointerreference
manipulating pointers*, &, +no direct manipulation permitted
functionsint max(int a, int b)public static int max(int a, int b)
pass-by-valueprimitive data types, structs, and pointers are passed by value; array decays to pointerall primitive data types and references (which includes arrays), are passed by value
defining a data structurestructclass - key difference is language support for defining methods to manipulate data
accessing a data structurea.numerator for elementsa.numerator for instance variables,
c = a.plus(b) for methods
pointer chasingx->left->rightx.left.right
allocating memorymallocnew
de-allocating memoryfreeautomatic garbage collection
memory allocation of data structures and arraysheap, stack, data, or bssheap
buffer overflowsegmentation fault, core dump, unpredicatable programchecked run-time error exception
declaring constantsconst and #definefinal
variable auto-initializationnot guaranteedinstance variables (and array elements) initialized to 0null, or false, compile-time error to access uninitialized variables
data hidingopaque pointers and staticprivate
interface methodnon-static functionpublic method
data type for generic itemvoid *Object
castinganything goeschecked exception at run-time or compile-time
demotionsautomatic, but might lose precisionmust explicitly cast, e.g., to convert from long to int
polymorphismunioninheritence
overloadingnoyes for methods, no for operators
graphicsuse external librariesJava library support, use our standard drawing library
nullNULLnull
enumerationenumtypesafe enum
preprocessoryesno
variable declarationat beginning of a blockbefore you use it
variable naming conventionssum_of_squaressumOfSquares
commenting/* *//* */ or //
file naming conventionsstack.cstack.hStack.java - file name matches name of class
callbackspointers to global functionsuse interfaces for commmand dispatching
variable number of argumentsvarargsString ...
assertionsassertassert
exit and return value to OSexit(1)System.exit(1)

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

0 comments:

About Me

Somewhere, Selangor, Malaysia
An IT by profession, a beginner in photography

Blog Archive

Blogger templates