Comparison Between Java And C Information Technology Essay

Many programming languages exist today such as C, C++, C#, Java, Visual Basic, COBOL, Python and many more. It is often important to know what each languages can offer, their advantages and disadvantages, strengths and weaknesses, how mature it is, and support of the language. So how does a developer choose which language to use for a project. In some cases, a company chooses to use a certain language simply because they have programmer of that language. This may lead to wrong use of language, and ultimately unproductively. It is important to know what the purpose of the project is, consider every factor when choosing language. It might not be a bad idea to develop an application using other language which you are less familiar with. In this seminar, I will discuss about two programming language, Java and C#.

It is important to know that, Java and C# has a lot in common, but still has notable difference. If Java is same as C#, then it will negate the purpose of C#, if Java is better than C# or perfect, then there is no reason for C# to exist. Java may have some weaknesses which do not exist in C#, and vice versa.

Introduction to Java

Java is an object oriented programming language developed by James Gosling of Sun Microsystems and released in 1995. Java also shares a lot of similarity with other programming language such as C and C++ such as the syntax. But Java’s object model is much simpler and has lesser low-level facilities. At one point, Java was called “C++ for Internet”, but Internet is not the only place you will find Java. Java is platform independent. It means Java can run on any platform that supports Java Virtual Machine. This is archived by compiling the code into bytecode instead of platform specific machine code. Nowadays, we can see Java on almost anyway, from Linux to Windows, computer to PDA, cellular phone to television. Java is developed with five principles in mind:

Object oriented, familiar and simple

Secure and robust

Portable and architecture neutral

High performance

Threaded, dynamic and interpreted

(Y. Daniel Liang,2009)

Introduction to C#

C# is an object oriented programming language developed by Microsoft. It was first released in 2002. It is designed for Common Language Infrastructure. C# incorporated many strength of other languages such as C++ and the ease-of-use of VB into it. It also has many syntax similar to Java. Programmer of Java, C or C++ can learn C# real quick too. It shares some similarity with Java, such as modern, simple, object oriented and general purpose. (John Lewis,2007) Throughout its development, C# has gone through several versions:

Version

Release Date

.NET Framework

Visual Studio

C# 1.0

January 2002

.NET Framework 1.0

Visual Studio .NET 2002

C# 1.2

April 2003

.NET Framework 1.1

Visual Studio .NET 2003

C# 2.0

November 2005

.NET Framework 2.0

Visual Studio 2005

C# 3.0

November 2007

.NET Framework 3.5

Visual Studio 2008

C# 4.0

April 2010

.NET Framework 4

Visual Studio 2010

(Wikipedia,2010)

COMPARISON JAVA AND C#

Both Java and C# are famous nowadays and some developers might wonder which one is better. In this chapter, we will talk about the differences, pros and cons of Java and C#.

Data Types

Simple/Primitive Type

Both Java and C# has a number of built-in data types that are passed by value instead of by reference. In Java, it is called primitive types and in C#, it is simple types. In C# however, has more simple/primitive type than Java. This is due to C# support of unsigned byte, which is lacking in Java. C# also has a high precision decimal arithmetic type, which is useful for financial application. However, Java library has a arbitrary precision decimal type which perform similar function as C#’s high precision decimal arithmetic type. But it is a reference type instead of a built-in type, which means it must be alter using type methods instead of usual arithmetic operators. (Dare,2007)

Value Type and Object

By using the keyword struct, C# enable developer to create a user-defined value types. It is like primitive type, they are passed by value instead of reference. By default, value type comes with a constructor. This gives developer to use the value type in arrays without initialize it. Java however, asides from built-in primitive types, doesn’t include the concept of value types.

Object, on the other hand, exist in Java but not C#. Objects can be viewed as a variable defined by user, contains data and methods. It is actually instantiations of classes. Access to object is similar to access to array or interface; it is accessed by reference instead of direct access. (Shyamal and Kailash, 2005)

2.2 Operator Overloading

