Thursday, September 4, 2008

Java fundamentals

I covered a few things about Java and recommend two sources about Java for C++ programmers:

I mentioned that my most common error coming to Java from C++ is in declaring variables:

TrafficLight tLight;

In both languages, this declares a variable named tLight. In C++, the variable is of type TrafficLight and the instance is initialized by a constructor. In Java, the variable is of type reference to TrafficLight and is initialized to null. To create an actual object, use

TrafficLight tLight = new TraffcLight();

Some other points I made:

  • One public class is defined in a file. The class Foo must be defined in file foo.java.
  • Classes are organized into packages. All the classes in a package must be in a directory that has the name of the package.
  • A Java source file is compiled using javac, the Java compiler. The output of the compiler is a class file—e.g., Foo.class.
  • A Java program is run using the Java Virtual Machine (JVM) via the java command. The class method having the signature public static void main(String args[]); is invoked as the starting point of a program.
  • A Java program is usually a collection of packages and classes that are stored together in a Java archive (.jar) file. [In fact, Netbeans stores the classes for each project in a separate .jar file.]

We went to the lab and I tried, anyway, to introduce you to regular expressions. We had trouble using the NetBeans edtor, but it worked in vim. Regular-Expressions.info seems chock full of info about regular expressions.

No comments: