Java

How top 10 programming languages work

TIOBE index: July 2014

I am programming for more than 10 years. I realized that, when I found my first, personal home page (written in PHP) on my hard drive. Some files have last modification date: May 2003. Since that time I was working (or playing) with 9 of top 10 programming language from TIOBE Index list. As I mentioned, I started with PHP, then I learnt a little bit JavaScript (to create fancy menus or ‘secret pages’ on my website). In high school I started learning C++. At the University I learnt C, more C++ and Java. Then I learnt C# on my own, and it is still my favorite language. Along with C# I learnt ASP.NET and T-SQL (to be able to create websites with databases). I also learnt Python and Objective-C. Former became useful when I started my Research Assistant Job.

More experience I gain, more interested I am in how things work underneath the code level. In this post I want to provide short description of the Top 10 programming languages (by TIOBE Index), how they work and their super short history.

PHP

Interpreted language created by Rasmus Lerdorf. Originally, collection of Perl scripts, rewritten to C for performance reasons, ability to work with web-forms and communicate with databases. Most popular interpreter: Zend Engine. Since PHP 4, the PHP parser compiles input to produce bytecode for processing by the Zend Engine. Facebook developed two PHP interpreters: HipHop (transforms the PHP scripts into C++ code and then compiles) and HipHop Virtual Machine (converts PHP into a high-level bytecode, which is then translated into x86-64 machine code dynamically). Recommended book: PHP and MySQL Web Development.

JavaScript

Dynamic, interpreted and prototype-based language. JavaScript is superset of ECMAScript standard. Usually used as part of web browser, but also in server-side (Node.js) or even for desktop and mobile application development. Current browsers perform just-in-time compilation. There are many JavaScript engines (interpreters): Rhino (written in Java), SpiderMonkey (written in C, first JS engine), V8 (written C++, by Google, used in Google Chrome), Chakra (performs JIT compilation on a separate CPU core, parallel to the web browser; created by Microsoft, used in Internet Explorer) etc. Recommended book: JavaScript: The Good Parts.

C/C++

C is compiled, static type language created by Denis Ritchie. It influenced many other languages, e.g.: C++, C#, Java, JavaScript, Objective-C, Perl, PHP, Python, and even Unix’s C Shell. C++ is nearly a superset of C, with object oriented features inspired by Simula language, created by Bjarne Stroustrup. C and C++ are compiled to machine specific code, thus platform specific compiler is needed. Most popular compilers: GCC and Visual C++. C++ evolve, Modern C++ (currently C++ 11 and vNext: C++ 14) introduces: smart pointers, for each (range for), lambda functions and much more. Recommended book: Ivor Horton’s Beginning Visual C++ 2012The C Programming Language (by Brian Kernighan and Denis Ritchie)  used to be one of the most popular programming books ever.

Java

C-based language created by James Gosling, which introduces automatic memory management by Garbage Collector. Java is compiled to bytecode (.class files), which runs on JVM (Java Virtual Machine). Its main design goal is “Write Once, Run Anywhere” (WORA). The same code can be compiled to the same bytecode on different machines. Then JVM (platform-specific) translates bytecode to particular machine code during run-time (Just-in-Time (JIT) compilation). Java applets can run in web browser. My personal recommendation for Java book is Hortsmann’s Core Java Volume I – Fundamentals and Volume II – Advanced Features. Well known Java book is Bruce Eckel’s Thinking in Java.

C#

C# is Microsoft’s version of Java language created by Anders Hejlsberg. However, C# is closer to C++ in case of design and syntax. It is compiled to Intermediate Language (equivalent of bytecode in Java) and runs on Common Language Runtime (equivalent of JVM). C# also uses Garbage Collector. It has many features not existing in Java like rich native interoperability, query language (LINQ) or dynamic type. For more, check Comparison of C# and Java. Book for quick start: Sams Teach Yourself Visual C# 2010 in 24 Hours recommended by Eric Lippert (former developer of C# compiler). To dive in, check: C# 5.0 in a Nutshell: The Definitive Reference by Joseph Albahari and Ben Albahari, C# in Depth by Jon Skeet and CLR via C# by Jeffrey Richter.

Python

Dynamic, interpreted language, created by Guido van Rossum. Most popular implementation: CPython (implemented in C). It compiles Python programs to intermediate code (.pyc files) and runs them on Virtual Machine. There are many other implementations, e.g. Jython, which compiles Python to Java Bytecode (.class files). Python is much more expressive language than C or Java. Some constructs, which needs 10 lines of C code or 3 lines of Java code, requires only 1 line in Python (check reverse words example in my post about Python). Python uses whitespace indentation, rather than curly braces or keywords, to delimit blocks. To get started I recommend Google’s Python Class (videos section) created by Nick Parlante. Other resources: Python tutorial and Dive into Python. You can also check my post Python jump start for more details.

Objective-C

Strict superset of C language with object-oriented capabilities inspired by Smalltalk. Every C program can be compiled by Objective-C compiler, C code can be included within Objective-C code. Most popular compilers: Clang and LLVM. Instead of calling methods (like in C++: obj->method(param)), messages to objects are being sent ([obj method:param]) and resolved at runtime (not at compile time like in C++). There is no Garbage Collector (which works at run-time) in Objective-C, but instead Automatic Reference Counting (compile-time feature) is used. Objective-C is the main programming language used by Apple for the OS X and iOS. However, in this year Apple announced plans to replace Objective-C with new Swift language. Resources to learn Objective-C can be found in my other post.

Transact-SQL

Declarative language for managing data held in a relational database management system (RDBMS). Created by Microsoft (for MS SQL Sever), T-SQL is an extension to SQL, which makes it Turing complete. It adds to SQL: procedural programming, local variables, functions for string and date processing, mathematics etc. and allows FROM clause in DELETE and UPDATE statements (which allows joins to be included). T-SQL (and SQL) query differs from program in such a way that they just tell what to do, not how to do that. Figuring out how to execute query is a role of query analyzer. Check Understanding how SQL Server executes a query.  To learn T-SQL, you should learn SQL first. Recommended tutorial: T-SQL Step by Step Course (video tutorial). Recommended books: Itzik Ben-Gan’s Microsoft SQL Server 2012 T-SQL Fundamentals and other his books.

(Visual) Basic (.NET)

Compiled language created by Microsoft. Compiles to native language or P-Code and uses the Microsoft C++ compiler to generate the executable. It derives from BASIC. Similar to Python: tabs and new lines are used to delimit blocks. VB (under Visual Studio) allows to create GUI using drag-and-drop technique. Last version (VB6) was released in 1998 and is abandoned in favor of VB.NET, which introduces many features (present also in C#), but still supported, even on Windows 8. Check Comparison of VB and VB.NET. Both (VB.NET and C#) run on the same run time (CLR). More details can be found here. Recommended tutorials to get started: Visual Basic Fundamentals: Development for Absolute Beginners by Bob Tabor and Visual Basic .NET Tutorials. Good reference is Visual Basic at Wikibooks. To get VB syntax highlighting in SublimeText, this package works.

Summary

I read about all 10 languages in Wikipedia before writing this post. I was surprised how much I could learn in 5-10 minutes reading. I don’t know why, but I feel much better now, when I refreshed and organized my knowledge. If you want to start learning about new programming language, I recommend you to read about it on Wikipedia first. Even more, read about all languages you are using now on Wikipedia as well. It gives you great, high-level overview. Now, Wikipedia will be my start point of research about “some new thing”.


Medical Device Coordination Framework

Patient room - future

As a Research Assistant at SAnToS Lab I am working on Medical Device Coordination Framework. It is a long term research effort. MDCF is an implementation of Integrated Clinical Environment. There is many people involved in it. Not only from my University. There are folks from University of Pennsylvania and some other researchers who cooperate with us. My part of research is focused on code translation. You can call it Model Driven Development. The long-term research goal is to create a AADL/BLESS to SPARK Ada translator for Medical Devices.

Technologies

AADL stands for Architecture Analysis & Design Language. It is modeling language, which allows to describe hardware and software parts of the system. It can be represented in graphical and textual form.

BLESS stands for Behavioral Language for Embedded Systems with Software. It is an extension to AADL language. AADL can be extended by language annexes. BLESS is one of them. It is designed to prove embedded software/system conformance to specification. In other words: to verify the AADL model.

SPARK is a programming language and static verification technology designed specifically for the development of high integrity software. It is subset of Ada programming language. It contains all features of Ada, except:

  • Pointers
  • Exceptions
  • Aliasing between variables
  • All Concurrency features
  • Side effects in expressions and functions

However, it contains toolset for Software Verification, such as:

  • Examiner – analyze code and ensures that it conforms to the SPARK language; also verify program to some extent using Verification Conditions (VC)
  • Simplifier – simplify Verification Conditions generated by Examiner
  • Proof Checker – prove the Verification Conditions

I blogged about SPARK Ada recently.

Research background

The ultimate goal has a big scope. The goal is to deliver a platform, which allows different medical devices to work together. Moreover, there will be monitored and controlled by centralized system.

We use and create a lot of different technologies. From AADL, BLESS, DML to SPARK Ada, Java, through many other. Most of them are still under development. I work mainly with AADL, BLESS and SPARK Ada. The biggest challenge for me is to understand how AADL models should be mapped to SPARK Ada in real-World. Unfortunately, I cannot find some Open Source examples on github. Companies treat it as their intellectual property, and do not share it. More over, the AADL committee still did not decide how some AADL constructs should be mapped to Software. Thus, my role is to propose it (based on existing work).

There is an AADL to Ada code generator: Ocarina. It is written in Ada and it uses Ada features, which are not in SPARK language. However, it is a good point to start. Another resource I am based on is Aerospace Standard – Architecture Analysis & Design Language (AADL) V2 Programming Language Annex Document. It describe data types translation and base for ports communication.

The essence of AADL models is port-based communication. Usually implemented in publish-subscribe mode. It allows to interaction between different components of the system.

Sample medical device

To create code translator for Medical Devices, I needed some example of Medical Device. One member of our research group, Brian Larson created AADL/BLESS models of Patient-controlled analgesia Infusion Pump. This device, is used to infuse a pain killer. The dose can be controlled by patient. He also created Open PCA Pump Requirements. Document, which describe precisely functionalities of PCA Pump. My role is to develop AADL/BLESS to SPARK Ada translation schemes and translate existing PCA Pump models, to assure that translation makes sense.

PCA Pump

Research plan

There are many issues and problems to solve in this project. One of them is SPARK limitation. Besides excluded features mentioned above, SPARK 2014 does not support multitasking (yet). SPARK 2005 support it in some extent (with Ravenscar profile). Thus I use mainly SPARK 2005.

The plan is as follows:

  • Create AADL/BLESS to SPARK Ada translation schema
  • Manually implement working PCA-Pump prototype on BeagleBoard-xM based on Requirement document to find out possible implementation issues
  • Manually translate AADL/BLESS models to SPARK Ada, reusing code created during implementation
  • Create AADL/BLESS to SPARK Ada translator based on manual translated code

How it goes?

I started working on this project around 1 year ago. Most of the time, I spend on studying AADL, SPARK Ada, BLESS and Ravenscar Profile. I thought, I would accomplish much more. However during the process I faced a lot of problems and I had to Shave the Yak. First challenge was to run SPARK Ada code on BeagleBoard-xM device. In cooperation with Ada Core, we got cross-compiler, which works for its processor. It took around 3 months, because cross-compiler was under development. After around half a year of exploring, I came up with AADL ports translation schema (based on AADL annex document). Simultaneously, I was working on PCA Pump implementation (based on its Requirements Document). Most of it is described in SPARK Ada programming for ARM-based device(BeagleBoard-xM). It allowed me to better understand SPARK Ada development environment and create translation schemes for AADL packages threads. SPARK Ada development is much different than Java, C# or Python. After many hours spent on AADL/BLESS models analysis, I created mapping from BLESS to SPARK Ada.

For now, most of the work is done. Especially, translations schema. I have less than 2 months to finish everything. My plan is to refactor existing PCA Pump implementation and create final mock of code translated from AADL/BLESS models. Additionally I want to add implementation to some ‘translated’ part and apply SPARK Verification tools and AUnit tests. Finally, I will need to describe everything in my Master Thesis.

My thoughts so far…

Development for safety-critical systems is totally different, than creating Web or Mobile applications. My research adventure is very fascinating and allow me to discover new things everyday. It gives me different perspective of Software Development, which will be useful in the future. No matter if I would work for company producing Aircrafts or Space Shuttles. It will be valuable also for Web/Mobile Start-up in Silicon Valley or personal Mobile App. Finally I understand where UML and variety of Software Design Processes can be applied. Programming in SPARK Ada requires deep thinking and understanding about how every small part of the program work. There is no high abstraction layer, where calling some library function will do all work. It allows to understand what is underneath and how powerful languages like C#, Java or Python are.

If you are at the University now, I strongly recommend you to join some Research Group. Or at least talk to some of your professors and ask if you can do some Research-oriented project for him. No matter, if you are Graduate or Undergraduate student. If research topic is out of your comfort zone, then it is even better!

outside comfort zone


Interprocess communication between Python and Java

For my Research work I need to communicate between Python and Java/Scala application.

The first idea was to use WebSockets. It is universal and flexible. Additionally communication is easy. There are many ways to implement it:

Implementation would be easy, but we are using customized version of Python, which is included in GNAT Programming Studio. Some libraries (required for WebSockets) are excluded (because of legal reasons) and we wouldn’t be able to redistribute it along with missing packages. Thus, we decide to implement interprocess communication through pipes.

From Python to Java

The communication is one direction: from Python app to Java app. Every message is one line of text (ultimately: json format). E.g.: “text\r\n”. To close communication, we send message: “x\r\n”.

This is sample Java app, which write received messages to file result.txt:

import java.io.*;

public class MyClass {
	public static void main(String[] args) {
		try {
			BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
			PrintWriter writer = new PrintWriter("result.txt", "UTF-8");
			String s = bufferRead.readLine();
			while(s.equals("x")==false) {
				writer.println(s);
				s = bufferRead.readLine();
			}
			writer.close();
		} catch(IOException e) {
			e.printStackTrace();
		}
	}
}

And here is sample Python app, which sends messages to Java app:

#!/usr/bin/python

import subprocess

p = subprocess.Popen(["java", "MyClass"], stdin=subprocess.PIPE)
p.stdin.write("First line\r\n")
p.stdin.write("Second line\r\n")
p.stdin.write("x\r\n") # this line will not be printed into the file

From Java to Python

We can also communicate in opposite direction: from Java app to Python app. To do that we just need to change reading to writing in Java app and writing to reading in Python app.

Change in Java app is simple: we just write to standard output.

import java.io.*;

public class MyClass2 {
	public static void main(String[] args) throws InterruptedException{
		System.out.println("First msg");
		System.out.println("Second msg");
		System.out.println("x");
	}
}

In Python app we need to create stdout pipe and read instead of write:

#!/usr/bin/python

import subprocess

p = subprocess.Popen(["java", "MyClass2"], stdout=subprocess.PIPE)
line = p.stdout.readline()
while(line != "x\n"):
	print line
	line = p.stdout.readline()

In this scenario, both processes are managed by Python app. You can also do it in opposite direction and invoke Python app from Java app.


Multithreading: C# vs. Java

In my pervious post I described basic multithreading constructs in C#. Now, I would like to compare them to conforming constructs in Java. It might be useful for those of you, who has already created some multithreaded applications in Java, and would like to learn how to do the same in C#.

Creating a new thread

C#:

using System;
using System.Threading;

class ThreadTest
{
  static void Main()
  {
    Thread t = new Thread (Method);
    t.Start();    
  }
 
  static void Method()
  {
    Console.WriteLine("Thread started");
  }
}

Java:

public class Program {
  public static void main(String[] args) {
    ThreadClass t = new ThreadClass();
    t.start(); 
  }
}
 
class ThreadClass extends Thread {
  @Override
  public void run() {
    System.out.println("Thread started");
  }
}

Waiting for another thread to finish

C#:

using System;
using System.Threading;

class ThreadTest
{
  static void Main()
  {
    Thread t = new Thread (Method);
    t.Start();   
    t.Join(); //wait for thread t
    Console.WriteLine("Created thread finished");
  }
 
  static void Method()
  {
    Console.WriteLine("Started new thread...");
    Thread.Sleep(1000);
    Console.WriteLine("Finishing new thread...");
  }
}

Java:

public class Program {
  public static void main(String[] args) {
    ThreadClass t = new ThreadClass();
    t.start();
    try {
      t.join(); //wait for thread t
      System.out.println("Created thread finished");
	} catch(InterruptedException e) { 
	  // handle exception
	}
    
  }
}
 
class ThreadClass extends Thread  {
  @Override
  public void run() {
    System.out.println("Started new thread...");
    try {
      Thread.sleep(1000);
    } catch(InterruptedException e) {
      // handle exception
    }
    System.out.println("Finishing new thread...");
  }
}

Accessing shared variable

C#:

using System;
using System.Threading;

class ThreadTest
{
  static readonly object locker = new object();
  static int sharedVariable;

  static void Main()
  {
    Thread t = new Thread (Method);
    t.Start();   
    lock(locker)
    {
      // sample operation
      if(sharedVariable==0)
      {
        sharedVariable = 1;
      }
    }
  }
 
  static void Method()
  {
    lock(locker)
    {
      // sample operation
      if(sharedVariable>0)
      {
        sharedVariable++;
      }
    }
  }
}

Java:

public class Program {
  public static int sharedVariable;
  public static final Object lock = new Object();
  public static void main(String[] args) {
    ThreadClass t = new ThreadClass();
    t.start(); 
    synchronized(lock)
    {
      //sample operation
      if(sharedVariable==0) {
        Program.sharedVariable = 1;
      }
    }      
  }
}
 
class ThreadClass extends Thread {
  @Override
  public void run() {
    synchronized(Program.lock) {
      //sample operation
      if(Program.sharedVariable>0) {      
        Program.sharedVariable++;
      }
    }
  }
}

Signaling

C#:

using System;
using System.Threading;

class ThreadTest
{
  static EventWaitHandle _waitHandle = new AutoResetEvent (false);
 
  static void Main()
  {
    new Thread (Waiter).Start();    
    Console.WriteLine("Wait for notification...");
    _waitHandle.WaitOne();
    Console.WriteLine("Notification received.");
  }
 
  static void Waiter()
  {
    Thread.Sleep (1000);
    Console.WriteLine("Sending notification...");
    _waitHandle.Set();
  }
}

Java:

class Program
{ 
  public static void main(String[] args) {
    ThreadClass t = new ThreadClass();
    t.start();
    System.out.println("Wait for notification..."); 
    synchronized(t) {
      try {
        t.wait();
      } catch(InterruptedException e) {
        //handle exception
      }
    }
    System.out.println("Notification received.");
  }
}
 
class ThreadClass extends Thread {
  @Override
  public void run() {
  	try {
      Thread.sleep(1000);
    } catch(InterruptedException e) {
      //handle exception
    }    
    System.out.println("Sending notification...");
    synchronized(this) {
      notify();
    }
  }
}

Summary

As we can see, threading constructs in both languages are very similar.

I put all above code in github repository: Threading-in-CSharp-vs-Java.

Do you think, there are some other fundamental examples, which should be mentioned in this post?


Garmin Programming Competition 2014

Yesterday at my university, Garmin was hosting the Programming Competition.

Competition like that is a great opportunity to practice programming, algorithms and problem solving skills.

We could form teams of 1-2 students. My partner was Daniel Wang. We could use following languages: C++, C#, Java and Python. We chose Java. We were using Eclipse IDE and git to share the code.

Organizers prepared very real-life problems.

Competitions had two parts:

  1. Read and perform simple input conversion (1 hour).
  2. Reuse code from Program 1 and do more complicated operations with input (3 hours).

We solved part 1 without any problems. Part 2 required two programs: 2A and 2B. We solved both, but our programs didn’t pass all test cases. In 2A: 3/5 test cases passed, in 2B: 4/5. There were points for correct solution and bonus points (depended on the time when solution was submitted). Additionally, there was a timer showing how many bonus points we could earn sending solution now.

You can find problems description here. Our solutions are available on Bitbucket repository. Be aware that it is bad code, written under time pressure and never refactored. However, efficient enough to pass required time constraints. This is how you write code during programming contests 🙂

After competition we asked organizers about test input sets and they sent it to us.

This is the description of the bug we had and how we fix it (you can skip this part if you don’t want to read the description of problems):

The issue we had, was reading (shift mod 26) straight from the input file. Because e.g. shift = 45 should be effectively shift = 19 (45 mod 26). The program should stop when shift = 0. Then, when original shift was e.g. 52 we had stored shift=0 and we stopped (which was wrong). We thought that we should stop when the ‘effective shift’ is 0. Unfortunately, the test cases were stopping when the ‘original shift’ was 0. Fix was pretty easy (we just store two values: shift and shiftOrig for each cell). It would be very hard to fix this without test cases from organizers 😉 It was like in real-life: small detail caused significant error.

The challenge during programming competitions is that you cannot get all test cases. This time, there were 1 sample input per program. Which make sense, because otherwise everybody could just hardcode expected solution 🙂 What is worse, you cannot see the program output (in case of runtime error). Fortunately, guys form Garmin were nice and when we asked, they were showing us the output (on their machine). It helped us (a lot) in fixing the runtime errors (during the competition).

The next event I am looking forward is Google Code Jam. It starts in April 11, 2014 (Friday). Registration begins in March 11. I really encourage you to sign up. You can learn what various problems you can face and what you should consider during writing your programs. Additionally you can practice, apply your programming skills and compete with the best programmers in the World.