Another difference of Java and C# is operator overloading. In Java, user defined operator cannot be overloaded, whereas C# allows user defined operator overloading. User defined operator can be implemented by declaring it in classes or structs. Only operators as shown below can be overloaded:

Overloadable unary operators

Overloadable binary operators

+ – ! ~ ++ true false

+ – * / % & ! ^ << >> == != < > <= >=

However, other operators such as member access, method invocation, “=”, “&&”, “||”, “?:”, “checked”, “unchecked”, “new”, “typeof”, “as” and “is” are not overloadable. Operator overloading makes class implementation of C# able to handle objects more natural. This feature gives C# an advantage over Java. (Shyamal and Kailash, 2005)

2.3 Arrays

Arrays in Java and C# have slight difference. This doesn’t affect how we write the code or develop our application, but it is worth knowing. In Java, multi dimensional arrays are actually jagged arrays, which mean it is arrays within arrays. In C#, multi dimensional arrays are true rectangular arrays. This affects the way the data are accessed. C#’s arrays, in some cases can have better performance compare to Java’s arrays. This is due to there is only a single pointer dereference for C#’s arrays, jagged arrays have one for every dimension. (Shyamal and Kailash, 2005)

Read also  Total Quality Management Information Technology Essay

2.4 Strings

Strings in Java and C# are quite identical, only the method that handle strings are a bit different. In Java, we have to use charAt() method to access certain character. For example, if we wish to access object z’s character which is located at position y, z.charAt(y) is used. If we want to know the length of the string, length() method is used. But in C#, we simply need to use z[y] to access the character at position y of z string. A.Length is used if we want to know the length. C# way of handling strings is easier to use and understand. This increases readability of code. (Shyamal and Kailash, 2005)

Java

C#

public class Strings

{

public static void main(String[]

args)

{

int i, j, k;

String a;

final int N = 10;

for (j=1; j<=N; j++)

{

a = “”;

for (i=1; i<=N-j; i++)

a = a + ” “;

for (i=1; i<=2*j-1; i++)

a = a + “*”;

for (k=0; k<a.length(); k++)

System.out.print(a.charAt(k));

System.out.println();

}

}

}

class Strings

{

static void Main(string[] args)

{

int i, j, k;

string a;

const int N = 10;

for (j=1; j<=N; j++)

{

a = “”;

for (i=1; i<=N-j; i++)

a = a + ” “;

for (i=1; i<=2*j-1; i++)

a = a + “*”;

for (k=0; k<a.Length; k++)

System.Console.Write(a[k]);

System.Console.WriteLine();

}

}

}

Source: A Comparison of Java and C# (Shyamal and Kailash, 2005)

2.5 Properties

Properties are feature exist only in C#. Properties allow developer to access member of a class. We can access the member as if it was a field, but it is actually a method call. However, access a member by using properties may have side effects when calculations or setting value. This may happen when it is transparent to the developer. In order to counter this, C# provide a way for developer to change the implementation and how it is actually used. Developer is able to create read-only, write-only or read-write properties. We can even change the visibility of it, such as public accessors and private modifiers. In Java, it would be the accessors and modifiers which is similar to the properties of C#. (Dare,2007)

using System;

public class User {

public User(string name){

this.name = name;

}

private string name;

//property with public getter and private setter

public string Name{

get{

return name;

}

private set {

name = value;

}

}

private static int minimum_age = 13;

//read-write property for class member, minimum_age

public static int MinimumAge{

get{

return minimum_age;

}

set{

if(value > 0 && value < 100)

minimum_age = value;

else

Console.WriteLine(“{0} is an invalid age, so minimum age remains at {1}”, value, minimum_age);

}

}

public static void Main(string[] args){

User newuser = new User(“Bob Hope”);

User.MinimumAge = -5; /* prints error to screen since value invalid */

User.MinimumAge = 18;

//newuser.Name = “Kevin Nash”; Causes compiler error since Name property is read-only

Console.WriteLine(“Minimum Age: ” + User.MinimumAge);

Console.WriteLine(“Name: {0}”, newuser.Name);

}

} // User

Source: A Comparison Of Microsoft’s C# Programming Language To Sun Microsystems (Dare,2007)

