CSC 2400: Computer Systems Week 1 – C versus Java
Reminders q Sign up for ZyBook – VILLANOVACSC2400RobsonFall2020 q HW 1 – Due a week from today http://www.csc.villanova.edu/~mprobson/courses/f a20-csc2400/hw1.html q Sign up for Piazza (23/42) - https://piazza.com/villanova/fall2020/csc2400
Hierarchy of Computer Languages Source: https://thebittheories.com/levels-of-programming-languages-b6a38a68c0f2
C vs. Java: Design Goals q Java design goals - Support object-oriented programming - Allow same program to be executed on multiple operating systems - Support using computer networks - Execute code from remote sources securely - Adopt the good parts of other languages (esp. C and C++) q Implications for Java - Good for application-level programming - High-level • Virtual machine insulates programmer from underlying assembly language, machine language, hardware - Portability over efficiency - Security over efficiency - Security over flexibility
C vs. Java: Design Goals q C design goals - Support structured programming - Support development of the Unix OS and Unix tools • As Unix became popular, so did C q Implications for C - Good for system-level programming • But often used for application-level programming – sometimes inappropriately - Low-level • Close to assembly language; close to machine language; close to hardware - Efficiency over portability - Efficiency over security - Flexibility over security
C vs. Java: Design Goals q Differences in design goals explain many differences between the languages q C’s design goal explains many of its eccentricities - We’ll see examples throughout the course
C vs. Java: Overview (cont.) Bad things you can do in C that you can’t do in Java q - Shoot yourself in the foot (safety) - Shoot others in the foot (security) - Ignore wounds (error handling) Dangerous things you must do in C that you don’t in Java q - Explicitly manage memory via malloc() and free() Good things you can do in C, but (more or less) must do in Java q - Program using the objected-oriented style Good things that you can’t do in C but can do in Java q - Write completely portable code
C vs. Java Java C Hello.java: hello.c: import java.io.* #include <stdio.h> Overall public class Hello { int main() { Program public static void main(String[]s) printf("Hello 2400\n"); { Structure return 0; System.out.println("Hello 2400"); } } } % javac Hello.java % gcc hello.c % ls % ls Hello.class a.out Building Hello.java hello.c % % % java Hello % ./a.out Hello, world Hello, world Running % %
C vs. Java (cont.) Java C char // 16-bit unicode char /* 8 bits */ Character type (unsigned) char byte // 8 bits (unsigned) short short // 16 bits Integral types int // 32 bits (unsigned) int long // 64 bits (unsigned) long float float // 32 bits Floating point double double // 64 bits types long double /* no equivalent */ boolean Logical type /* use integral type */ Generic pointer // no equivalent void* type #define MAX 1000 const int MAX = 1000; final int MAX = 1000 ; Constants enum {MAX = 1000};
C vs. Java (cont.) Java C int [] a = new int [10]; int a[10]; float [][] b = float b[5][20]; Arrays new float [5][20]; Array bound // run-time check /* no run-time check */ checking // Object reference is an int *p; Pointer type // implicit pointer class Mine { struct Mine { int x; int x; Record type float y; float y; } }
C vs. Java Example Java C DataTypes.java: datatypes.c: import java.io.* #include <stdio.h> public class DataTypes { void go() public static void go() { { int a = 2; int a = 2; float b = 3.1416; float b = 3.1416; char s[100]; byte[] s = new byte [100]; scanf("%s", s); System.in.read(s); printf("%s", s); System.out.print(Arrays.toString(s)); } } int main() { public static void main(String[]args) go(); { return 0; go(); } } }
C vs. Java (cont.) Java C String s1 = "Hello"; char *s1 = "Hello"; String s2 = new char s2[6]; Strings String ("hello"); sprintf( s2,"%s", "hello" ); s1 + s2 #include <string.h> String s1 += s2 strcat(s1, s2); concatenation &&, ||, ! &&, ||, ! Logical ops =, !=, >, <, >=, <= =, !=, >, <, >=, <= Relational ops Arithmetic +, -, *, /, %, unary - +, -, *, /, %, unary - ops >> , << , >>> , & , | , ^ >> , << , & , | , ^ Bitwise ops = , *= , /= , += , -= , <<= , = , *= , /= , += , -= , <<= , Assignment >>= , >>>= , = , ^= , |= , %= >>= , = , ^= , |= , %= ops
C vs. Java Example Java C StringManipulation.java: mystring.c: import java.io.* #include <stdio.h> #include <string.h> public class StringManipulation { public static void main(String[]args) int main() { { char * s1 = "Computer Science"; String s1 = "Computer Science"; int x = 2400; int x = 2400; char * s2 = "is fun!\n" String s2 = "is fun!\n" char fun[100]; char tmp[5]; String fun = s1 + " " + x + " " + s2; System.out.println(fun); strcpy(fun, s1); } sprintf(tmp, " %d ", x); } strcat(fun, tmp); strcat(fun, s2); printf("%s\n", fun); return 0; }
C vs. Java (cont.) Java C if (i < 0) if (i < 0) statement1 ; statement1 ; if stmt else else statement2 ; statement2 ; switch (i) { switch (i) { case 1: case 1: ... ... break ; break ; case 2: case 2: switch stmt ... ... break ; break ; default : default : ... ... } } // no equivalent goto SomeLabel; goto stmt
Source: https://www.xkcd.com/292/
C vs. Java Example Java C Exam.java: exam.c: public class Exam { #include <stdio.h> public static void main(String args[]) int main() { { char grade = (char)System.in.read(); char grade; switch(grade) { scanf(“%d”, &grade); case 'A': System.out.print("Yay!"); switch(grade) { break; case 'A': printf("Yay!"); case 'B': break; case 'C': System.out.print("Well done"); case 'B': break; case 'C': printf("Well done"); case 'D': System.out.print("Passed!"); break; break; case 'D': printf("Passed!"); case 'F': System.out.print("Try again"); break; break; case 'F’: printf("Try again"); default: System.out.print("Invalid"); break; } default: printf("Invalid"); if ((grade == 'A') || (grade == 'B')) } System.out.print("Congratulations!!!") if((grade=='A’)||(grade=='B')) } printf("Congratulations!!!") } return 0; }
C vs. Java (cont.) Java C int i; for (int i=0; i<10; i++) for (i=0; i<10; i++) for stmt statement ; statement ; while (i < 0) while (i < 0) while stmt statement ; statement ; do { do { statement ; statement ; do-while stmt … … } while (i < 0) } while (i < 0) continue ; continue ; continue stmt labeled continue SomeLabel; /* no equivalent */ continue stmt break ; break; break stmt labeled break break SomeLabel; /* no equivalent */ stmt
C vs. Java Example Java C Factorial.java: factorial.c: public class Factorial { #include <stdio.h> public static void main(String[] args) #include <string.h> { final int NUM_FACT = 100; #define NUM_FACT 100 for(int i = 0; i < NUM_FACT; i++) int fact(int n) System.out.println( { i + "! is ” + fact(i) ); /* identical to Java */ } } int main() public static int fact(int n) { { int i; int result = 1; for(i = 0; i < NUM_FACT; i++) for(int i = 2; i <= n; i++) printf( “%d! is %d”, i, fact(i)); result *= i; return 0; return result; } } }
C vs. Java (cont.) Java C return 5; return 5; return stmt return ; return ; { { statement1 ; statement1 ; Compound stmt statement2 ; statement2 ; (alias block) } } throw , try-catch-finally /* no equivalent */ Exceptions /* comment */ /* comment */ Comments // another kind // depends on standard f(x, y, z); Method / someObject .f(x, y, z); f(x, y, z); function call SomeClass .f(x, y, z);
C vs. Java Features q Java - Object-oriented - Portable - Secure q C - Efficient - Flexible - Lower level (closer to the system)
Recommend
More recommend