Java versus C# Java versus C#

advertisement
Java versus C#
An introduction to C# and Visual
Studio
Main Method
• Java: It is possible to compile a Java program
without a main method, provided that class is
used as a super class. However the program will
execute only when there is a valid main method
in the class.
• C#: It is not possible to compile a C# source file
without a valid main method. If it is absent, then
the compiler will show an error message like
program ‘filename.exe’ does not have an entry
point defined.
Primitives
• Both languages support a number of built-in types which
are copied and passed by value rather than by reference.
Java calls these types primitive types, while they are called
simple types in C#. The simple/primitive types typically
have native support from the underlying processor
architecture.
• C# has a few more primitive types than Java, because it
supports unsigned as well as signed integer types, and a
decimal type for decimal floating-point calculations.
• Java lacks the unsigned types. In particular, Java lacks a
primitive type for an unsigned byte. The Java byte type is
signed, while the C# byte is unsigned and sbyte is signed.
Simple/Primitive Types
C# alias
.NET CLR type
Width (bits)
sbyte
byte
short
ushort
int
SByte
Byte
Int16
UInt16
Int32
8
8
16
16
32
uint
UInt32
32
long
Int64
64
ulong
UInt64
64
float
Single
32
double
Double
64
decimal
Decimal
128
char
bool
Char
Boolean
16
8
Range
(approximate)
-128 to 127
0 to 255
-32768 to 32767
0 to 65535
-2,147,483,648 to
2,147,483,647
0 to 4,294,967,295
9,223,372,036,854,7
75,808 to
9,223,372,036,854,7
75,807
0 to
18,446,744,073,709,
551,615
-3.402823e38 to
3.402823e38
1.79769313486232e
308 to
1.79769313486232e
308
±1.0 × 10e−28 to
±7.9 × 10e28
\u0000 to \uFFFF
false, true
Java type
Java wrapper
byte
Not available
short
Not available
int
Byte
Not available
Short
Not available
Integer
Not available
Not available
long
Long
Not available
Not available
float
Float
double
Double
Not available
Not available
char
boolean
Character
Boolean
Constants
• Java, defined global constants using public
static final.
• In C#, the static keyword does exist (final does
not), but you can define constants like this:
public const MyConstant = 101;
• When the constant needs to be initialized at
run-time (for example in a constructor), use
the readonly keyword instead of const.
Operators and control statements
• All operators like Arithmetic, logical,
increment and decrement etc available in Java
are supported by C#. Moreover control flow
statements like if, else, if-else, for, do-while,
while etc included with Java are also available
in C#. However C# reintroduces the popular
GoTo statement found in C++.
Objects
• Java made everything an object. How Java forced you
to make classes for everything.
• This turns out to be partially true. It is necessary to
make classes.
• However, primitives are not objects.
• In C#, even what were considered primitives in Java are
objects in C#.
• Absolutely everything descends from System.Object.
This choice by the C# team allows for greater
consistency and for such things as a cleaner
implementation of automatic boxing/unboxing
Inheritance
• In Java, the keyword extends is used to inherit
from a parent class, and the keyword implements
is used to inherit an interface. In C#, the
distinction does not exist and it’s all done in the
method signature.
public class MyClass : MyParentClass
{
...
}
Overriding
In Java, all methods are by default virtual and you can override them.
In C#, all methods are non-virtual. To override a method in the
parent class, make sure the method of the parent class is defined as
virtual using the virtual keyword.
class MyParentClass
{
public virtual void MyMethod() { ... }
}
In the child class, the method must use the override keyword.
class MyChildClass : MyParentClass
{
public override void MyMethod() { ... }
}
Hello World
HelloWorld Java Program
class Hello {public static void main(String args[])
{System.out.println("HelloWorld");}}
HelloWorld C# Program
class Hello {public static void Main()
{Console.WriteLine("Hello world");}}
Additional Differences
– cs files
• In C#, the name of a .cs file does not have to match the
name of the class defined in it (though this is still a
good idea).
• A .cs file can contain multiple classes.
• A class can be split across multiple .cs files if declared
partial.
– Conventions
• C# generally uses the same naming conventions as Java,
except that in C#, method names are generally
capitalized (PascalCase).
• Typical C# code will put the opening curly brace of a
block on a line by itself.
Additional Differences
– Main method
• The args parameter to Main is optional in C#
• The Main method can return either void or int in C#
– String comparisons
• C# lets you compare strings with == and != (casesensitive).
– Inheritance
• C# uses “base” where Java uses “super”
For Each and 2 dim Array
for-each:
• for-each loop in C# is slightly different; given int[] array = { 1, 2, 3 };
– Java: for (int i : array)…
– C#: foreach (int i in array)…
Two-dimensional arrays
• C# distinguishes between square and jagged arrays:
int[,] square = new int[3, 3];
square[0, 0] = 1;
int[][] jagged = new int[3][];
jagged[0] = new int[3];
jagged[1] = new int[3];
jagged[2] = new int[3];
jagged[0][0] = 1;
C# includes language support for getters and setters.
– Java:
• Class contains
private int number;
int getNumber() { return this.number; }
int setNumber(int number) { this.number = value; }
• Client code looks like method calls
obj.setNumber(5);
System.out.println(obj.getNumber);
– C#
• Class contains
private int number;
public int Number {
get { return this.number; }
set { this.number = value; }
// name for the value being set
}
• Client code looks like using a field
obj.Number = 5;
Console.WriteLine(obj.Number);
// value is a magic
Parameters
• C# has mechanisms to allow methods to modify their
primitive/value parameters
– Specifying a parameter as “ref” says that the method gets the current
value of the parameter, and can modify the caller’s copy (“reference
semantics”)
– Specifying a parameter as “out” allows the method to set the value of
the parameter (it cannot look at the initial value of the parameter, and
it must set the value before returning)
– Example:
int a = 5;
Modify(ref a);
// caller needs to specify ref too
Console.WriteLine(a);
// prints 6
Change(out a);
// caller needs to specify out too
Console.WriteLine(a);
// prints 10
static void Modify(ref int a) { a = a + 1; }
static void Change(out int a) { a = 10; }
Download