Comparison of C# and Java language / PDF
Comparison of C# and Java language / PDF
Introduction
Comparison of C# and Java language / PDF
Introduction
Many
programming languages exist today: C, C++, Microsoft Visual Basic®, COBOL, C#, Java, and
so on. With so many languages, how does a software engineer decide which one to
use for a project? Sometimes, a language is chosen because the developers of a
company like or know it, which may be reasonable. Sometimes, a language is used
because it is the latest and greatest, and this becomes a marketing tool to
generate more public-relations interest in a product, which may not be
reasonable in and of itself. In an ideal world, a programming language should
be chosen based upon its strengths for performing a certain task the problem to solve
should determine the language to use.
This
paper would quickly become a book or series of books if it attempted to compare
strengths and weaknesses, platform support, and so on for many programming
languages. Rather, to limit its scope, it will attempt to compare only C# and
Java. Some languages, such as C++ and Pascal, will also be used for comparison,
but only to help demonstrate potential motivations for the creation of the
newer programming languages with their newer features. If some weakness exists
and is exposed in the older language, and then shown to be nonexistent or
hidden in the newer language, this may help understand the motivation for some
change made by the architect of the newer language. Knowing this motivation is
often important, because otherwise it is not possible to objectively critique a
language.
For
example, if a so-called “feature” that existed in a previous language is
removed from the newer language, then a developer may feel that the latter is
not a worthy candidate to use because it doesn’t have the power of the former. This may not be the case; the newer language
may be actually doing him a favor by saving him from falling into some known
trap.
Naturally,
Java came before C#, and C# was not created in a vacuum. It is quite natural that C# learned from both
the strengths and weaknesses of Java, just as Java learned from Objective-C, which
learned from C, etc. So, C# should be
different than Java. If Java were
perfect, then there would have been no reason to create C#. If C# is perfect, then there is no reason to
create any new programming language. The
job would then be done. However, the
future is unclear, and both C# and Java are good object-oriented programming
languages in the present, so they beg to be compared.
It
is important to note that not everything can be covered here. The subject matter is simply too large. But the goal is to give enough information so
as to help managers and software developers make a somewhat-informed choice
about a language to use in certain situations.
Maybe some little language quirk in C# may make someone choose
Java. Maybe some blemish in Java will
influence someone to pick C#. Either
way, this document will attempt to dive deep enough into details to possibly
stir-up some old fool’s gold, or to dig-up some new hidden treasure, which aids
in our goal.
(Items
shaded in gray such as the paragraph below and subsequent sections have been
highlighted to separate my opinion from the rest of the paper.)
While
I am not covering everything about C# and Java in this paper, I will attempt to
supply some in-depth analysis on most of the topics that are covered. I don’t believe that it is generally
worthwhile just to say that some functionality exists in a language and
therefore try to imply that the language is powerful. For example, I could simply say, “C# is a
good language because it is object oriented.”
Naturally, that would assume that an object-oriented language is
automatically good, which, from my experience, not everyone agrees with. So, I feel in this case that I have to show
why writing object-oriented code is good first, which should strengthen the above
assertion. This can get a little
tedious, but I think that it is important.
Also,
I generally do not like to promote anything that I have not used. If I say below that the “language
interoperability using Visual Studio .NET is outstanding because it is very
easy,” then I have run at least some basic tests to really see if it is in fact
“easy.” More than likely, while not
everyone will agree with its opinions, it doesn’t just “wave its hand” by just restating
what others have said; rather it tries to put what others have said to the
test.
What’s similar
between C# and Java?
C#
and Java are actually quite similar, from an application developer’s
perspective. The major similarities of
these languages will be discussed here.
All Objects are
References
Reference
types are very similar to pointers in C++, particularly when setting an
identifier to some new class instance.
But when accessing the properties or methods of this reference type, use
the ‘.’ operator, which is similar to accessing data instances in C++ that are
created on the stack. All class
instances are created on the heap by using the new operator, but delete
is not allowed, as both languages use their own garbage collection schemes,
discussed below.
It
should be noted that actual pointers may be used in C#, but they can only be
manipulated in an unsafe mode, which is discouraged. This paper will not deal with writing
“unsafe” C# code not only because it is discouraged, but also because I have
very little experience using it; maybe even more important, because the
comparisons with Java will nearly vanish as Java does not support anything like
it.
Garbage Collection
How
many times have you used the new
keyword in C++, then forgot to call delete
later on? Naturally, memory leaks are a
big problem in languages like C++. It’s
great that you can dynamically create class instances on the heap at run-time,
but memory management can be a headache.
Both
C# and Java have built-in garbage collection.
What does this mean? Forgetaboutit! At least forget about calling delete.
Because if you don’t forget, the compiler will remind you! Or worse, Tony might make you a Soprano. Don’t be a wise guy; neither language gives
you the permission to whack any Object that’s become expendable. But you may be asked to call new fairly often, maybe more than you’d
like. This is because all Objects are
created on the heap in both languages, meaning that the following is frowned on
in either language:
class
BadaBing
{
public BadaBing()
{
}
}
BadaBing
badaBoom(); //You can’t create temporary
data but you must use parens on a constructor
The
compiler will send a little message to you about this because you are
attempting to create temporary storage. See,
there’s this thing you gotta do:
BadaBing
badaBoom = new BadaBing();
Now
badaBoom is made and has at least
one reference. Later on, you might be
tempted to get rid of him yourself:
delete
badaBoom; //illegal in C# and Java – the
compiler will complain
Use
badaBoom as long as you want, then
the garbage collector will dispose of him for you when you decide to give
someone else your reference. Waste
management is a beautiful thing..............
0 commentaires: