Some thoughts on Java and C#

Similarities and differences

A coffee with milk and ice cubes, and a piece of cheese cake on a white plate
Mastering the differences between Java and C#? Piece of cake :-)

Although Android is leaving Java behind at an incredible pace, there is still a seemingly infinite amount of samples written in Java on the web. If you are coming from another programming language, for example C#, it may be beneficial to know what’s going on. Let’s take a look at a very basic Android activity written in Java:

package com.thomaskuenneth.testapp1;import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activitymain);
}
}

The very first line contains a package statement. Packages organize a set of related classes and interfaces in a namespace. To which package a class or interface belongs is specified using the package keyword. As you can see in my example, such names can be rather long (com.thomaskuenneth.testapp1). A package name follows some conventions, for example it resembles domain names in reverse order. Also, it is lower case only. To free the developer from the hassle of having to type it every time a class is referenced, the import statement may be used. In the line starting with public class I refer to Activity, whose fully qualified class name is android.app.Activity.

The extends keyword makes MainActivity a child of Activity. As Java is a single inheritance language, there is only one direct ancestor. @Override is an annotation. It documents that the method onCreate() is present in the parent class, too. super.onCreate() invokes the implementation of the parent. Finally, protected is an access modifier that controls who (that is, objects) can call that method.

Now, let us turn to C#. The following snippet uses Xamarin.

using Android.App;
using Android.Widget;
using Android.OS;
namespace TestApp1
{
[Activity(Label = “TestApp1”, MainLauncher = true)]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main);
}
}
}

C# has the concept of namespaces, too. In my example a namespace called TestApp1 is declared. Real-world-applications should follow Microsoft’s namespace conventions. To reference other namespaces, the using directive is used. However, Android.OS is not a class but a namespace. To achieve a similar effect in Java, one would write

import android.app.*;

If the wildcard should be used has always been debated heavily. One reason is that if you use more than, say, 10 classes the import block becomes very big. On the other hand, almost all IDEs allow us to collapse it…

Before moving on, I feel the need to remind you that the mechanics in Java and C# are not equal, so please refer to the language specifications of C# and Java for further detail. In particular, C# knows something like

using (MyResource myRes = new MyResource())
{
myRes.DoSomething();
}

This ensures that myRes is disposed automatically as soon as it is no longer accessible. Java uses a similar mechanism in combination with try-with-resources. However, the idea of resource is restricted to the idea of something being closeable (java.lang.AutoCloseable).

In C#, inheritance is documented with a colon. MainActivity is a direct descendent of Android.App.Activity. What super is for Java, is base for C#. Both refer to the direct ancestor.

Have you spotted the line starting with [ and ending with ]? Here we see so-called attributes. Frankly, they remind me of annotations, but I would love to hear someone comment on this. See the docs for further reference. Finally, a minor, yet important difference: remember when to start something with a uppercase letter. In Java it is setContentView() and onCreate(), whereas in C# we have OnCreate() and SetContentView().

There are more similarities and differences. Which of then do you find hard to remember? Tell me, I’d love to hear them…

A previous version of this article was published 2017/05/30 on my Blogger.com blog Tommis Blog. I deleted the weblog in the wake of the GDPR (General Data Protection Regulation), so the original post is no longer available (or only through the Wayback machine of the Internet Archive).

If not stated otherwise images are © Thomas Künneth

--

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Thomas Künneth

Thomas Künneth

Google Developer Expert for Android. Author. Speaker. Listener. Loves writing.

More from Medium

Type Conversion in Java

Java Sealed Class

Need for Abstract Classes and Interfaces in JAVA.