Review:
difference in behavior between C and Java in
the assignment statement
- Java will
only allow
"safe" assignments
for number data types:
int x;
double y;
Allowed:
y = x;
Not allowed (without casting):
x = y; // Need: x = (int) y;
|
- In contrast,
C is
always allow
assignment of
any number type value
int x;
double y;
Allowed:
y = x;
x = y; // Without casting !
|
|
Difference in behavior between C and Java
in
function invocations using
mismatched types
- The
same behavior
applies
when you
invoke
a function
using
values of
different data type
than
specified in the
function signature:
- Java will
allow a
function call
if:
- The data type of
the actual parameter
can
safely be
converted to
the data type of
the formal parameter
|
- C will
always
allow the
function call
But remember that:
- C will
convert
the actual parameter
to the data type of the
formal parameter
if
they are different number types
|
|
|
Example function calls that shows the
difference between C and Java
- The following
C program will
compile and
run
without
casting:
int square(int x)
{
return x*x;
}
int main( )
{
float a = 4, b;
b = square(a); // C compiler will convert (float) a to (int) !
printf("Square of %f = %f\n", a, b);
}
|
|
DEMO:
demo/C/set1/param1.c
Example function calls that shows the
difference between C and Java
DEMO:
demo/C/set1/param1.java
❮
❯