Download - Differences Between C and Java
-
8/10/2019 Differences Between C and Java
1/24
Differences between Java
and C
CS-2303, C-Term 2010 1
Differences between Java and C
CS-2303, System Programming Concepts
(Slides include materials from The C Programming Language, 2ndedition, by Kernighan and Ritchie and
from C: How to Program, 5thand 6theditions, by Deitel and Deitel)
-
8/10/2019 Differences Between C and Java
2/24
Differences between Java
and C
CS-2303, C-Term 2010 2
Java and C
Java is derived from C
Many of its syntactic characteristics are
similar to C
However, there are some huge differences
-
8/10/2019 Differences Between C and Java
3/24
Differences between Java
and C
CS-2303, C-Term 2010 3
Expressions
Arithmetic operators are the same:
+, , *, /, %, ++,
Numerical type conversion is mostly thesame
Java spells out divide by zero,NaN(not anumber, etc.)
C & C++ are machine dependent
Check the textbooks for details
-
8/10/2019 Differences Between C and Java
4/24
Differences between Java
and C
CS-2303, C-Term 2010 4
Relational Operators
Relational operators work the same way but return
different results:
>, >=,
-
8/10/2019 Differences Between C and Java
5/24
Differences between Java
and C
CS-2303, C-Term 2010 5
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 with a word
Shift operators differ a little bit
> (right shift)is machine dependent in C/C++
I.e., whether to fill from left with zeros or sign bits
-
8/10/2019 Differences Between C and Java
6/24
Differences between Java
and C
CS-2303, C-Term 2010 6
Assignment and Unary Operators
Assignment operators work the same:
=, +=, -=, *=, /=, &=, |=, ^=
The following unary operators are availableC/C++ but not in Java
~ invert the bits of a word
* pointer dereference
& pointer creation
(type) cast (i.e., forceable type conversion)
sizeof # of bytes in operand or data type
-> pointer dereference with field selection
-
8/10/2019 Differences Between C and Java
7/24
Differences between Java
and C
CS-2303, C-Term 2010 7
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
Distributed throughout text in D&D
-
8/10/2019 Differences Between C and Java
8/24
Differences between Java
and C
CS-2303, C-Term 2010 8
Statements
Statements in C/C++: Labeled statement
Expression statement
Compound statement Selection statement
Iteration statement
Jump statement
-
8/10/2019 Differences Between C and Java
9/24
Differences between Java
and C
CS-2303, C-Term 2010 9
Statements
Statements in C/C++: Labeled statement
Expression statement
Compound statement Selection statement
Iteration statement
Jump statement
E.g., cases of a switch
statement
Similar to Java
-
8/10/2019 Differences Between C and Java
10/24
Differences between Java
and C
CS-2303, C-Term 2010 10
Statements
Statements in C/C++: Labeled statement
Expression statement
Compound statement Selection statement
Iteration statement
Jump statement
Any expressionfollowed by ';'
Much like to Java
-
8/10/2019 Differences Between C and Java
11/24
Differences between Java
and C
CS-2303, C-Term 2010 11
Statements
Statements in C/C++: Labeled statement
Expression statement
Compound statement Selection statement
Iteration statement
Jump statement
Sequence of statementsenclosed in "{}"
Called a block in Java
-
8/10/2019 Differences Between C and Java
12/24
Differences between Java
and C
CS-2303, C-Term 2010 12
Statements
Statements in C/C++: Labeled statement
Expression statement
Compound statement Selection statement
Iteration statement
Jump statement
switch (expr)if (expr)statement
if (expr) statement
else statement
Same as in Java
-
8/10/2019 Differences Between C and Java
13/24
Differences between Java
and C
CS-2303, C-Term 2010 13
Statements
Statements in C/C++: Labeled statement
Expression statement
Compound statement Selection statement
Iteration statement
Jump statement
while (expr) statement
do statement while (exp
for (exp1; exp2, exp3)
statementVery similar to Java
-
8/10/2019 Differences Between C and Java
14/24
Differences between Java
and C
CS-2303, C-Term 2010 14
Statements
Statements in C/C++: Labeled statement
Expression statement
Compound statement Selection statement
Iteration statement
Jump statementbreak;
continue;
return expr
Very similar to Java
goto
Not present in Java
Not allowed in thiscourse
-
8/10/2019 Differences Between C and Java
15/24
Differences between Java
and C
CS-2303, C-Term 2010 15
Summary about Statements
Pretty much the same in C/C++ and Java
Be sure to check details in textbooks
-
8/10/2019 Differences Between C and Java
16/24
Differences between Java
and C
CS-2303, C-Term 2010 16
Formatted Input & Output
Very different between CandJava
Very different between Cand C++
Handled by library functions in Cprintf()
scanf()
getc()
putc()
Many others!
-
8/10/2019 Differences Between C and Java
17/24
Differences between Java
and C
CS-2303, C-Term 2010 17
printf()Print formatted data
printf("string containing '%' specifiers",expr1, expr2, expr3, );
Copy the string, character-by-character, tothe output.
When the ith'%'is encountered, treat it as aconversion specifierfor converting the valueof expri
Copy the converted value to the output perinstructions encoded in the conversion specifier
Return number of characters printed
-
8/10/2019 Differences Between C and Java
18/24
Differences between Java
and C
CS-2303, C-Term 2010 18
printf()conversion specifiers
See K&R, p 154 & 244 or D&D Chapter 9 %d or%i
Treat expression as a decimal number (with sign)
%u Treat expression as unsigned decimal number
%f Treat expression as double precision floating point number; print
without exponent
%e or%E Treat expression as double precision floating point number; print with
exponent (base 10) scientific notation
%c Treat value of expression as the code for a single character
%s Treat expression as a pointer to a string
Later in this
course
-
8/10/2019 Differences Between C and Java
19/24
Differences between Java
and C
CS-2303, C-Term 2010 19
printf()conversion specifiers (continued)
Conversion specifiers may optionallycontain
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
Examples%6dprint signed decimal number in 6-char field%8.4fprint floating point number with four places
after decimal point, field width of 8 characters
-
8/10/2019 Differences Between C and Java
20/24
Differences between Java
and C
CS-2303, C-Term 2010 20
scanf()Scan formatted data
scanf("string containing '%' specifiers",&var1, &var2, &var3, );
Scan the input, matching the string character bycharacter.
When the ith'%'is encountered, treat as aconversion specifierfor converting next sequenceof characters and storing result in vari
Copy the converted value to the output per instructions encodedin the conversion specifier
Stop if input does not match string or conversionnot successful
Return number of successful conversions.
-
8/10/2019 Differences Between C and Java
21/24
Differences between Java
and C
CS-2303, C-Term 2010 21
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, storeresult in j
Continue scanning, skipping blanks and tabs
Try to match a floating point number. If successful,store in x
Return number of items stored.
-
8/10/2019 Differences Between C and Java
22/24
Differences between Java
and C
CS-2303, C-Term 2010 22
scanf()Details
K&R Table B-2 (p. 246), D&D Chapter 9
printf()and scanf()are bothneeded for
Lab #1
Programming Assignment #1
-
8/10/2019 Differences Between C and Java
23/24
Differences between Java
and C
CS-2303, C-Term 2010 23
Summary
Differences and similarities between Java
and C Expressions
Statements
There are lotsof other differences Will be covered during the course
-
8/10/2019 Differences Between C and Java
24/24
Differences between Java
and C
CS-2303, C-Term 2010 24
Questions?