C Programming vs. Java Programming

Thing C Java

type of languagefunction orientedobject orientedbasic programming unitfunctionclass = ADTportability of source codepossible with disciplineyesportability of compiled codeno, recompile for each architectureyes, bytecode is “write once, run anywhere”securitylimitedbuilt-in to languagecompilationgcc hello.ccreates machine language codejavac Hello.javacreates Java virtual machine language bytecodelinking in the Math librarygcc -lm calculate.cno special flags neededjoint compilationgcc main.c helper1.c helper2.cjavac Main.java– any dependent files are automatically re-compiled if neededexecutiona.outloads and executes programjava Hellointerprets byte codehello, 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 typesintusually 32 bit 2’s complement;longusually 32 bit 2’s complementintis 32 bit 2’s complement;longis 64 bit 2’s complementfloating point typesfloatusually 32 bit;doubleusually 64 bitfloatis 32 bit IEEE 754 binary floating point;doubleis 64 bit IEEE 754boolean typeuseint: 0 for false, nonzero for truebooleanis its own type – stores valuetrueorfalsecharacter typecharis usually 8 bit ASCIIcharis 16 bit UNICODEfor 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.lengthstrings'\0'-terminated character arraybuilt-in immutableStringdata typeaccessing a library#include <stdio.h>import java.io.File;accessing a library function#include "math.h"x = sqrt(2.2);all function and variables names are globalx = Math.sqrt(2.2);functions have different namespacesprinting 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 libraryint x = StdIn.readInt();memory addresspointerreferencemanipulating pointers*, &, +no direct manipulation permittedfunctionsint 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 valuedefining a data structurestructclass– key difference is language support for defining methods to manipulate dataaccessing a data structurea.numeratorfor elementsa.numeratorfor instance variables,c = a.plus(b)for methodspointer chasingx->left->rightx.left.rightallocating memorymallocnewde-allocating memoryfreeautomatic garbage collectionmemory allocation of data structures and arraysheap, stack, data, or bssheapbuffer overflowsegmentation fault, core dump, unpredicatable programchecked run-time error exceptiondeclaring constantsconstand#definefinalvariable auto-initializationnot guaranteedinstance variables (and array elements) initialized to0,null, orfalse, compile-time error to access uninitialized variablesdata hidingopaque pointers andstaticprivateinterface methodnon-static functionpublicmethoddata type for generic itemvoid *Objectcastinganything goeschecked exception at run-time or compile-timedemotionsautomatic, but might lose precisionmust explicitly cast, e.g., to convert fromlongtointpolymorphismunioninheritenceoverloadingnoyes for methods, no for operatorsgraphicsuse external librariesJava library support, use our standard drawing librarynullNULLnullenumerationenumtypesafeenumpreprocessoryesnovariable declarationat beginning of a blockbefore you use itvariable naming conventionssum_of_squaressumOfSquarescommenting/* *//* */or//file naming conventionsstack.c,stack.hStack.java– file name matches name of classcallbackspointers to global functionsuse interfaces forcommmand dispatchingvariable number of argumentsvarargsString ...assertionsassertassertexit and return value to OSexit(1)System.exit(1)

不论你在什么时候结束,重要的是结束之后就不要悔恨

C Programming vs. Java Programming

相关文章:

你感兴趣的文章:

标签云: