AddThis Social Bookmark Button

Print

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

Comparing the Language Specification

Primitives

Primitives in C# are called value types and there are more predefined value types than in Java. For example, C# has uint, or unsigned integer. Table 2 lists all the predefined types in C#.



Table 2: Value types in C#

Type

Description

object

The ultimate base type of all other types

string

String type; a string is a sequence of Unicode characters

sbyte

8-bit signed integral type

short

16-bit signed integral type

int

32-bit signed integral type

long

64-bit signed integral type

byte

8-bit unsigned integral type

ushort

16-bit unsigned integral type

uint

32-bit unsigned integral type

ulong

64-bit unsigned integral type

float

Single-precision floating point type

double

Double-precision floating point type

bool

Boolean type; a bool value is either true or false

char

Character type; a char value is a Unicode character

decimal

Precise decimal type with 28 significant digits

Constants

Forget the static final modifiers in Java. In C# constants can be declared using the const keyword.

public const int x = 55;

In addition, the designers of C# added the readonly keyword, which you can use if the constant value cannot be determined at compile time. These read-only fields can only be set through an initializer or a class constructor.

The entry point of a public class

In Java, the entry point of a public class is the public static method named main, which accepts an array of String objects as arguments and returns no value. In C#, the main method is the public static method called Main (with capital M), which also accepts an array of String objects and returns no value, as given in the following signature.

public static void Main(String[] args)

However, there is more. If you are not passing anything to the Main method, you can use an overload of Main, the one without an argument list. Therefore, the following Main method is also a valid entry point.

public static void Main()

Furthermore, the Main method can also return an int, if you want. For example, the Main method of the following code returns 1.

using System;
public class Hello {
public static int Main() {
Console.WriteLine("Done");
return 1;
}
}

As a comparison, overloading the main method is illegal in Java.

The switch statement

Unlike in Java, where the switch statement can only be used on integers, in C# switch can also work with string variables. Consider the following C# code that uses the switch statement with a string variable.

using System;
public class Hello {
public static void Main(String[] args) {
switch (args[0]) {
case "boss":
Console.WriteLine("Good morning, Sir. We are ready to serve you.");
break;
case "employee":
Console.WriteLine("Good morning. You can start working now.");
break;
default:
Console.WriteLine("Good morning. How are you today?");
break;
}
}
}

Unlike in Java, the switch statement in C# does not allow fall-through when reading code by requiring that each case block have either a break at the end of the block or a goto another case label in the switch.

The foreach statement

A foreach statement enumerates the elements of a collection, executing a statement for each element of the collection. Consider the following code.

using System;
public class Hello {
public static void Main(String[] args) {
foreach (String arg in args)
Console.WriteLine(arg);
}
}

If you pass arguments when calling the executable, such as the following

Hello Peter Kevin Richard

The output will be the following lines of text.

Peter
Kevin
Richard

Pages: 1, 2, 3, 4

Next Pagearrow