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. cCreates machine language code |
Javac Hello. javaCreates Java virtual machine language bytecode |
Linking in the Math library |
Gcc-lm calculate. c |
No special flags needed |
Joint compilation |
Gcc main. c helper1.c helper2.c |
Javac Main. java-Any dependent files are automatically re-compiled if needed |
Execution |
A. outLoads and executes program |
Java HelloInterprets 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 |
IntUsually 32 bit 2's complement; LongUsually 32 bit 2's complement |
IntIs 32 bit 2's complement; LongIs 64 bit 2's complement |
Floating point types |
FloatUsually 32 bit; DoubleUsually 64 bit |
FloatIs 32 bit IEEE 754 binary floating point; DoubleIs 64-bit IEEE 754 |
Boolean type |
UseInt: 0 for false, nonzero for true |
BooleanIs its own type-stores valueTrueOrFalse |
Character type |
CharIs usually 8 bit ASCII |
CharIs 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 (* )); |
Int [] a = new int [N]; |
Array size |
Arrays don't know their own size |
A. length |
Strings |
'\ 0'-Terminated character array |
Built-in immutableStringData type |
Accessing a library |
# Include <stdio. h> |
Import java. io. File; |
Accessing a library function |
# Include "math. h" X = sqrt (2.2 ); All functions and variables names are global |
X = Math. sqrt (2.2 ); Functions have different namespaces |
Printing to standard output |
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 ); |
Java library support, but easier to use our library Int x = StdIn. readInt (); |
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 |
Primitive data types, structs, and pointers are passed by value; array decays to pointer |
All primitive data types and references (which primary des arrays), are passed by value |
Defining a data structure |
Struct |
Class-Key difference is language support for defining methods to manipulate data |
Accessing a data structure |
A. numeratorFor elements |
A. numeratorFor instance variables, C = a. plus (B)For methods |
Pointer chasing |
X-> left-> right |
X. left. right |
Allocating memory |
Malloc |
New |
De-allocating memory |
Free |
Automatic garbage collection |
Memory allocation of data structures and arrays |
Heap, stack, data, or bss |
Heap |
Buffer overflow |
Segmentation fault, core dump, unpredicatable program |
Checked run-time error exception |
Declaring constants |
ConstAnd# Define |
Final |
Variable auto-initialization |
Not guaranteed |
Instance variables (and array elements) initialized0,Null, OrFalse, Compile-time error to access uninitialized variables |
Data hiding |
Opaque pointers andStatic |
Private |
Interface method |
Non-static function |
PublicMethod |
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 fromLongToInt |
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 |
TypesafeEnum |
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) |