Some thoughts on Java and C#

In a couple of weeks I will be giving a talk about best practices in native Android app development using Xamarin during Developer Week 2017 in Nuremberg. So, time to freshen up my C# knowledge. Care to join me? Fine. 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 {

    protected void onCreate(Bundle savedInstanceState) {

The Java™ Tutorials explain:
A package is a namespace that organizes a set of related classes and interfaces. 
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#.

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)

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 still is debated heavily, at least now and then. 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())

This ensures that myRes is disposed automatically as soon as it is no longer accessible. try-with-resources, anyone? :-)

Inheritance is documented in C# 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 couple of screenshots from Android Studio 3.0

The final Android O SDK should arrive sometime in July or August, with Android O to follow shortly after. Along with the new Android version we will see a new major version of Android Studio. Google has released a preview recently, so why not take it for a spin? The following two screenshots show the known project assistant. Have you spotted the added support for Kotlin, as announced during I/O 2017?

Spot the new Kotlin support option

Choosing the platform

The main windows has not changed a lot. Winking smile Then again, please take a closer look at the editor window, in particular the filename.

Android Studio main window

.kt is the extension for Kotlin source files. And : means extends. Classes in Kotlin can have properties. These can be declared as mutable, using the var keyword or read-only using the val keyword. Remember, Swift uses let for the latter one. Winking smile

Are you going to switch to Kotlin? I’d love to hear your opinions.


Quick tip: Setting the application implementation version for native app bundles

If you have set up your NetBeans project to use native packaging, you can set the implementation version of your app by adding a line such as


to nbproject/project.properties. The version is for example used in the filename for .msi installation files. And you can access it from inside your Java code:

