Carnegie Mellon Worcester Polytechnic Institute Differences between Java
Carnegie Mellon Worcester Polytechnic Institute Differences between Java and C Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, Absolute C++, by Walter Savitch, The C++ Programming Language, Special Edition, by Bjarne Stroustrup, and from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel) CS-2303, A-Term 2012 Differences between Java and C 1
Carnegie Mellon Worcester Polytechnic Institute Java and C ¢ Java is derived from C++, hence from C ¢ Many of its syntactic characteristics are similar to C ¢ However, there are some huge differences CS-2303, A-Term 2012 Differences between Java and C 2
Carnegie Mellon Worcester Polytechnic Institute Expressions ¢ Arithmetic operators are the same: – +, –, *, /, %, ++, – – ¢ ¢ Numerical type conversion is mostly the same § Java spells out divide by zero, Na. N (not a number, etc. ) § C & C++ are machine dependent Check the textbooks for details CS-2303, A-Term 2012 Differences between Java and C 3
Carnegie Mellon Worcester Polytechnic Institute Relational Operators ¢ Relational operators work the same way but return different results: – >, >=, <, <=, ==, != ¢ In Java, they return values FALSE and TRUE ¢ C/C++ operators return values 0 and 1 ¢ nt a t r ! o p In C/C++, m ry i e V § a value of zero means “false” § any value that is not zero means “true” § E. g. , 1, 5, -1000000, 3. 14159, 6. 626068 × 10 -34 CS-2303, A-Term 2012 Differences between Java and C 4
Carnegie Mellon Worcester Polytechnic Institute Conditional and Bitwise Operators ¢ Conditional execution operators are same in Java and C/C++: – ||, &&, ? followed by : ¢ Bitwise operators are same in Java and C/C++: – |, &, ^ for bit-by-bit operations within a word ¢ Shift operators differ a little bit << (left shift) is the same >> (right shift) is machine dependent in C/C++ § I. e. , whether to fill from left with zeros or sign bits CS-2303, A-Term 2012 Differences between Java and C 5
Carnegie Mellon Worcester Polytechnic Institute Assignment and Unary Operators ¢ Assignment operators work the same: – =, +=, –=, *=, /=, &=, |=, ^= ¢ The following unary operators are available C/C++ but not in Java ~ * & (type) sizeof –> CS-2303, A-Term 2012 invert the bits of a word pointer dereference pointer creation cast (i. e. , forceable type conversion) # of bytes in operand or data type pointer dereference with field selection Differences between Java and C 6
Carnegie Mellon Worcester Polytechnic Institute Summary about Expressions and Operators ¢ Pretty much the same in C/C++ and Java ¢ Be sure to check details in textbook ¢ Be sure to check operator precedence Table 2 -1 in K&R (p. 53) § Figure 2. 3 in Absolute C++ § Note: Absolute C++ uses the term “Display” to mean “figure” CS-2303, A-Term 2012 Differences between Java and C 7
Carnegie Mellon Worcester Polytechnic Institute Statements ¢ Statements in C/C++: – § § § CS-2303, A-Term 2012 Labeled statement Expression statement Compound statement Selection statement Iteration statement Jump statement Differences between Java and C 8
Carnegie Mellon Worcester Polytechnic Institute Statements ¢ Statements in C/C++: – § § § CS-2303, A-Term 2012 Labeled statement Expression statement Compound statement Selection statement Iteration statement Jump statement E. g. , cases of a switch statement Similar to Java Differences between Java and C 9
Carnegie Mellon Worcester Polytechnic Institute Statements ¢ Statements in C/C++: – § § § CS-2303, A-Term 2012 Labeled statement Expression statement Compound statement Selection statement Iteration statement Jump statement Differences between Java and C Any expression followed by '; ' Much like to Java 10
Carnegie Mellon Worcester Polytechnic Institute Statements ¢ Statements in C/C++: – § § § CS-2303, A-Term 2012 Labeled statement Expression statement Compound statement Selection statement Iteration statement Jump statement Differences between Java and C Sequence of statements enclosed in "{}" Called a “block” in Java 11
Carnegie Mellon Worcester Polytechnic Institute Statements ¢ Statements in C/C++: – § § § CS-2303, A-Term 2012 Labeled statement Expression statement Compound statement Selection statement Iteration statement Jump statement switch (expr) {cases} if (expr) statement else statement Same as in Java Differences between Java and C 12
Carnegie Mellon Worcester Polytechnic Institute Statements ¢ Statements in C/C++: – § § § CS-2303, A-Term 2012 Labeled statement Expression statement Compound statement Selection statement Iteration statement Jump statement while (expr) statement do statement while (expr); for (exp 1; exp 2, exp 3) statement Very similar to Java Differences between Java and C 13
Carnegie Mellon Worcester Polytechnic Institute Statements ¢ Statements in C/C++: – § § § Labeled statement Expression statement Compound statement Selection statement Iteration statement Jump statement break; continue; return expr; Very similar to Java goto Not present in Java! Not allowed in this course CS-2303, A-Term 2012 Differences between Java and C 14
Carnegie Mellon Worcester Polytechnic Institute Summary about Statements ¢ ¢ Pretty much the same in C/C++ and Java Be sure to check details in textbooks and/or reference manuals CS-2303, A-Term 2012 Differences between Java and C 15
Carnegie Mellon Worcester Polytechnic Institute Formatted Input & Output ¢ Very different between C and Java Very different between C and C++ ¢ Handled by library functions in C ¢ § § § CS-2303, A-Term 2012 printf() scanf() getc() putc() Many others! Differences between Java and C 16
Carnegie Mellon Worcester Polytechnic Institute printf() – Print formatted data ¢ ¢ ¢ printf("string containing '%' specifiers", expr 1, expr 2, expr 3, …); Copy the string, character-by-character, to the output stream When the ith '%' is encountered, treat it as a conversion specifier for converting the value of expri § ¢ Copy the converted value to the output per instructions encoded in the conversion specifier Return number of characters printed CS-2303, A-Term 2012 Differences between Java and C 17
Carnegie Mellon Worcester Polytechnic Institute printf() conversion specifiers ¢ ¢ ¢ %d or %i K&R, pp 154 & 244 § Treat expression as a decimal number (with sign) § Treat expression as unsigned decimal number § Treat expression as double precision floating point number; print without exponent %u %f %e or %E § Treat expression as double precision floating point number; print with exponent (base 10) — scientific notation § Treat value of expression as the code for a single character § Treat expression as a pointer to a string %c %s CS-2303, A-Term 2012 Differences between Java and C Later in this course 18
Carnegie Mellon Worcester Polytechnic Institute printf() conversion specifiers (continued) ¢ Conversion specifiers may optionally contain Right or left alignment in the field § Minimum field width (padded on right or left) § Precision – i. e. , § – Maximum length of string – Number of decimal places of floating point value § Size of data type – E. g. , short, double, float, etc. ¢ Examples %6 d – print signed decimal number in 6 -char field %8. 4 f – print 64 -bit floating point number with four places after decimal point, field width of 8 characters %hu – print an unsigned short integer (i. e. , 16 bits in Pentium) CS-2303, A-Term 2012 Differences between Java and C 19
Carnegie Mellon Worcester Polytechnic Institute scanf() — Scan formatted data ¢ ¢ ¢ scanf("string containing '%' specifiers", &var 1, &var 2, &var 3, …); Scan the input, matching the string character by character Note ampersands When the ith '%' is encountered, treat as a conversion specifier for converting next sequence of characters and storing result in vari § ¢ ¢ Copy the converted value to the output per instructions encoded in the conversion specifier Stop if input does not match string OR if conversion was not successful Return number of successful conversions CS-2303, A-Term 2012 Differences between Java and C 20
Carnegie Mellon Worcester Polytechnic Institute scanf() – Typical Usage int j; double x; scanf("%d%f", &j, &x); ¢ ¢ ¢ Scan the input, skipping blanks and tabs Try to match a signed integer; if successful, store result in j and continue Continue scanning, skipping blanks and tabs Try to match a floating point number. If successful, store in x Stop if unsuccessful or new-line encountered Return number of items stored CS-2303, A-Term 2012 Differences between Java and C 21
Carnegie Mellon Worcester Polytechnic Institute scanf() Details ¢ K&R Table B-2 (p. 246) printf() and scanf() are both needed for Lab #1 and Programming Assignment #1 CS-2303, A-Term 2012 Differences between Java and C 22
Carnegie Mellon Worcester Polytechnic Institute Summary ¢ Differences and similarities between Java and C Expressions § Statements § ¢ There are lots of other differences § CS-2303, A-Term 2012 Will be covered during the course Differences between Java and C 23
Carnegie Mellon Worcester Polytechnic Institute Questions? Next Topic CS-2303, A-Term 2012 Differences between Java and C 24
- Slides: 24