WindowsDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


AddThis Social Bookmark Button

Comparing C# and Java
Pages: 1, 2, 3, 4

There is no >>> Shift Operator in C#

In C#, there exist unsigned variable types such as uint and ulong. Therefore, in C# the right shift operator (>>) works differently on unsigned variable types and signed variables (such as int and long). Right shifting uint or ulong discards the low-order bits and sets high-order empty bit positions to zero. With int and long variables, however, the >> operator discards the low-order bits and set the high-order empty bit positions to zero only if the variable value is positive. For operations on a negative number, the high-order empty bit positions are set to 1.



In Java, there is no unsigned variable. Therefore, you use the >>> operator to include negative bits in right shifting, and use the >> operator otherwise.

goto

In Java, goto is a keyword that is not used. In C# goto brings you to the specified label. However, C# treats goto with extra care. For instance, a goto cannot be used to jump into a statement block. In Java, you use labeled statements with break or continue to replace goto in C#.

Declaring an Array

Declaring an array in Java is very flexible. Indeed, there are a number of forms that are all legal. For example, the following lines of code are equivalent.

int[] x = { 0, 1, 2, 3 };
int x[] = { 0, 1, 2, 3 };

However, in C# only the first line is valid. The [] cannot be placed after the variable name.

Packaging

A package in C# is called a namespace. To import a namespace in C# you use the word "using." The following code imports the System namespace for use.

using System;

However, unlike in Java, C# allows the use of an alias for a namespace or a class in a namespace.

using TheConsole = System.Console;
public class Hello {
public static void Main() {
TheConsole.WriteLine("Using an alias");
}
}

Conceptually, Java packages are the same as .NET namespaces. However, implementations are different. In Java, package names are also a physical thing that determines the directory structure where your .java files must reside. In C#, there is a complete separation between physical packaging and logical naming, so the name for your namespace has nothing to do with the physical packaging. In C#, each source file can contribute to multiple namespaces and can take multiple public classes.

The physical packaging in .NET is called assembly. Each assembly contains a manifest that enumerates the files that are contained in the assembly, controls what types and resources are exposed outside the assembly, and maps references from those types and resources to the files that contain the types and resources. Assemblies are self-contained and an assembly can be contained in a single file or be split among a number of files. This packaging mechanism solves the problem with DLL files, notoriously known as DLL Hell.

The Default Package

In Java, the java.lang package is the default package that is automatically included without having to be imported. To output a piece of text to the console, you can write the following code.

System.out.println("Hello world from Java");

In C#, there is no default package. To output text to the console, you use the WriteLine method of the Console object in the System namespace. However, you must always import the package explicitly. Thus, the following code.

using System;
public class Hello {
public static void Main() {
Console.WriteLine("Hello world from C#");
}
}

Object-Orientation

Java and C# are both fully object-oriented languages. In terms of the three principles of object-oriented programming, they could not be more similar.

  • Inheritance: both support single inheritance of classes, but a class can implement multiple interfaces. All classes are inherited from a common base class.
  • Encapsulation and visibility: in both Java and C# you can decide how visible your class members are. Except for the internal access modifier in C#, accessibility is very much alike.
  • Polymorphism: both Java and C# support some forms of polymorphism in very similar ways.

Accessibility

Each member of a class has a form of accessibility. The access modifiers in C# are comparable to those in Java, with the addition of internal. In short, there are five forms of accessibility in C#, as given below.

  • public members are available to all code.
  • protected members are accessible only from derived classes.
  • internal members are accessible only from within the same assembly.
  • protected internal members are accessible only from derived classes within the same assembly.
  • private members are accessible only from the class itself.

Extending a Class

You use the keyword "extends" when performing inheritance in Java. C# adopts the C++ style when extending a class. For instance, the following code is how you create a new class named Button by extending a parent class called Control.

public class Button: Control {
.
.
.
}

Final Classes

Since there is no final keyword in C#, if you do not want your class to be extended, you can use the sealed keyword, like in the following example.

sealed class FinalClass {
.
.
.
}

Interfaces

The notion of interface in C# is very much like that in Java. There is the keyword interface and an interface can extend one or more other interfaces. By convention, an interface name starts with the capital I. The following code is an example of an interface in C#, which is indistinguishable from an interface in Java.

interface IShape {
void Draw();
}

The syntax for extending an interface is the same as extending a class. For example, the IRectangularShape interface below extends the IShape interface.

interface IRectangularShape: IShape {
int GetWidth();
}

If you are extending from two or more interfaces, the list of parent interfaces are separated by commas, like in the following code.

interface INewInterface: IParent1, IParent2 {
}

Unlike Java, however, an interface in C# must not contain fields.

Note also that in C#, all methods in an interface are public by default. Unlike Java, where the modifier public could be present in a method signature (even though this is not necessary), explicitly specifying an interface method as public is illegal in C#. For example, the following interface will generate a compile error in C#.

interface IShape {
public void Draw();
}

Pages: 1, 2, 3, 4

Next Pagearrow