Anda di halaman 1dari 5

Homework

Programming Languages
Adarsh Burma

Comparison between java and C based on design goals:

Simplicity and readability: C is simpler than java for


instance take feature multiplicity (having more than one way to
perform a particular operation) is often confusing.
o For example, in C++ or Java you can decrement a
variable in four different ways: x = x 1; x -= 1; x--; --x

But in C referencing the same memory cell with more than one
name
E.g., in C, both x and y can be used to refer to the
same memory cell
int x = 5;int *y = &x;

Clarity about binding: Both C and JAVA are statically typed


meaning that types are checked at compile time, and a
program that does not have a static type is rejected by the
compiler.
Examples : In JAVA the last two statements would raise type
exceptions as it is strongly typed.

a=9
b="9"
c=concatenate(a,b)//produces"99"
d=add(a,b)//produces18

To avoid these exceptions, some kind of explicit type


conversion would be necessary, like this.
a=9

b="9"

c=concatenate(str(a),b)

d=add(a,int(b))

C can also be considered strongly typed but it has some


loopholes like you can freely cast any pointer type to any
other pointer type.
Example:
int a = 5;
int* intPtr = &a;
char* charPtr = (char*) intPtr;

However, in general this invokes undefined behavior (though it


happens to work on many platforms). This said, there seem to be
some exceptions:

you can cast to and from void* freely


you can cast to and from char* freely

Reliability: With C the compiler is producing "native" code


(more like assembler code) for the machine it will run on. It's
reliable unless it produces incorrect code in that regard. The
java compiler produces "intermediate" code which is code that
runs on a JVM. That stands for "java virtual machine". So
besides the compiler you are also depending on the JVM where
the code runs to be reliable.

Support: I see both have good number of IDEs available in


the market and even tutorials but it always depends on the
domain you are looking out for networking machine learning
domains have good JAVA support where as graphics; scientific
simulations etc have good C support OpenGL widely used for
such stuff. Java has well documented APIs available online for
end users easily to use and understand which is example
oriented. I personally like as a JAVA developer.

Abstraction: JAVA has high level of abstraction than C. For


instance consider the collections used in JAVA like hash, trees
etc. they must be using nodes with pointers internally this
allows the details to be ignored and code to be reused instead

of repeated but in C we dont have such feature.


Implementation details are separated from the interface,
allowing them to be changed without re-writing all code using
Abstract Data types in object oriented languages like JAVA.

Orthogonality : Lack of Orthogonality in C:


An array can contain any data type except void
Parameters are passed by value but array are passed
by reference

More formally, an orthogonal language would have exactly ONE


way to do a given operation. Non-orthogonal languages would
have more than one way to achieve the same effect.
Simplest example:
for loop; vs. while loop;
for and while are non-orthogonal. Common violation in both C and
JAVA.
In Java you'd have to evaluate for example if there is a
combination of keywords/constructs that could affect each other
when used simultaneously on an identifier. For example when
applying public and static to a method, they do not interfere
with each other, so these two are orthogonal (no side effects
besides what the keyword is intended to do).
My conclusion is we cant decide which is more orthogonal unless
we evaluate all features in these two languages in terms of
orthogonality.

Efficient implementation : C prepares a native code can


directly interact with hardware were as JAVA runs on a virtual
machine time to load virtual machine dominates especially
during short runtimes. Memory can be allocated deallocated
using C but garbage collector comes into play in case of JAVA
thus all C applications can manage memory effectively in more
finer ways than java.

Anda mungkin juga menyukai