Which language on the Raspberry Pi?

Image
What language should you use on the Raspberry Pi? There is quite a choice, including Python, Scratch, Java, C, Julia, and that's not counting the web stuff like Javascript, Html and Php.
There is no definitive answer - it depends on what you want to do. If you are teaching a child the basics of programming then Scratch might be a good choice. If you are an older learner then you might go for Python or Java with Greenfoot.
If you are interested in programming the Raspberry Pi hardware then there are libraries for pretty much any language that will let you do that. 
If you want a graphical user interface then you may want to give C a miss. But not necessarily because you can tack on a GUI in almost any language these days.
So where does that leave us?
Here is a list of some of the programming languages available for the Raspberry Pi with my brief personal notes and an example program for each one (except Scratch).

Advertisement

Scratch This is a graphical language designed to tea…

A little bit of Java


Java is a comprehensive, general purpose language. It has a ton of libraries available for it, some come built in when you get it, others you can download. One of the most important libraries, Swing, let's you build vsophisticated user interfaces.

So Java can be used to build desktop applications but it is also widely used on the web, too. The are several frameworks for building web applications. It is also the language for building Android applications.

All in all, it's a useful language to learn. 

Java is fully object-oriented (although I won't be demonstrating that, here) and inherits much of its syntax from C and C++.

Here's an example. It’s the same guessing game that I've used in my other "A little bit of..."  posts. It gives you a flavour of the language and illustrates some basic programming constructs.

import java.util.Scanner;
import java.util.Random;
public class guess {
// Start here
public static void main (String args[]) {
Random r = new Random();
int x = r.nextInt(9)+1;
for(int i = 0; i < 5; i++){
int answer = getGuess();
     
if (answer == x){
System.out.println("Correct!");
break;
}else{
System.out.println("Wrong!");
}
}
 
System.out.print("The answer was ");
System.out.println(x);
    }
// define a function to get a guess
static int getGuess(){
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a guess (1 to 10): ");
int a = keyboard.nextInt();
return a;
}
}

At the beginning of the program we see a couple of lines that import libraries. They come with Java but they still need to be explicitly imported into the program. 

The rest of the program consists of a single class that had the same name as the file. A class is the basic object-oriented building block in Java. Even when we are not actually writing in an object-oriented style, as here, we still need to wrap our code in a class declaration.

Inside the class are two functions, main and getGuess. The first is where the program starts. I'm going to go through this quite quickly because this is not really a Java tutorial, just a presentation of a simple program so you can get a flavour of Java,

Look at the main function. The first thing we do is create an object of the class Random -  the object is called r. A Random object lets us retrieve random values (surprise, surprise!) and we do this by invoking its nextInt function. We pass the value 9 to it and it returns a value between 0 and 9. We actually want a value from 1 to 10, so we simply add 1 to the result.

Following this is a for loop that iterates 5 times (we get 5 chances to guess the right number)  and inside that loop we get the guess from the user by calling the getGuess function. This function gets a value from the keyboard using a Scanner object.

We then check if the guess is correct and print out and appropriate response.

That's about it. As I said this is by no means a Java tutorial and it does not show its powerful object-oriented features but it, hopefully, gives you a feel for the language.

Back to Which Language for the Raspberry Pi.

Advertisement

Comments

Popular posts from this blog

Just Enough Python

Learn to code with C

Introduction to Python by Andrew Ng