java4python 3.0 documentation

Getting Started

«  Preface   ::   Contents   ::   Basics  »

Getting Started

Why Java? Dynamic versus Static Languages

Python is a nice language for beginning programming for several reasons.

  • The syntax is sparse and clear.
  • The underlying model is very simple. Everything is an object.
  • You can write powerful and interesting programs without a lot of work.

Python is representative of one kind of language, called a dynamic language. Dynamic languages can be interpreted directly, which means that the actual text of the program — the source code — is used while the program is running. In contrast, a static language is executed in two phases: first the program is translated from source code to binary code, and then the binary code is interpreted. Although the terms dynamic and static language are widely used, the distinction is a fuzzy one. Most execution engines do both translation and interpretation.

  • Static refers to what the translater does. The translater is called a compiler.
  • Dynamic refers to what the interpreter does.

Dynamic languages do more in the interpreter. By doing more in the interpreter, dynamic languages have more flexibility. By doing more in the compiler, static languages gain their own advantages.

  • Compiled languages are fast. They gain speed in at least two ways:

    First, by distinguishing objects from builtin types — aka base types. In order to do basic arithmetic, a dynamic language may need to convert from an object to base type and then back; a static language avoids the conversions.

    Second, by performing name lookups in advance. Dynamic languages use the name of a method (or a derived selector) to look up the method during interpretation. Compilation makes this much more efficient, often resolving the actual method in advance.

  • Compiled languages are excellent for writing large programs that are maintained over time. Python and other scripting languages require that the programmer keep track of a lot of information. For example if you set variable x to reference a turtle, and forget later that x is a turtle but try to invoke a string method on it, you will get an error. Compiled languages track this information using types for variables. This means that programmers need to keep track of less information informally, reducing the chance of miscommunication between to programmers.

    In addition compiled languages support a notion of hiding. Java allows you to declare some data to be private, meaning that it is only accessible by certain method and not others. Python uses naming conventions for the same thing; variables starting with __ are meant to be ignored by most methods, but there is nothing that enforces this.

Python is representative of a whole class of languages, sometimes referred to as scripting languages. Other languages in the same category as Python are Ruby and Perl. Java is representative of what I will call industrial strength languages, which include C++, C# and Scala. Industrial strength languages are good for projects with several people working on the project where being formal and careful about what you do may impact lots of other people.

Why Java?

  • Java is the most widely taught programming language.
  • Java is one of the most widely used programming languages.
  • Java is industrial strength used for large systems by large groups of people
  • Java is simpler than C++ or Objective-C.

Although Java is an enormous language, the core language is very small. The features that we will use are common to every static language. If you want to program in C++ or Objective-C, you should learn Java first.

Hello World

A time honored tradition in Computer Science is to write a program called “hello world.” The “hello world” program is simple and easy. There are no logic errors to make, so getting it to run relies only on understanding the syntax. To be clear lets look a a “complicated” version of hello world for Python:

def main():
    print "Hello World!"

Remember that we can define this program right at the Python command line and then run it:

>>> main()
"Hello World!"
>>>

Now lets look at the same program written in Java:

1
2
3
4
5
public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

What we see is that at the core there are a few similarities, such as a main and the string “Hello World” However there is a lot more stuff around the edges that make it harder to see the core of the program. Do not worry! An important skill for a computer scientist is to learn what to ignore and what to look at carefully. You will soon find that there are some elements of Java that will fade into the background as you become used to seeing them. One thing that will help you is to learn a little bit about Java Naming Conventions.

Running from the command line

The first question you probably have about this little program is “How do I run it?” Running a Java program is not as simple as running a Python program. The first thing you need to do with a Java program is compile it. First we must type the hello world program into a file and save that file using the name Hello.java The file name must be the same as the public class you define in the file. Once we have saved the file we compile it from the command line as follows:

1
2
3
4
5
6
$ ls -l Hello.*
-rw-r--r--   1 bmiller  bmiller  117 Jul 19 17:46 Hello.java
$ javac Hello.java
$ ls -l Hello.*
-rw-r--r--   1 bmiller  bmiller  391 Jul 19 17:47 Hello.class
-rw-r--r--   1 bmiller  bmiller  117 Jul 19 17:46 Hello.java

The command javac compiles our java source code into compiled byte code and saves it in a file called Hello.class. Hello.class is a binary file so you won’t learn much if you try to examine the class file with an editor. Hopefully you didn’t make any mistakes, but if you did you may want to consult the Common Mistakes section for helpful hints on compiler errors.

Now that we have compiled our java source code we can run the compiled code using the java command.

$ java Hello
Hello World!
$

Now you may be wondering what good is that extra step? What does compiling do for us? There are a couple of important benefits we get from compiling:

  • Early detection of errors
  • Faster Program Execution

The job of the compiler is to turn your java code into language that the Java Virtual Machine (JVM) can understand. We call the code that the JVM understands byte code. The JVM interprets the byte code much like the Python interpreter interprets your Python. However since byte code is much closer to the native language of the computer it can run faster.

When the compiler does the translation it can find many different kinds of errors. For example if you make a typo the compiler will find the typo and point it out to you before you ever run the program. We will look at some examples of compiler errors shortly. Chances are you will create some on your own very soon too.

The Anatomy of HelloWorld

Now that we have run our hello world program, lets go back and look at it carefully to see what we can learn about the Java language.

1
2
3
4
5
public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

This simple example illustrates a few very important rules:

  1. Every Java program must define a class, all code is inside a class.
  2. Everything in Java must have a type
  3. Every Java program must have a function called public static void main(String[] args)

Lets take the hello world example a line at a time to see how these rules are applied. On line 1 we see that we are declaring a class called Hello. As rule 1 says all Java code resides inside a class. Unlike Python where a program can simply be a bunch of statements in a file, Java programs must be inside a class. So, we define a class, Hello is not a very useful class it has no instance variables, and only one method. You will also notice the curly brace { In Java blocks of code are identified by pairs of curly braces. The block starts with a { and ends with a }. You will notice that I indented my code that followed the left brace, but in Java this is only done by convention it is not enforced.

On the next line we start our method definition. The name of this method is:

public static void main(String[] args)!

Everything on this line is significant, and helps in the identification of this method. For example the following lines look similar but are in fact treated by Java as completely different methods:

  • public void main(String[] args)
  • public static void main(String args)
  • public static void main()
  • void main(String args)

Just digging in to this one line will take us deep into the world of Java, so we are going to start digging but we are not going to dig too deeply right away. Much of what could be revealed by this one line is better understood through other examples, so be patient.

  • The first word, public indicates to the Java compiler that this is a method that anyone can call. We will see that Java enforces several levels of security on the methods we write, including public, protected, and private methods.
  • The next word, static tells Java that this is a method that is part of the class, but is not a method for any one instance of the class. The kind of methods we typically wrote in Python required an instance in order for the method to be called. With a static method, the object to the left of the . is a class, not an instance of the class. For example the way that we would call the main method directly is: Hello.main(parameter1). For now you can think of static methods the same way you think of methods in Python modules that don’t require an instance, for example the math module contains many methods: sin, cos, etc. You probably evaluated these methods using the names math.cos(90) or math.sin(60).
  • The next word, void tells the Java compiler that the method main will not return a value. This is roughly analogous to omitting the return statement in a Python method. In other words the method will run to completion and exit but will not return a value that you can use in an assignment statement. As we look at other examples we will see that every Java function must tell the compiler what kind of an object it will return. This is in keeping with the rule that says everything in Java must have a type. In this case we use the special type called void which means no type.
  • Next we have the proper name for the method: main. The rules for names in Java are similar to the rules in Python. Names can include letters, numbers, and the _. Names in Java must start with a letter.
  • Finally we have the parameter list for the method. In this example we have one parameter. The name of the parameter is args however, because everything in Java must have a type we also have to tell the compiler that the value of args is an array of strings. For the moment You can just think of an array as being the same thing as a list in Python. The practical benefit of declaring that the method main must accept one parameter and the parameter must be a an array of strings is that if you call main somewhere else in your code and and pass it an array of integers or even a single string, the compiler will flag it as an error.

That is a lot of new material to digest in only a single line of Java. Lets press on and look at the next line: System.out.println("Hello World!");. This line should look a bit more familiar to you. Python and Java both use the dot notation for finding names. In this example we start with System. System is a class. Within the system class we find the object named out. The out object is the standard output stream for this program. Having located the out object Java will now call the method named println(String s) on that object. The println method prints a string and adds a newline character at the end. Anywhere in Python that you used the print function you will use the System.out.println method in Java.

Now there is one more character on this line that is significant and that is the ; at the end. In Java the ; signifies the end of a statement. Unlike Python where statements are almost always only one line long java statements can spread across many lines. The compiler knows it has reached the end of a statement when it encounters a ;. This is a very important difference to remember. In Java the following statements are all legal and equivalent. I would not encourage you to write your code like this, but you should know that it is legal.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
System.out.println("Hello World");
System.out.println("Hello World")
;
System.out.println
    (
     "Hello World"
    )     ;
System.
  out.
    println("Hello World")
    ;

The last two lines of the hello world program simply close the two blocks. The first or outer block is the class definition. The second or inner block is the function definition.

If we wanted to translate the Java back to Python we would have something like the following class definition.

1
2
3
4
class Hello(object):
    @staticmethod
    def main(args):
        print "Hello World!"

Notice that we used the decorator @staticmethod to tell the Python interpreter that main is going to be a static method. The impact of this is that we don’t have to, indeed we should not, use self as the first parameter of the main method! Using this definition we can call the main method in a Python session like this:

>>> Hello.main("")
Hello World!
>>>

«  Preface   ::   Contents   ::   Basics  »