Sie sind auf Seite 1von 24

Differences between Java and C

CS-2303, System Programming Concepts


(Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie and
from C: How to Program, 5th and 6th editions, by Deitel and Deitel)

CS-2303, C-Ter
m 2010

Differences be
tween Java and

Java and C
Java is derived from C
Many of its syntactic characteristics are
similar to C
However, there are some huge differences

CS-2303, C-Ter
m 2010

Differences be
tween Java and

Expressions
Arithmetic operators are the same:
+, , *, /, %, ++,

Numerical type conversion is mostly the


same
Java spells out divide by zero, NaN (not a
number, etc.)
C & C++ are machine dependent

Check the textbooks for details


CS-2303, C-Ter
m 2010

Differences be
tween Java and

Relational Operators
Relational operators work the same way but return
different results:
>, >=, <, <=, ==, !=

In Java, they return values FALSE and TRUE


n t!
In C/C++, they return values 0 and 1
a
t
or
p
im
y
In C/C++,
r
Ve
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, C-Ter
m 2010

Differences be
tween Java and

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


(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, C-Ter
m 2010

Differences be
tween Java and

Assignment and Unary Operators


Assignment operators work the same:
=, +=, -=, *=, /=, &=, |=, ^=

The following unary operators are available C/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
CS-2303, C-Ter
m 2010

Differences be
tween Java and

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

CS-2303, C-Ter
m 2010

Differences be
tween Java and

Statements
Statements in C/C++:

Labeled statement
Expression statement
Compound statement
Selection statement
Iteration statement
Jump statement

CS-2303, C-Ter
m 2010

Differences be
tween Java and

Statements
Statements in C/C++:

Labeled statement
Expression statement
Compound statement
Selection statement
Iteration statement
Jump statement

CS-2303, C-Ter
m 2010

Differences be
tween Java and

E.g., cases of a switch


statement
Similar to Java

Statements
Statements in C/C++:

Labeled statement
Expression statement
Compound statement
Selection statement
Iteration statement
Jump statement

CS-2303, C-Ter
m 2010

Differences be
tween Java and

Any expression
followed by ';'
Much like to Java

10

Statements
Statements in C/C++:

Labeled statement
Expression statement
Compound statement
Selection statement
Iteration statement
Jump statement

CS-2303, C-Ter
m 2010

Differences be
tween Java and

Sequence of statements
enclosed in "{}"
Called a block in Java

11

Statements
Statements in C/C++:

Labeled statement
Expression statement
Compound statement
Selection statement
Iteration statement
Jump statement

CS-2303, C-Ter
m 2010

Differences be
tween Java and

switch (expr)
if (expr)statement
if (expr) statement
else statement
Same as in Java

12

Statements
Statements in C/C++:

Labeled statement
Expression statement
Compound statement
Selection statement
while (expr) statement
do statement while (expr
Iteration statement
for (exp1; exp2, exp3)
Jump statement
statement
Very similar to Java

CS-2303, C-Ter
m 2010

Differences be
tween Java and

13

Statements
Statements in C/C++:

Labeled statement
Expression statement
Compound statement
Selection statement
Iteration statement
goto
break;
Jump statement
Not present in Java
continue;
Not expr
allowed in this
return
course
Very similar
to Java

CS-2303, C-Ter
m 2010

Differences be
tween Java and

14

Summary about Statements


Pretty much the same in C/C++ and Java
Be sure to check details in textbooks

CS-2303, C-Ter
m 2010

Differences be
tween Java and

15

Formatted Input & Output


Very different between C and Java
Very different between C and C++
Handled by library functions in C

printf()
scanf()
getc()
putc()

Many others!
CS-2303, C-Ter
m 2010

Differences be
tween Java and

16

printf() Print formatted data


printf("string containing '%' specifiers",
expr1, expr2, expr3, );

Copy the string, character-by-character, to


the output.
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, C-Ter
m 2010

Differences be
tween Java and

17

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
%f
%e
%c
%s

Treat expression as unsigned decimal number


Treat expression as double precision floating point number; print without
exponent

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


CS-2303, C-Ter
m 2010

Differences be
tween Java and

Later in this
18
course

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

Examples
%6d print signed decimal number in 6-char field
%8.4f print floating point number with four places
after decimal point, field width of 8 characters
CS-2303, C-Ter
m 2010

Differences be
tween Java and

19

scanf() Scan formatted data


scanf("string containing '%' specifiers",
&var1, &var2, &var3, );

Scan the input, matching the string character by


character.
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 conversion


not successful
Return number of successful conversions.
CS-2303, C-Ter
m 2010

Differences be
tween Java and

20

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
Continue scanning, skipping blanks and tabs
Try to match a floating point number. If successful,
store in x
Return number of items stored.
CS-2303, C-Ter
m 2010

Differences be
tween Java and

21

scanf() Details
K&R Table B-2 (p. 246), D&D Chapter 9

printf() and scanf() are both needed for


Lab #1
Programming Assignment #1

CS-2303, C-Ter
m 2010

Differences be
tween Java and

22

Summary
Differences and similarities between Java
and C
Expressions
Statements

There are lots of other differences


Will be covered during the course

CS-2303, C-Ter
m 2010

Differences be
tween Java and

23

Questions?

CS-2303, C-Ter
m 2010

Differences be
tween Java and

24

Das könnte Ihnen auch gefallen