2.6 Indexer

Indexer is used to overload the [ ] operator for class. It is useful when a class act as a container of a kind of object. Indexer can support any type, for example, string, or integer as index. This means indexer is flexible. On the other hand, we can also create indexer that uses multidimensional array syntax. This is how we can mix different type as index. And of course, indexer can be overloaded too. (Dare,2007)

2.7 Preprocessor Directives

C# has a preprocessor that contain some of the functionality of C or C++ preprocessor. The main function of the preprocessor is the ability to #define and #undef. The preprocessor also can select a certain part of code to compile first, based on certain keyword such as #if, #elif and #else. Besides that, there are also #error, #warning and #pragma that control warning message during compilation. Last but not least, the #line that is used to report line number and source file when compiler detects errors.

#define DEBUG /* #define must be first token in file */

using System;

#pragma warning disable 169 /* Disable ‘field never used’ warning */

class PreprocessorTest{

int unused_field;

public static void Main(string[] args){

#if DEBUG

Console.WriteLine(“DEBUG Mode := On”);

#else

Console.WriteLine(“DEBUG Mode := Off”);

#endif

}

}

Source: A Comparison Of Microsoft’s C# Programming Language To Sun Microsystems (Dare,2007)

2.8 Verbatim Strings

In C#, the use of escape sequences within string constants doesn’t exist. C# is able to declare strings as it is. Therefore, backslashes, quote, newlines and tabs can be declared in a string without using the escape sequences. However, double quote is the only character that requires some sort of escape sequence. Thus, verbatim string is used. Verbatim string can be declared by using the @ symbol in front of the string declaration.

using System;

class VerbatimTest{

public static void Main(){

//verbatim string

string filename = @”C:My DocumentsMy FilesFile.html”;

Console.WriteLine(“Filename 1: ” + filename);

//regular string

string filename2 = “C:\My Documents\My Files\File.html”;

Console.WriteLine(“Filename 2: ” + filename2);

string snl_celebrity_jeopardy_skit = @”

Darrell Hammond (Sean Connery) : I’ll take “”Swords”” for $400

Will Farrell (Alex Trebek) : That’s S-Words, Mr Connery.

“;

Console.WriteLine(snl_celebrity_jeopardy_skit);

}

}

Source: A Comparison Of Microsoft’s C# Programming Language To Sun Microsystems (Dare,2007)

2.9 Overflow Detection

C# offer the ability for developer to specify whether to detect or ignore overflow in type conversions and expressions. Overflow detection can affect performance, this is why C# offer developer to choose to enable overflow detection by using /checked of the compiler option. Developer can choose to always checked or always not checked a block by using the “checked” keyword and “unchecked” keyword.

Read also  The Scope And Complexity Of Enterprise Systems Information Technology Essay

using System;

class CheckedTest{

public static void Main(){

int num = 5000;

/* OVERFLOW I */

byte a = (byte) num; /* overflow detected only if /checked compiler option on */

/* OVERFLOW II */

checked{

byte b = (byte) num; /* overflow ALWAYS detected */

}

/* OVERFLOW III */

unchecked{

byte c = (byte) num; /* overflow NEVER detected */

}

}//Main

}

Source: A Comparison Of Microsoft’s C# Programming Language To Sun Microsystems (Dare,2007)

2.10 Explicit Interface Implementation

In Java, sometimes it is possible to have namespace colllisions when a class implements an interface. For example, a class can implement the IFileHandler interface and IWindow interface. Both of them may have a Close method. For IFileHandler, the Close method means to close the file and IWindow Close method means to close the GUI window. Java doesn’t have a solution for this. Developer have to write another Close method at IFileHandler handle and IWindow handle and invoke that Close method when want to close them. However, C# provide the ability to allow developer to bind the method implementations to specific interfaces. Therefore, from the previous example, if it is C#, the class will have different Close method that can be invoked based on the class was treated as IFileHandler or IWindow. This interface are private, if developer wish to access it, developer have to cast the object to the required type to invoke the method.

using System;

interface IVehicle{

//identify vehicle by model, make, year

void IdentifySelf();

}

interface IRobot{

//identify robot by name

void IdentifySelf();

}

class TransformingRobot : IRobot, IVehicle{

string model;

string make;

short year;

string name;

TransformingRobot(String name, String model, String make, short year){

this.name = name;

this.model = model;

this.make = make;

this.year = year;

}

void IRobot.IdentifySelf(){

Console.WriteLine(“My name is ” + this.name);

}

void IVehicle.IdentifySelf(){

Console.WriteLine(“Model:” + this.model + ” Make:” + this.make + ” Year:” + this.year);

}

public static void Main(){

TransformingRobot tr = new TransformingRobot(“SedanBot”, “Toyota”, “Corolla”, 2001);

// tr.IdentifySelf(); ERROR

IVehicle v = (IVehicle) tr;

IRobot r = (IRobot) tr;

v.IdentifySelf();

r.IdentifySelf();

}

}

Source: A Comparison Of Microsoft’s C# Programming Language To Sun Microsystems (Dare,2007)

2.11 Cross Platform Portability

One of the main attarction of Java is cross platform portability. Java is able to run on various kind of platform such as Windows, Linux, MacOS, and many more. This is done by compiling a Java program into byte code. Byte code is code that can be executed anywhere, any platform, any machine that support Java Virtual Machine. The Java Virtual Machine will translate the byte code into the operating system’s machine code. This is how Java can obtain such powerful cross platform portability. On the other hand, C# also has a few project to enable cross platform capability. Such as the Mono project to enable C# program to run on Linux. But the progress is rather slow and Java Virtual Machine has mature over the years. In term of cross platform portability C# has no way to compare with Java. (Dare,2007)

2.12 Dynamic Class Loading

Dynamic class loading is one of the Java’s powerful feature. It enable the ability for classes to be loaded at runtime dynamically. It downloads class files in the form of byte code that don’t exist in the running machine from another machine. The process is transparent to the user, user don’t have to do anything. Changes can be implemented on a machine and allow another applications which is on different machine to be extended at runtime. C# however, also support downloading classes remotely, but the client must publish the assembly for the server to load it through an URL. (Dare,2007)

public class MyRMIServer extends UnicastRemoteObject

implements SomeInterface {

public MyRMIServer() throws RemoteException{ super();}

public String obtainName(IStockTicker ticker){

String stock_ticker = ticker.getTicker();

if(stock_ticker.equalsIgnoreCase(“MSFT”))

return “Microsoft Corporation”;

else if(stock_ticker.equalsIgnoreCase(“SUNW”))

return “Sun Microsystems”;

else

return “Unknown Stock Ticker”;

}/* obtainName(IStockTicker) */

}

Source: A Comparison Of Microsoft’s C# Programming Language To Sun Microsystems (Dare,2007)

2.13 Static Imports

Static import is a feature of Java. It allows the access of static members of a class even without specifying the class name when importing it. The purpose of this is to reduce the length of code when a class needs to access the static members of another class. This can increase readability of code. The static import can be done by using the “import static” keyword. It is similar to “import”, but instead of importing a package, a class is imported instead. (Dare,2007)

import static java.awt.Color.*;

public class Test{

public static void main(String[] args) throws Exception{

//constants not qualified thanks to static import

System.out.println(RED + ” plus ” + YELLOW + ” is ” + ORANGE);

}

}

Source: A Comparison Of Microsoft’s C# Programming Language To Sun Microsystems (Dare,2007)

STRENGTH AND WEAKNESS

Java

Strength:

Cross platform portability

Free/Open source

Driven by community instead of company. Public can discuss, propose and vote for new feature in Java.

More mature

Lots of libraries available

Weakness:

Lacks unsigned integer type

Lacks properties

C#

Strength:

Closely tied to Windows. Able to use a lot of service provided by Windows, thus have better performance compare to Java in Windows.

Easier to code

.Net framework uses CLR that support a lot languages, which mean code written in other languages can work with C#

Weakness:

Not well supported on platform other than Windows

Stuck to a single supplier, the Microsoft

4 CRITICAL EVALUATION

4.1 What Should Be Considered When Choosing A Language

Between Java and C#, we can’t really decide which one is better. Both are superior to another in certain situation. So when we want to develop an application, it really boils down to what problem we are trying to solve, what difficulties are we facing and many other factors.

Read also  Using Computer Forensics to Investigate Employee Data Theft

Time Constraints

Time constraints can affect our choice of language. We need to know when is the deadline, how much time do we have, how fast do we have to develop the application. Regarding this, we need to know how much time we need to write the code, and also how much time we need to solve the problems and bugs. Besides that, the development tools available for these languages will also affect our choice. People who are more comfortable or familiar with Visual Studio than NetBean may choose C# as their language. We always work slower with things that we are less familiar with.

Environments

Environments can play an important role when choosing language of development. If we know our application is targeted to Windows user, C# might be a better choice over Java, because C# uses services provided by Windows and can perform better than Java in Windows environment. But if the application is targeted for several operating systems, Java might be a better choice, since it is better when it comes to cross-platform portability. Not only OS, when we talk about environment, we also talk about integration with another application. For example, if our application has to work with another application which is written in C#, writing our application in C# might be a better choice, since calling a C# method is always easier when it is called by another C# application.

Condition of the Language

Condition of the language includes community/support that we can find when we face problem, continuity of the language and how mature the language is. Choosing a language that you can’t find any support will be a bad idea, we will only cause more problems and slow down the process of development. A mature language can ensure we face less language problem, since most of them are solved. Continuity of the language means how long is the language going to exist, will it continue to grow or just died out like many other languages did. For example, C# is tied to Microsoft only, if Microsoft decide to stop C#’s development, we might have to change our application to Java in the future. Therefore, knowing the continuity of the language is important too.

Programmer’s Skills

Before choosing language, we also need to know the skills of the programmer. A programmer who is more proficient in C# can produce good, less error code. In turn, we can have less time debugging and speed up the development process.

Purpose of Application

The last and the most important factor, the purpose of the application. This plays a critical part, because some programming language might be not suitable for some purpose. For example, Java has high portability, but it is unsuitable for certain things like game programming, writing hardware driver or artificial intelligence. There are other programming languages that are more suitable for those purposes.

4.2 Opinion and Linkage to Final Year Project

In my opinion, both languages are powerful, both of them are almost the same thing, yet still different, none is superior than the other, only which situation is more suitable than the other.

Java is famous for cross platform. We only need to write the code once, and able apply it to many kind of platform with minor or no changes, given that it support Java Virtual Machine. Java also has a large supporting community. Whenever we face problem, we can go forum and ask people or guru for suggestion, finding support such as this has absolutely no problem. Another selling point of Java is because it is free. We can get anything about Java free from the Internet, even the development tool of Java, NetBean.

As for C#, the thing that I like the most is because of Visual Studio. I am able to work with other programmer using other language such as VB to develop an application. I also like to use Visual Studio compare to NetBean. The properties function made my work a lot easier.

For my final year project, I’m developing a Hospital System. My target platform to run the application is Windows, so I’m choosing C# to develop the application. Between C# and Java, I’m more comfortable with using Visual Studio than NetBean and writing C# code. On the other hand, C# in Windows can never goes wrong. C# can outperform Java in Windows. Since my target is only on Windows, Java’s cross platform portability is useless for me. Besides, my partner can use other languages which is supported by Visual Studio too, this cross language compatibility give us more options to develop our application.

5 CONCLUSION

At the end of the day, it is not really about which language is more powerful than the other. C# programmer may wish things in Java is actually in C#, and Java programmer may wondering why some features of C# is missing in Java. It is about which language is more suitable to use in which situation. C# and Java both have their own advantages and disadvantages. Language choice is all depends on the purpose of your application. But one thing is for sure, Java and C# will continue to grow and remain on the IT world for a long time.

Order Now

Order Now

Type of Paper
Subject
Deadline
Number of Pages
(275 words)