I have recently been taking the time to learn more programming languages. Recently I have been experimenting with C# and I have seen the many similarities between this programming language and Java. I began to think, if I were to write a simple guide that explains how Java programmers can convert their Java classes into C#, what are the things that I would include?
Here are five things I would include in such a guide:
- There is a difference in importing things when comparing Java to C#. In Java, you can import needed packages, classes, and more by simply using the keyword “import”. For example, to import needed Java “util” files, you would type “import java.util.*;”. However, to import needed files in C#, you would need to use a different keyword, “using”. For example, to import “System” in C#, you would need to type “using System;”. This is important to know because if you are a Java programmer who does not know much about C#, you likely will not know the difference in these importing keywords, which could cause troubles if you need to import or “use” things in C#.
- There is also a difference in declaring variables when comparing Java to C#. It is a subtle difference, but it is one that could cause errors if it is not noticed. The difference comes in the form of the case of the first character of the object you are creating. In Java, this first character must be upper-case. However, in C#, this character should be lowercase. For example, when declaring a string in Java you would do “String exampleString;”. Notice that the first letter in “String” is uppercase. However, when you declare a string in C#, you should type “string exampleString;”. Notice that the first letter in “string” is lowercase. This is a small difference, but it is important to successfully converting a Java class or program to one in C#.
- There is a difference when printing or outputting information in Java when compared to C#. In Java, to print a new line, you would use the code “System.out.println();”. So for example, if you wanted to print out a line that says “Hello!” you would type, “System.out.println(“Hello!”);”. This print line command is different than that of C#. In C#, to print a new line, you would use the code “Console.WriteLine();”. So to print out “Hello!” in C#, you would type, “Console.WriteLine(“Hello!”);”. It is important to note this difference when it comes to printing things in Java versus C#, because many programs print something out in some capacity. So, if you are converting your Java program that prints something out to a C# program, it is important to know this key difference.
- One important thing to know about when working with classes in C# is the idea of a “namespace”. A namespace is used to organize code. This is used in C# when one is working with classes that are spread across multiple files. So if you wanted to create a main file for a C# program that uses a class located in a different file, you should use this namespace keyword. Once you have created a namespace with the class you want to use, in the class you want to use it in you must import it using the “using” keyword. Note that if you are creating a program with all of it’s classes in one file, it is not necessary to use this keyword. Such an idea is not utilized in Java so it is important for a Java programmer to know about this if they intend to convert their Java classes into C# classes.
- When comparing classes in Java to classes in C#, there are also differences in classes overall. According to developerdrive.com, Java offers full support for instance-level inner classes, whereas if you wanted to use this type of class in C#, you would have to do several workarounds to do this. Additionally, Java does not support partial classes in any capacity whereas C# does. It is also important to note that there is a difference between Java and C# when it comes to anonymous classes. An anonymous class is a class without a name. In Java, anonymous classes are implicit, whereas in C#, anynomous classes are defined at the statement-level. For a Java programmer who wants to convert their Java classes to C# classes, it is important to know these differences about how classes function in each language.
- Reference: http://www.developerdrive.com/2012/06/5-differences-between-c-and-java-objects-and-classes/
This simple guide should help a Java programmer successfully convert their Java classes into C#. There are actually many similarities between Java and C#. For example, {}’s are used in accordance with methods in both languages. Similarly, semicolons appear at the ends of lines in both languages. Both languages are also object-oriented programming languages. However, there are many differences between the two languages. For example, Java does not support object overloading but C# does. As mentioned in the guide, there are also differences when it comes to printing lines and declaring variables such as that of a string. Overall, Java and C# are both object-oriented programming languages that are very similar but face several subtle differences that could confuse a Java programmer who is trying to convert their Java programs or classes to C#.