Unit 1 - Migrating From C# To JAVA
Unit 1 - Migrating From C# To JAVA
C# TO JAVA
Data Access Unit 1 – Migrating from C# to JAVA
Content
1. What is C#? ................................................................................................................ 3
Features of C# ............................................................................................................... 3
2. What is JAVA?............................................................................................................ 4
Features of Java ............................................................................................................ 4
3. Advantages and disadvantages ................................................................................. 5
Advantages of C# .......................................................................................................... 5
Disadvantages of C# ...................................................................................................... 5
Advantages of Java ....................................................................................................... 5
Disadvantages of Java ................................................................................................... 5
4. Key differences between C# and JAVA ..................................................................... 6
4.1. Main Function .................................................................................................... 6
4.2. Print statements ................................................................................................ 6
4.3. Constants ........................................................................................................... 7
4.4. Inheritance ......................................................................................................... 7
4.5. Polymorphism & Overriding / Overloading ....................................................... 7
4.6. Multiple Classes in a Single File ......................................................................... 8
4.7. Primitive Types................................................................................................... 9
4.8. Strings ................................................................................................................ 9
4.9. Array Declaration ............................................................................................... 9
4.10. Switch Statements........................................................................................ 10
4.11. Importing Libraries ....................................................................................... 10
4.12. Properties ..................................................................................................... 10
4.13. Pass by Reference ........................................................................................ 11
4.14. Exception handling ....................................................................................... 12
5. Activities .................................................................................................................. 12
6. Bibliography ............................................................................................................ 12
1. What is C#?
Developed by Microsoft around 2000, as its .NET initiative, C# is a multi-paradigm
programming language. It offers programming disciplines like strongly typed,
lexically, scoped, generic, object-oriented, and component-oriented. C# is one of
the programming languages that is designed for the CLI (Common Language
Infrastructure).
Features of C#
2. What is JAVA?
Developed by Sun Microsystems and designed by James Gosling in 1995, Java is
a class-based, object-oriented multi-paradigm language. It is designed with the
WORA concept i.e. Write Once Run Anywhere and thus is called platform-
independent language. Applications in Java are compiled to bytecode that can
run execute on any JVM regardless of its underlying OS and architecture. It offers
its applications in web development and other Android-based software for
several devices. High-level applications of Java include embedded systems,
desktop, and applications. Devices such as smartphones, ATMs, home security
systems and more are all supported by Java.
Features of Java
Disadvantages of C#
Poor GUI.
The application must be windows based as C# is an internal part of the
.NET framework.
Software is proprietary, so it requires an upfront purchase.
C# mostly depends on the .Net framework, and so is less flexible.
C# executes slowly, and the program needs to be compiled each time
when any changes are made.
Advantages of Java
Disadvantages of Java
C#:
static void Main(string[] args)
Java:
public static void main(String[] args)
C#:
System.Console.WriteLine("Hello world!"); or
Console.WriteLine("Hello again!");
Java:
System.out.println("Hello world!");
4.3. Constants
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.
Java:
In Java, compile-time constant values are declared inside a class using the
keyword final:
4.4. Inheritance
C# uses C++ syntax for inheritance, both for class inheritance and interface
implementation as opposed to the extends and implements keywords.
C#:
class B:A, IComparable
{
…………
}
Java:
class B extends A implements IComparable
{
……………
}
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
Example:
using System;
To call a base class method, you use the keyword base in C# and super in
Java.
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.
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:
The only significantly different primitive in C# is the decimal type, a type which
stores decimal numbers without rounding errors. Eg:
4.8. Strings
Java does not allow operator overloading. Thus, the following valid sentence in
C#:
String myString;
if ( myString == “value” )
It’s not valid in Java. Instead, we have to use the equals method:
if ( myString.equals( “value” ) )
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) {
Both languages support this functionality and C# follows Java’s technique for
importing libraries:
using System;
using System.IO;
using System.Reflection;
import java.util.*;
import java.io.*;
4.12. 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.
C#:
public int Size {
get { return size; }
set { size = value; }
}
Or
Java:
public int getSize() {
return size;
}
public void setSize ( int value ) {
size = value;
}
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.
Both C# and Java include an exception handling mechanism, but while C# only
handles unchecked exceptions, JAVA supports both checked and unchecked. You
can learn more about the JAVA handling mechanism here:
https://beginnersbook.com/2013/04/java-checked-unchecked-exceptions-
withexamples/
5. Activities
1. Download the .cs files (C#) in the platform . You should translate them to
English (that’s to say, name of methods/properties/members/variables,
and then convert them into JAVA format (the easiest way to do that is to
paste them into a new project/file in IntelliJ IDEA and compile to detect
the differences between C# and JAVA.
2. In exercise 2, add two more classes: AlumnoFCT and AlumnoErasmus. Both
of them must inherit from Alumno. AlumnoFCT will have three specific
attributes: empresa, tutor e instructor (the three of them are strings).
AlumnoErasmus will have the starting and ending date of the internship,
and the origin country.
3. (Optional) Modify the form in exercise 2 to add the two new types of
students.
6. Bibliography
https://en.wikipedia.org/wiki/Comparison_of_C_Sharp_and_Java
https://www.janbasktraining.com/blog/java-vs-c-sharp/
https://hackr.io/blog/c-sharp-vs-java
https://www.educba.com/java-vs-c-sharp/
https://www.geeksforgeeks.org/java-vs-c-sharp/