C# and Java Similarities - Computer Science & Engineering

Download Report

Transcript C# and Java Similarities - Computer Science & Engineering

C#
Amy Broom
Hossam Ragaban
Randal Rollings
Fall 2003
History


The history of C# begins early in 1970’s when C was
invented in which UNIX was written. But C had some
drawbacks. The power that C affords the programmer
makes it easy for him to blunder. For example, it allows the
programmer to directly allocate memory and permits him to
deallocate this memory.
Then came C++ in the 1980’s, who like its predecessor
took the world by a storm. Despite it being object-oriented
C++ had some problems. Veteran programmers often had
problems migrating from C (structured) to C++ ( objectoriented ) because effective C++ programming requires a
completely new mindset.
More History


Sun created Java. They were motivated with the World
Wide Web. They sought a single environment that would
target multiple platforms. Programs in Java are not
compiled to machine code, but instead to an intermediate
language or bytecode that can be executed on any
computer equipped with a suitable interpreter program
(The Java Runtime).
Today, Microsoft unveils an alternative strategy for bringing
the development and web worlds together and online with
.NET. Because all .NET languages compile to a common
bytecode, a developer can program in whichever language
he wants and share his works with other developers using
other .NET languages.
Even More History
In June 2000, Microsoft announced both the .NET platform and a
new programming language called C#. C# is a strongly-typed
object-oriented language designed to give the optimum blend of
simplicity, expressiveness, and performance. The .NET platform
is centered around a Common Language Runtime (similar to a
JVM) and a set of libraries which can be exploited by a wide
variety of languages which are able to work together by all
compiling to an intermediate language (IL). C# and .NET are a
little symbiotic: some features of C# are there to work well with
.NET, and some features of .NET are there to work well with C#
(though .NET aims to work well with many languages). The C#
language was built with the hindsight of many languages, but
most notably Java and C++. It was co-authored by Anders
Hejlsberg (who is famous for the design of the Delphi language),
and Scott Wiltamuth.
Common Language
Runtime




The CLR serves as an execution engine for
applications.
Every byte of code that you write for the
framework runs in the CLR.
CLR consists of instructions written in CIL(common
intermediate language-pseudo-machine language).
CIL instructions are just-in-time and compiled into
native machine code at run time.
A given method is compiled only once-the first time
it is called-and thereafter cached in memory so it
can be executed again without delay.
Advantages
This robust development package, which uses the component
oriented C# development language, offers beginning and
intermediate developers with C++ or Java experience a modern
language and environment for creating next-generation software.
Visual C# .NET 2003 delivers superior functionality for
streamlining business processes, including:




Rapid design, development, and deployment support for
creating and consuming Web services.
Form designers and visual controls for creating rich Windowsbased applications.
Authoring tools and services for building powerful
Microsoft .NET server-based solutions.
Migration tools for converting Java-based projects to the
Microsoft .NET development environment
Advantages
With Visual C# .NET 2003, developers can
build solutions for a wide range of clients;
including Windows, the Web, and mobile or
embedded devices. Using this elegant
programming language and its tools,
developers can leverage their existing C++
and Java-language skills and knowledge to be
successful in the .NET environment.
Improvements
C# offers significant improvements
including: unified type system, powerful
new language constructs which are easily
understood by developers, and “unsafe”
code of maximum developer control. The
language provides inherent support for
properties, indexers, versioning, operator
overloading, delegates, and custom
attributes.
C# and Java Similarities







Compiles into machine-independent languageindependent code which runs in a managed execution
environment.
Garbage Collection coupled with the elimination of
pointers (in C# restricted use is permitted within code
marked unsafe)
No header files, all code scoped to packages or
assemblies, no problems declaring one class before
another with circular dependencies
Classes all descend from object and must be allocated on
the heap with new keyword
Thread support by putting a lock on objects when
entering code marked as locked/synchronized
Interfaces, with multiple-inheritance of interfaces, single
inheritance of implementations
Inner classes
C# and Java Similarities
cont.







No concept of inheriting a class with a
specified access level
No global functions or constants, everything
belongs to a class
Arrays and strings with lengths built-in and
bounds checking
The "." operator is always used, no more >, :: operators
null and boolean/bool are keywords
All values are initialized before use
Try Blocks can have a finally clause
Main Features
1.
Pointers are missing in C#.
2.
Unsafe operations such as direct memory manipulation are
not allowed.
3.
In C# there is no usage of "::" or "->" operators.
4.
Since it`s on .NET, it inherits the features of automatic
memory management and garbage collection.
5.
Integer values of 0 and 1 are no longer accepted as boolean
values.Boolean values are pure true or false values in C# so
no more errors of "="operator and "=="operator. "==" is
used for comparison operation and "=" is used for assignment
operation.
Testing and Debugging
Testing and debugging tools are of the
languages features providing a
powerful remote and multi-language
debugger that allows developers to
test applications and build reliable
multi-tier solutions that span process
boundaries and are written in multiple
programming languages.
Delegates
A delegate can be thought of as a type-safe objectoriented function pointer, which is able to hold multiple
methods rather than just one. Delegates handle
problems which would be solved with function pointers
in C++, and interfaces in Java. It improves on the
function pointer approach by being type safe and being
able to hold multiple methods. It improves on the
interface approach by allowing the invocation of a
method without the need for inner-class adapters or
extra code to handle multiple-method invocations.
Events
C# provides direct support for events. Although event
handling has been a fundamental part of programming
since programming began, there has been surprisingly
little effort made by most languages to formalize this
concept. If you look at how today's mainstream
frameworks handle events, we've got examples like
Delphi's function pointers (called closures), Java's inner
class adaptors, and of course, the Windows API's message
system. C# uses delegates along with the event keyword
to provide a very clean solution to event handling.
Properties


Properties will be a familiar concept to Delphi and Visual Basic
users. The motivation is for the language to formalize the concept
of getter/setter methods, which is an extensively used pattern,
particularly in RAD (Rapid Application Development) tools.
This is typical code you might write in Java or C++:
foo.setSize (getSize () + 1);
label.getFont().setBold (true);

The same code you would write like this in C#:
foo.size++;
label.font.bold = true;
Polymorphism
Virtual methods allow object oriented
languages to express polymorphism. This
means a derived class can write a method
with the same signature as a method in its
base class, and the base class will call the
derived class's method. By default in Java, all
methods are virtual. In C#, like C++, the
virtual keyword must be used so that the
method will be called by the base class.
Foreach Statement
C# provides a shorthand for for-loops:

In Java or C++:
for (int i = 0; i < array.length; i++)...

In C#:
foreach (int i in array)...
Bibliography

http://msdn.microsoft.com/vcsharp/productinfo/features/
default.aspx

http://genamics.com/developer/csharp_comparative.htm

http://www.csharphelp.com/archives/archive55.html

http://www.funducode.com/csharp/basics/basics1.htm

http://msdn.microsoft.com/vcsharp/productinfo/overview
/default.aspx