and some big differences Some August 30, 016 object-oriented vs procedural non-interpreted vs interpreted memory management references vs. free, unrestricted pointers error handling A Very Simple Program A Very Simple Program 1 public class Welcome { public static void main(string args[]) { 3 System.out.println("Welcome to IS 107"); 4 5 1 3 4 printf("welcome to IS 107\n"); 5 6 1 public class Welcome { public static void main(string args[]) { 3 System.out.println("Welcome to IS 107"); 4 5 s main( ) starting point of the program returns int, not void return status 0 OK 1 3 4 printf("welcome to IS 107\n"); 5 6 int argc, char **argv same as s String args
A Very Simple Program ompiling and Running in Stuff.java 1 public class Welcome { public static void main(string args[]) { 3 System.out.println("Welcome to IS 107"); 4 5 #include 1 3 4 printf("welcome to IS 107\n"); 5 6 idea not unlike import. different mechanism preprocessor. text mangling public class Stuff { javac Stuff.class 1101 0101 0110 1000 java PU ompiling and Running in... but it s really not that bad printf.o formal gcc -o executable_file_name source_file_name welcome.c Source program (text) Preprocessor (cpp) welcome.i Modified source program (text) ompiler (gcc) welcome.s Assembler (as) Assembly program (text) welcome.o Linker (ld) Relocatable object programs (binary) welcome Executable object program (binary)
... but it s really not that bad... but it s really not that bad formal gcc -o executable_file_name source_file_name formal gcc -o executable_file_name source_file_name example gcc -o stuff stuff.c example gcc -o stuff stuff.c and to execute./stuff Data Types Primitives primitives all types objects integer types byte short int long floating-point types float double characters char boolean boolean
Primitives vs. Types Primitives Sizes integer types byte short int long floating-point types float double characters char boolean boolean integer types char short int long floating-point types float double characters char boolean any integer type type size (bytes) byte 1 short int 4 long 8 float 4 double 8 char boolean 1 bit Why are About Size? Why are About Size? For example, if s byte and long can both represent integers, why use one instead of the other? For example, if s byte and long can both represent integers, why use one instead of the other? large size > values you can represent small size < memory used More on this later
Types Sizes Printing a String type size (bytes) char 1 short? int? long? float? double? printf("welcome to IS 107\n"); Format Strings Format Strings int x=10; printf("x is %d\n", x); int x=10; printf("x is %d\n", x); placeholder
Format Strings Format Strings. Multiple Placeholders int x=10; printf("x is %d\n", x); int x=10, y=0, z=30; printf("x is %d, y=%d, z=%d\n, x, y, z); placeholder x is 10 Format Strings. Multiple Placeholders Format Strings. Different Formats int x=10, y=0, z=30; printf("x is %d, y=%d, z=%d\n, x, y, z); int x=10, y=0, z=30; printf("x is %x, y=%x, z=%x\n", x, y, z); x is 10, y is 0, z is 30
Format Strings. Different Formats What Was the Deal with char Again? int x=10, y=0, z=30; char x=65; printf("x is %x, y=%x, z=%x\n", x, y, z); x++; printf("x is %d\n", x); x is a, y=14, z=1e What Was the Deal with char Again? an Also Be Used to Represent haracters char x=65; x++; printf("x is %d\n", x); char x= A ; printf("x is %c\n", x); x is 66
an Also Be Used to Represent haracters if char x= A ; printf("x is %c\n", x); x is A 1 if (some condition) statement; 3 4 if (some other condition) { 5 statement_1; 6 statement_; 7... 8 statement_n; 9 1 if (some condition) statement; 3 4 if (some other condition) { 5 statement_1; 6 statement_; 7... 8 statement_n; 9 if-else for loop 1 if (some condition) statement_1; 3 else 4 statement_; 5 6 if (some condition) { 7 statement_1; 8 statement_; 9... 10 else { 11 statement_3; 1 statement_4; 13... 14 1 if (some condition) statement_1; 3 else 4 statement_; 5 6 if (some condition) { 7 statement_1; 8 statement_; 9... 10 else { 11 statement_3; 1 statement_4; 13... 14 Also the same as generic 1 for (initial condition; test; update) statement;
for loop for loop Also the same as generic 1 for (initial condition; test; update) statement; example 1 for (i=0; i<len; i++) A[i]+=; Also the same as generic 1 for (initial condition; test; update) statement; example 1 for (i=0; i<len; i++) A[i]+=; another example 1 for (i=0, j=len-1; i<j; i++, j--) swap(a, i, j); Loop ontrol Variables boolean public static void main(string args[]) { for (int i=0; i<max; i++) { for (int i=max; i>0; i--) { for (int i=0; i<thresh; i++) { int i; for (i=0; i<max; i++) { for (i=max; i>0; i--) { expression result result 10<0 true 1 10>0 false 0 for (i=0; i<thresh; i++) {
means you can have things like but also means... if (1) { /* always runs */ if (0) { /* never runs */ 1 int x=10, y=0; 3 if (x=y) { 4 printf("equal\n"); 5 else { 6 printf("not equal\n"); 7 int i=50; while (i) { i--; but also means... so then you get confused, angry and add 1 int x=10, y=0; 3 if (x=y) { 4 printf("equal\n"); 5 else { 6 printf("not equal\n"); 7 equal 1 int x=10, y=0; 3 if (x=y) { 4 printf("equal\n"); 5 else { 6 printf("not equal\n"); 7 8 9 printf("x=%d, y=%d\n", x, y);
so then you get confused, angry and add so then you get confused, angry and add 1 int x=10, y=0; 3 if (x=y) { 4 printf("equal\n"); 5 else { 6 printf("not equal\n"); 7 8 9 printf("x=%d, y=%d\n", x, y); equal x=0, y=0 1 int x=10, y=0; 3 if (x=y) { 4 printf("equal\n"); 5 else { 6 printf("not equal\n"); 7 8 9 printf("x=%d, y=%d\n", x, y); What s Happening? assignment then test no compiler error Things that are almost completely the same in and Arrays if, if-else for, while, do-while switch though not Strings. operators +, -, mostly comments: mostly /* supported everywhere */ // mostly supported OK int A[5]; int A[]={10,0,30,40,50;
Arrays Arrays OK int A[5]; int A[]={10,0,30,40,50; Not OK int A[]; int []A={10,0,30,40,50; OK int A[5]; int A[]={10,0,30,40,50; Not OK int A[]; int []A={10,0,30,40,50; Legal but will get you in trouble int A[10]; A[15]=555; A[-3]=5; Arrays What do we pass when we pass an array in? size no.length field pass length with array