String version = getClass().getPackage().getImplementationVersion();
if (version == null) {
    version = …

Can you share some tips regarding NetBeans native packaging? I’d love to hear them…


Drawing on screen

In this post I would like to share my experience regarding drawing on screen.

Now, what do I mean by this? First, consider a pair programming session. Your fellow programmer and you are sitting in front of the screen. You wish to discuss something that is currently on display. For whatever reason your colleague does not get what you want to tell. So you end up pointing at an area on the screen with your finger. While this may work during a one on one, it does not scale well in mob programming sessions. So, you might end up using a laser pointer, just as you might do during ordinary presentations.

But… Why not simply draw on screen?

The idea is far from new, presentation programs have just the right tools for this. The following screenshot shows what Powerpoint offers while in presentation mode.

Choosing how to draw on screen in Powerpoint

Then again, my initial scenario did not involve a presentation. Windows 10 Anniversary Update introduced the Windows Ink Workspace, which you can see in the following screenshot:

Windows Ink Workspace

Screen Sketch takes a screenshot and gives you a couple of drawing tools you can use to annotate, save and share it. However, as you can see in the following screenshot, editing does not take place full screen. The upper right portion of the screen contains the drawing tools. The bottom shows the grayed out taskbar. On my monitor, a saved screenshot was 1728 by 972 pixel, compared to its original resolution of 1920 by 1080 pixel. That border in my opinion limits the impression of directly drawing on screen.

Drawing on a screenshot

A very good alternative in my opinion is Epic Pen. Here is how it looks like:

Screenshot of Epic Pen

The program is (at the time of writing this) free for personal use and has a nice clean interface.

If you are on a Mac, I suggest taking a look at Highlight by Nicolas Zinovieff. It has an even more reduced user interface and offers few paint modes. But in my experience, painting on screen does not require much features. Draw something freehand, draw a circle or a line. That’s it. Remember, I used this to draw someone’s attention to something on display – nothing more.

Do you use similar tools? Which one do you prefer?


for ever young – musings about for loops

Welcome to another installment of my series about polyglot madness. Winking smile

Today we will take a look at one way to do things repeatedly. Let’s tart with BASIC yet again.

10 FOR I = 1 TO 3

The FOR keyword defines both start and end value. Anything that follows until the NEXT keyword is executed repeatedly, in my case, that is just printing a number. Have you noticed the loop variable after NEXT? Most BASIC dialects allow to omit it. In this case the most recent loop is used. Now take a look at this one:

10 FOR I = 1 TO 5 STEP 2
15 FOR J = 1 TO 3

Screenshot of an Oric emulator

The STEP keyword is used to change the increment or decrement value. Before we go back to the future, please note that start, end and increment value are computed prior to the execution of the loop.

Here is how JavaScript handles for loops:

for (i = 1; i <= 3; i++) {

This surely looks familiar to C, Java and C# developers. There is a nice alternative usage, this time written in Java:

String [] a = {"Java", "C#", "JavaScript", "..."};
  for (String b : a) {

If we translate this into JavaScript, we get something like:

var a = ["Java", "C#", "JavaScript", "..."];
for (var b in a) {

If we run it, we get… the numbers 0 to 3. Oh my.  In defense of JavaScript I should say that I misused in. The for...in statement is meant to be used to iterate over object properties. To get my above example working as intended, we have to use of instead of in.

Now let’s turn to Swift. Take a look at the following code fragment.

let a = ["Java", "C#", "JavaScript", "..."]
for b in a {

This resembles my JavaScript example. But please not the missing paranthesis. To count from 1 to 10 is as simple as:

for b in 1...10 {

However, counting down from 10 to 1 has a caveat. Using 10...1, the code will crash. To achieve this, we can use the reversed() instance method:

for b in (1...10).reversed() {

Next: Dart:

var a = ["Java", "C#", "JavaScript", "..."];
for (var b in a) {

Looks familiar, right? As Dart is greatly influenced by C-like languages, the three-part variant works as well:

for (var i = 1; i <= 10; i++) {

That’s it for today. Stay tuned.


Some reflections on functions

Regular readers know my fascination for home computers. So, please take a look at the following small BASIC program:

20 GOSUB 70
30 FOR I = 1 TO 3
40 GOSUB 70
60 END

Screenshot taken from a emulated C64

In BASIC you can reuse code through so-called subroutines. They are invoked using the GOSUB keyword. END marks the end of the subroutine. As you can see in the above example, subroutines can be called from different parts of the program. However, subroutines are no functions, as they lack the ability to return a vlaue and to receive parameters. This is done as follows:

10 DEF FN POW2(X) = X * X

So, DEF FN defines a function, whereas FN invokes it.

JavaScript has the function keyword. It is used as follows:

function pow2(x) { return x * x; }

To invoke the function use something like pow2(3)

Swift is similar, but uses the func keyword:

func pow2(x: Double) -> Double { return x * x; }
print(pow2(x: 3.0))

Dart can be very terse:

pow2(x) => x * x;

Or more descriptive:

double pow2(double x) {
  return x * x;

Finally, Scheme:

(define pow2 (lambda (x) (* x x)))
(pow2 3)

What other language feature would you like to see compared? Feel free to suggest your favorites...


Developing Flutter apps on Windows

Just a quick reminder that it is now possible to develop Flutter apps on Windows, too. The setup consists of a couple of easy steps and is well documented here. If you have already installed Android Studio, it boils down to:
  • Cloning the Flutter repo
  • Setting the PATH environment variable
  • Running flutter doctor
  • Picking your favorite editor and installing a Flutter plugin
I prefer IntelliJ, which offers a seamless integration of Flutter. Below is the screenshot of my sample Flutter app called Temperature Converter.
Screenshot of my temperature converter
Please keep in mind, that you cannot build iOS apps on a Windows machine.

Quick tip: stop your Android SDK Tools Blues

As you all know, SDK Tools, Revision 25.3.0 (March 2017) brought significant changes to the Android SDK commandline tools. Most notably, the android command is no more.

I used that command frequently in order to list my Android virtual devices: android list avd.

To do that now, just use emulator -list-avds

To launch the emulator with an avd, type emulator @avd_name, where avd_name is returned by the above command.


Setting the Java version for NetBeans

Just a friendly reminder, primarily to myself- ;-) You can override the Java version to be used for running NetBeans in the file etc/netbeans.conf.

As you know I gave two talks during our company conference MATHEMA Campus. One was about jshell which naturally ;-) required me to install Java 9 early access. Today, back in the office, I wanted to do some programming. I launched NetBeans on my Mac - and after a couple of seconds it terminated. It was clear that this had to have to do with the early access install. However, I did not want to remove it. So - what to do?

/usr/libexec/java_home, as you may know, lists installed Java versions. It (expectedly) pointed to /Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home.

I hat already set JAVA_HOME to the Java 8 installation, but this certainly has no effect on app bundles. This is where etc/netbeans.conf comes in handy. It's inside the NetBeans installation directory, for example /Applications/NetBeans/NetBeans 8.2.app/Contents/Resources/NetBeans.

Just remove the hash symbol at the beginning of the line #netbeans_jdkhome= and enter the path to a jdk installation. On my machine this currently is /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home.


LET it be

In my previous post Some thoughts on being a polyglot developer I pondered about the usage of arrow constructs in programming languages. This time I will reflect on the let keyword.

As you know, I love home computers. So, let’s (ah, no pun intended) start with them. The following screenshot, by the way, shows the Oric emulator Oricutron by Peter Gordon.

Screenshot of the Oric emulator Oricutron by Peter Gordon

Most (but certainly not all) home computers offered a builtin BASIC interpreter and a (full screen) editor. As BASIC aimed to be easily learnable and understanable, the language sounds like short english sentences. For example, to assign a value to a variable, one might say Let A be Hello World. This translates to let a$ = “Hello World”. The dollar sign is needed to represent sequences of characters.

Other languages use let for assignment, too. There is an interesting question In what programming language did “let” first appear? on Software Engineering. Well. So far, so good. But we don’t use BASIC, do we?

Winking smile

ES6 brought let to the web. Instead of var we can now say

class black {}
let orange = new black();

The difference between both keywords is scoping. var is scoped to the nearest function block and let is scoped to the nearest enclosing block (which can be smaller than a function block). Both are global if the are outside any block. Variables declared with let are not accessible before they have been declared in their enclosing block. Further assignments to an already declared variable (using let) are valid. Why am I emphasizing this? Let us take a look at Swift. This language knows let, too. However assignments can be made only once. As you can see in the following screenshot, for non-constant-like usages we need to take var instead.

A Swift session at https://iswift.org/playground

The Dart programming language is optionally typed. That means that we can either write

var a = 42;


int a = 42;

Both result in int being printed.

To declare constants, Dart has the const keyword:

const a = 42;

Again, int is printed. Dart offers compiletime constants, so const a = new Random().nextInt(10); is not valid. A similar expression in ES6, however, is valid:

const c2 = Math.random();

The same is true for Swift’s let. The assigned value can be computed during runtime.

let a = random()

Speaking of const, remember that const is a reserved keyword in Java, but currently not used. C#, on the other hand, does:

public const double Pi = 3.14159;
Well, that has been enough of brain dance for now. 
Happy coding!