Difference between Java and c#


Published on

Some vital points which are to be remembered while learning JAVA and C# ..
Must see this slide

Published in: Software, Technology, Education
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Difference between Java and c#

  1. 1. Java vs. C#
  2. 2. What is C# ?  What do you guys think?  C# a new programming language or a new version of C/C++ ?  It is a strong language for network and internet programming. C# has redefined the programming landscape. In addition, C# designed with the need of C/C++ and Java programmers. This new language has been developed specifically with the .NET framework in mind, and as such is designated to be the .NET developer's language of choice. One very important matter about C#, it is the first component oriented programming language.
  3. 3. Differences with JAVA! 1. Subtle Differences in terms of syntax of Java and C# 2. Slight modification of concepts in C# that already exist in Java 3. Language features and concepts that do not exist in Java at all.
  4. 4. 1. Differences in terms of Syntax: Java main  C# Main Java: public static void main(String[] args) C#: static void Main(string[] args)  string is shorthand for the System.String class in C#. Another interesting point is that in C#, your Main method can actually be declared to be parameter-less static void Main()
  5. 5. 1. Differences in terms of Syntax: Print statements Java: System.out.println("Hello world!"); C#: System.Console.WriteLine("Hello world!"); or Console.WriteLine("Hello again!");
  6. 6. 1. Differences in terms of Syntax: Declaring Constants Java:  In Java, compile-time constant values are declared inside a class as static final int K = 100; C#:  To declare constants in C# the const keyword is used for compile time constants while the readonly keyword is used for runtime constants. The semantics of constant primitives and object references in C# is the same as in Java. const int K = 100;
  7. 7. 1. Differences in terms of Syntax: Inheritance  C# uses C++ syntax for inheritance, both for class inheritance and interface implementation as opposed to the extends and implements keywords. Java: class B extends A implements Comparable { …………… …………… } C#: class B:A, IComparable { ………… ………… }
  8. 8. 1. Differences in terms of Syntax: Primitive Types  In Java, all integer primitive types (byte, short, int, long) are signed by default.  In C# there are both signed and unsigned varieties of these types: Unsigned Signed Size byte sbyte 8 bits ushort short 16 bits uint int 32 bits ulong long 64 bits  The only significantly different primitive in C# is the decimal type, a type which stores decimal numbers without rounding errors. Eg: decimal dec = 100.44m;
  9. 9. 1. Differences in terms of Syntax: Array Declaration  Java has two ways in which one can declare an array: int[] iArray = new int[100]; //valid float fArray[] = new float[100]; //valid  C# uses only the latter array declaration syntax: int[] iArray = new int[100]; //valid float fArray[] = new float[100]; //ERROR: Won't compile
  10. 10. 2. Modified concepts from Java: Polymorphism & Overriding  The means of implementing polymorphism typically involves having methods in a base class that may be overridden by derived classes. These methods can be invoked even though the client has a reference to a base class type which points to an object of the derived class. Such methods are bound at runtime instead of being bound during compilation and are typically called virtual methods.  In Java all methods are virtual methods while in C#, as in C++, one must explicitly state which methods one wants to be virtual since by default they are not.  To mark a method as virtual in C#, one uses the virtual keyword. Also, implementers of a child class can decide to either explicitly override the virtual method by using the override keyword or explicitly choose not to by using the new keyword instead
  11. 11. 2. Modified concepts from Java: Polymorphism & Overriding Example: using System; public class Parent { public virtual void DoStuff(string str) { Console.WriteLine("In Parent.DoStuff: " + str); } } public class Child: Parent { public void DoStuff(int n) { Console.WriteLine("In Child.DoStuff: " + n); } public override void DoStuff(string str) { Console.WriteLine("In Child.DoStuff: " + str); } } public new void DoStuff(string str)
  12. 12. 2. Modified concepts from Java: Operator Overloading  Operator overloading allows standard operators in a language to be given new semantics when applied in the context of a particular class or type.  Operator overloading can be used to simplify the syntax of certain operations especially when they are performed very often, such as string concatenation in Java or interactions with iterators and collections in the C++ Standard Template Library.  Unlike C++, C# does not allow the overloading of the following operators; new,( ), ||, &&, =, or any variations of compound assignments such as +=, -=, etc.
  13. 13. 2. Modified concepts from Java: Switch Statements  There are two major differences between the switch statement in C# versus that in Java.  In C#, switch statements support the use of string literals and do not allow fall-through unless the label contains no statements.  switch(foo){ case "A": Console.WriteLine("A seen"); break; case "B": case "C": Console.WriteLine("B or C seen"); break; /* ERROR: Won't compile due to fall-through at case "D" */ case "D": Console.WriteLine("D seen"); case "E": Console.WriteLine("E seen"); break; }
  14. 14. 2. Modified concepts from Java: Multiple Classes in a Single File  Multiple classes can be defined in a single file in both languages with some significant differences.  In Java, there can only be one class per source file that has public access and it must have the same name as the source file.  C# does not have a restriction on the number of public classes that can exist in a source file and neither is there a requirement for the name of any of the classes in the file to match that of the source file.
  15. 15. 2. Modified concepts from Java: Importing Libraries  Both the langugaes support this functionality and C# follows Java’s technique for importing libraries:  C#: using keyword using System; using System.IO; using System.Reflection;  Java: import keyword import java.util.*; import java.io.*;
  16. 16. 3. New Concepts in C#: Enumerations  Java's lack of enumerated types leads to the use of integers in situations that do not guarantee type safety.  C# code: public enum Direction {North=1, East=2, West=4, South=8}; Usage: Direction wall = Direction.North;  Java equivalent code will be: public class Direction { public final static int NORTH = 1; public final static int EAST = 2; public final static int WEST = 3; public final static int SOUTH = 4; } Usage: int wall = Direction.NORTH;
  17. 17. 3. New Concepts in C#: Enumerations  Despite the fact the Java version seems to express more, it doesn't, and is less type-safe, by allowing you to accidentally assign wall to any int value without the compiler complaining.  C# enumerations support the ToString method, so they can report their value as string (such as “North") and not just an an integer.  There also exists a static Parse method within the Enum class for converting a string to an enumeration.
  18. 18. 3. New Concepts in C#: foreach Statement  The foreach loop is an iteration construct that is popular in a number of scripting languages (e.g. Perl, PHP, Tcl/Tk)  The foreach loop is a less verbose way to iterate through arrays or classes that implement the the System.Collections.IEnumerable interface.  Example: string[] greek_alphabet = {"alpha", "beta", "gamma", "delta"}; foreach(string str in greek_alphabet) { Console.WriteLine(str + " is a greek letter"); }
  19. 19. 3. New Concepts in C#: Properties  Properties are a way to abstract away from directly accessing the members of a class, similar to how accessors (getters) and modifiers (setters) are used in Java.  Particularly for read/write properties, C# provides a cleaner way of handling this concept. The relationship between a get and set method is inherent in C#, while has to be maintained in Java.  It is possible to create, read-only, write-only or read-write properties depending on if the getter and setter are implemented or not.
  20. 20. 3. New Concepts in C#: Properties  Java: public int getSize() { return size; } public void setSize (int val) { size = val; }  C#: public int Size { get {return size; } set {size = val; } }
  21. 21. 3. New Concepts in C#: Pointers  Although core C# is like Java in that there is no access to a pointer type that is analogous to pointer types in C and C++, it is possible to have pointer types if the C# code is executing in an unsafe context.  Pointer arithmetic can be performed in C# within methods marked with the unsafe keyword.  Example: public static unsafe void Swap(int* a, int*b) { int temp = *a; *a = *b; *b = temp; }
  22. 22. 3. New Concepts in C#: Pass by Refernce  In Java the arguments to a method are passed by value meaning that a method operates on copies of the items passed to it instead of on the actual items.  In C#, it is possible to specify that the arguments to a method actually be references.  In Java trying to return multiple values from a method is not supported.  The C# keywords used are ref and out. ChangeMe(out s); Swap(ref a, ref b);
  23. 23. REFERENCES:  OOP with Microsoft VB.NET and Microsoft Visual C#.NET by Robin A. Reyonlds-Haerle  JAVA 2 Essentials by Cay Horstmann Websites:  Java vs. C#: Code to Code Comparison http://www.javacamp.org/javavscsharp/  A Comparative Overview of C#: http://genamics.com/developer/csharp_comparative.htm  C#: A language alternative or just J--?, http://www.javaworld.com/javaworld/jw-11-2000/jw-1122-csharp1.html  A COMPARISON OF C# TO JAVA By Dare Obasanjo http://www.soften.ktu.lt/~mockus/gmcsharp/csharp/c-sharp-vs-java.html#foreach  Conversational C# for Java Programmers by Raffi Krikorian http://www.ondotnet.com/pub/a/dotnet/2001/05/31/csharp_4_java.html
  24. 24. Thank You…