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.print(“The answer was “);
// define a function to get a guess
static int getGuess(){
Scanner keyboard = new Scanner(;
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 (function () { var scriptProto = ‘https:’ == document.location.protocol ? ‘https://’ : ‘http://’; var script = document.createElement(‘script’); script.type = ‘text/javascript’; script.async = true; script.src = scriptProto+’’; (document.getElementsByTagName(‘head’)[0] || document.body).appendChild(script); })();

Source link

A little bit of Java

Leave a Reply

Your email address will not be published. Required fields are marked *