Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java and C# - Some Commonalities Compile into machine-independent, language- independent code which runs in a managed execution environment Garbage Collection.

Similar presentations


Presentation on theme: "Java and C# - Some Commonalities Compile into machine-independent, language- independent code which runs in a managed execution environment Garbage Collection."— Presentation transcript:

1 Java and C# - Some Commonalities Compile into machine-independent, language- independent code which runs in a managed execution environment Garbage Collection coupled with the elimination of pointers Powerful reflection capabilities No header files, all code is written inline Classes all descended from ‘object’ and must be allocated on the heap with ‘new’ keyword Thread support (similar to Java)

2 Java and C# - Some Commonalities Single inheritance Interfaces No global functions or constants, everything belongs to a class Arrays and strings with built-in bounds checking The "." operator is always used (no more ->, :: operators) ‘null’ and ‘boolean’/’bool’ are keywords All values must be initialized before use Only Boolean expressions are allowed in ‘if’ statements Try Block can have a finally clause

3 Both C# and Java compile initially to an intermediate language: C# to Microsoft Intermediate Language (MSIL), and Java to Java bytecode In each case the intermediate language can be run by interpretation, or just-in-time compilation, on an appropriate 'virtual machine' In C#, however, more support is given for the further compilation of the intermediate language code into native code

4 Some C# features More primitive data types than Java More extension to value types Supports safer enumeration types Support for ‘struct’ – light weight objects Operator overloading 'delegates' - type-safe method pointers used to implement event-handling Three types of arrays: –Single dimensional –Multi-dimensional rectangular –Multi-dimensional jagged

5 Restricted use of pointers The 'switch' statements has been changed to disallow 'fall-through' behavior Support for class 'properties'

6 C# Hello World using System;// System namespace public class HelloWorld { public static void Main()// the Main function starts // with capital M { Console.WriteLine("Hello World!”); }

7 C# Hello World Case sensitive Everything has to be inside a class Name of the class and the name of the file in which it is saved don’t need to match You are free to choose any extension for the file, but it is usual to use the extension '.cs' Supports both single line and multiple line comments

8 Variable Types (1): Reference Types and Value Types C# is a type-safe language Variables can hold either value types or reference types, or they can be pointers Where a variable ‘v’ contains a value type, it directly contains an object with some value Where a variable ‘v’ contains a reference type, what it directly contains is something which refers to an object

9 Built-in Types C# Type Signed?Bytes Occupied Possible Values sbyteYes1-128 to 127 shortYes2-32768 to 32767 intYes4-2147483648 to 2147483647 longYes8-9223372036854775808 to 9223372036854775807 byteNo10 to 255 ushortNo20 to 65535 uintNo40 to 4294967295 ulongNo80 to 18446744073709551615

10 Built-in Types C# Type Signed?Bytes Occupied Possible Values floatYes4Approximately ±1.5 x 10 - 45 to ±3.4 x 10 38 with 7 significant figures doubleYes8Approximately ±5.0 x 10 - 324 to ±1.7 x 10 308 with 15 or 16 significant figures decimalYes12Approximately ±1.0 x 10 - 28 to ±7.9 x 10 28 with 28 or 29 significant figures charN/A2Any Unicode character (16 bit) boolN/A1 / 2true or false

11 Structs Structs are significantly different than C++ In C++ a struct is exactly like a class, except that the default inheritance and default access are public rather than private In C# structs are very different from classes Structs in C# are designed to encapsulate lightweight objects They are value types (not reference types), so they're passed by value They are sealed, which means they cannot be derived from or have any base class other than ‘System.Value’Type, which is derived from Object Structs cannot declare a default (parameterless) constructor Structs are more efficient than classes

12 Properties Formalize the concept of getter/setter methods In C# the relationship between a get and set method is inherent, while in Java or C++ it has to be maintained

13 Properties Java/C++ Example public int getSize() { return size; } public void setSize (int value) { size = value; } foo.setSize (getSize () + 1);

14 Properties C# Example public int Size { get {return size; } set {size = value; } } foo.size = foo.size + 1;

15 Reference Types The two pre-defined reference types are object and string ‘object’ is the ultimate base class of all other types New reference types can be defined using 'class', 'interface', and 'delegate' declarations Reference types actually hold the value of a memory address occupied by the object they reference Aliasing object x = new object(); x.myValue = 10; object y = x; y.myValue = 20; after this statement both ‘x.myValue’ and ‘y.myValue’ equal 20

16 Reference Types No aliasing in strings – these are immutable –The properties of these objects can't change themselves –So in order to change what a string variable references, a new string object must be created string s1 = "hello"; string s2 = s1; s1 = “world”; //s1 points to a new string // s2 keeps pointing to the old string


Download ppt "Java and C# - Some Commonalities Compile into machine-independent, language- independent code which runs in a managed execution environment Garbage Collection."

Similar presentations


Ads by Google