Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java versus C# An introduction to C# and Visual Studio.

Similar presentations


Presentation on theme: "Java versus C# An introduction to C# and Visual Studio."— Presentation transcript:

1 Java versus C# An introduction to C# and Visual Studio

2 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.

3 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.

4 Simple/Primitive Types C# alias.NET CLR typeWidth (bits) Range (approximate) Java typeJava wrapper sbyteSByte8-128 to 127byteByte byteByte80 to 255Not available shortInt1616-32768 to 32767shortShort ushortUInt16160 to 65535Not available intInt3232 -2,147,483,648 to 2,147,483,647 intInteger uintUInt32320 to 4,294,967,295Not available longInt6464 - 9,223,372,036,854,7 75,808 to 9,223,372,036,854,7 75,807 longLong ulongUInt6464 0 to 18,446,744,073,709, 551,615 Not available floatSingle32 -3.402823e38 to 3.402823e38 floatFloat doubleDouble64 - 1.79769313486232e 308 to 1.79769313486232e 308 doubleDouble decimalDecimal128 ±1.0 × 10e−28 to ±7.9 × 10e28 Not available charChar16\u0000 to \uFFFFcharCharacter boolBoolean8false, truebooleanBoolean

5 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.

6 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++.

7 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

8 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 {... }

9 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() {... } }

10 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");}}

11 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.

12 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 != (case- sensitive). – Inheritance C# uses “base” where Java uses “super”

13 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;

14 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; } // value is a magic // name for the value being set } Client code looks like using a field obj.Number = 5; Console.WriteLine(obj.Number);

15 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 ppt "Java versus C# An introduction to C# and Visual Studio."

Similar presentations


Ads by Google