CSC 2400: Computer Systems Week 1 C versus Java Reminders q Sign - - PowerPoint PPT Presentation

csc 2400 computer systems
SMART_READER_LITE
LIVE PREVIEW

CSC 2400: Computer Systems Week 1 C versus Java Reminders q Sign - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

CSC 2400: Computer Systems

Week 1 – C versus Java

slide-2
SLIDE 2

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

slide-3
SLIDE 3

Hierarchy of Computer Languages

Source: https://thebittheories.com/levels-of-programming-languages-b6a38a68c0f2

slide-4
SLIDE 4

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
slide-5
SLIDE 5

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
slide-6
SLIDE 6

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
slide-7
SLIDE 7

C vs. Java: Overview (cont.)

q

Bad things you can do in C that you can’t do in Java

  • Shoot yourself in the foot (safety)
  • Shoot others in the foot (security)
  • Ignore wounds (error handling)

q

Dangerous things you must do in C that you don’t in Java

  • Explicitly manage memory via malloc() and free()

q

Good things you can do in C, but (more or less) must do in Java

  • Program using the objected-oriented style

q

Good things that you can’t do in C but can do in Java

  • Write completely portable code
slide-8
SLIDE 8

C vs. Java

Java C

Overall Program Structure

Hello.java: import java.io.* public class Hello { public static void main(String[]s) { System.out.println("Hello 2400"); } } hello.c: #include <stdio.h> int main() { printf("Hello 2400\n"); return 0; }

Building

% javac Hello.java % ls Hello.class Hello.java % % gcc hello.c % ls a.out hello.c %

Running

% java Hello Hello, world % % ./a.out Hello, world %

slide-9
SLIDE 9

C vs. Java (cont.)

Java C

Character type char // 16-bit unicode char /* 8 bits */ Integral types byte // 8 bits short // 16 bits int // 32 bits long // 64 bits (unsigned) char (unsigned) short (unsigned) int (unsigned) long Floating point types float // 32 bits double // 64 bits float double long double Logical type boolean /* no equivalent */ /* use integral type */ Generic pointer type // no equivalent void* Constants final int MAX = 1000; #define MAX 1000 const int MAX = 1000; enum {MAX = 1000};

slide-10
SLIDE 10

C vs. Java (cont.)

Java C

Arrays int [] a = new int [10]; float [][] b = new float [5][20]; int a[10]; float b[5][20]; Array bound checking // run-time check /* no run-time check */ Pointer type // Object reference is an // implicit pointer int *p; Record type class Mine { int x; float y; } struct Mine { int x; float y; }

slide-11
SLIDE 11

C vs. Java Example

Java C

DataTypes.java: datatypes.c: import java.io.* public class DataTypes { public static void go() { int a = 2; float b = 3.1416; byte[] s = new byte [100]; System.in.read(s); System.out.print(Arrays.toString(s)); } public static void main(String[]args) { go(); } } #include <stdio.h> void go() { int a = 2; float b = 3.1416; char s[100]; scanf("%s", s); printf("%s", s); } int main() { go(); return 0; }

slide-12
SLIDE 12

C vs. Java (cont.)

Java C

Strings String s1 = "Hello"; String s2 = new String("hello"); char *s1 = "Hello"; char s2[6]; sprintf(s2,"%s", "hello"); String concatenation s1 + s2 s1 += s2 #include <string.h> strcat(s1, s2); Logical ops &&, ||, ! &&, ||, ! Relational ops =, !=, >, <, >=, <= =, !=, >, <, >=, <= Arithmetic

  • ps

+, -, *, /, %, unary - +, -, *, /, %, unary - Bitwise ops >>, <<, >>>, &, |, ^ >>, <<, &, |, ^ Assignment

  • ps

=, *=, /=, +=, -=, <<=, >>=, >>>=, =, ^=, |=, %= =, *=, /=, +=, -=, <<=, >>=, =, ^=, |=, %=

slide-13
SLIDE 13

C vs. Java Example

Java C

StringManipulation.java: mystring.c: import java.io.* public class StringManipulation { public static void main(String[]args) { String s1 = "Computer Science"; int x = 2400; String s2 = "is fun!\n" String fun = s1 + " " + x + " " + s2; System.out.println(fun); } } #include <stdio.h> #include <string.h> int main() { char * s1 = "Computer Science"; int x = 2400; char * s2 = "is fun!\n" char fun[100]; char tmp[5]; strcpy(fun, s1); sprintf(tmp, " %d ", x); strcat(fun, tmp); strcat(fun, s2); printf("%s\n", fun); return 0; }

slide-14
SLIDE 14

C vs. Java (cont.)

Java C

if stmt if (i < 0) statement1; else statement2; if (i < 0) statement1; else statement2; switch stmt switch (i) { case 1: ... break; case 2: ... break; default: ... } switch (i) { case 1: ... break; case 2: ... break; default: ... } goto stmt // no equivalent goto SomeLabel;

slide-15
SLIDE 15

Source: https://www.xkcd.com/292/

slide-16
SLIDE 16

C vs. Java Example

Java C

Exam.java: exam.c: public class Exam { public static void main(String args[]) { char grade = (char)System.in.read(); switch(grade) { case 'A': System.out.print("Yay!"); break; case 'B': case 'C': System.out.print("Well done"); break; case 'D': System.out.print("Passed!"); break; case 'F': System.out.print("Try again"); break; default: System.out.print("Invalid"); } if ((grade == 'A') || (grade == 'B')) System.out.print("Congratulations!!!") } } #include <stdio.h> int main() { char grade; scanf(“%d”, &grade); switch(grade) { case 'A': printf("Yay!"); break; case 'B': case 'C': printf("Well done"); break; case 'D': printf("Passed!"); break; case 'F’: printf("Try again"); break; default: printf("Invalid"); } if((grade=='A’)||(grade=='B')) printf("Congratulations!!!") return 0; }

slide-17
SLIDE 17

C vs. Java (cont.)

Java C

for stmt for (int i=0; i<10; i++) statement; int i; for (i=0; i<10; i++) statement; while stmt while (i < 0) statement; while (i < 0) statement; do-while stmt do { statement; … } while (i < 0) do { statement; … } while (i < 0) continue stmt continue; continue; labeled continue stmt continue SomeLabel; /* no equivalent */ break stmt break; break; labeled break stmt break SomeLabel; /* no equivalent */

slide-18
SLIDE 18

C vs. Java Example

Java C

Factorial.java: factorial.c: public class Factorial { public static void main(String[] args) { final int NUM_FACT = 100; for(int i = 0; i < NUM_FACT; i++) System.out.println( i + "! is ” + fact(i) ); } public static int fact(int n) { int result = 1; for(int i = 2; i <= n; i++) result *= i; return result; } } #include <stdio.h> #include <string.h> #define NUM_FACT 100 int fact(int n) { /* identical to Java */ } int main() { int i; for(i = 0; i < NUM_FACT; i++) printf( “%d! is %d”, i, fact(i)); return 0; }

slide-19
SLIDE 19

C vs. Java (cont.)

Java C

return stmt return 5; return; return 5; return; Compound stmt (alias block) { statement1; statement2; } { statement1; statement2; } Exceptions throw, try-catch-finally /* no equivalent */ Comments /* comment */ // another kind /* comment */ // depends on standard Method / function call f(x, y, z); someObject.f(x, y, z); SomeClass.f(x, y, z); f(x, y, z);

slide-20
SLIDE 20

C vs. Java Features

q Java

  • Object-oriented
  • Portable
  • Secure

q C

  • Efficient
  • Flexible
  • Lower level (closer to the system)
slide-21
SLIDE 21
slide-22
SLIDE 22