A Quick Intro to Java for C and C++ Programmers
(Thanks to Scott Hudson for providing this)

Will be assuming you are a fluent C or C++ programmer.
This is going to be a quick introduction to the language.
I'm not going to teach anything much about object oriented programming.

nxt

 

 

 

 

What is Java

nxt

 

 

 

 

Why the hype?

nxt

 

 

 

 

 

Beyond the Hype

nxt

 

 

 

Goals

nxt

 

 

 

 

Major differences from C++

nxt

 

 

 

 

What's been added from C (all found in C++)

nxt

 

 

 

 

What's been added beyond C++

nxt

 

 

 

 

Things that are missing (but we probably won't miss):

nxt

 

 

 

 

Other things that are missing (that we might miss):

nxt

 

 

 

 

So is it too slow?

nxt

 

 

 

 

More details on differences with C++...

Lexical level

Syntactic level Flow control  

 

 

 

Classes

class point_list {
  int x;
  int y;
  point_list next;
  double distance_to(point_list other)
    {
      double dx, dy;
 
      dx = (double)(x - other.x);
      dy = (double)(y - other.y);
      return Math.sqrt(dx*dx + dy*dy);
  }
  double distance_to_next()
    {
      if (next == null)
        return 0;
      else
        return distance_to(next);
    }
}
What have we got here. Except... we have some problems here what are they?

1p 2v 3c nxt

 

 

 

 

 

 

 

Problem #1

It looks like we just declared a point_list to be part of a point_list...

 
Primitive types are:
byte 8 bit signed integer
short 16 bit signed integer
int 32 bit signed integer
long 64 bit signed integer
boolean true or false
char 16 bit character (Unicode)
float single precision floating point
double double precision floating point
note: exact definition of these is not left up to the implementation!
  back

 

 

Problem #2 Visibility

Classes also provide information hiding and the default is to not allow full access to instance variables or methods. So a better version would be:

public class point_list {
  protected int x;
  protected int y;
  protected point_list next;
  public double distance_to(point other)
    {
      double dx, dy;
 
      dx = (double)(x - other.x);
      dy = (double)(y - other.y);
      return Math.sqrt(dx*dx + dy*dy);
  }
  public double distance_to_next()
    {
      if (next == null)
        return 0;
      else
        return distance_to(next);
    }
}
nxt

 

 

 

 

Specifying protection

Members can be:
public Accessible to all
protected Accessible to classes within the same package and subclasses.
private Accessible only within the class itself
<unlabeled> Inside package: protected.

Outside package: private (no subclass access outside package).

Almost all classes are public, but can also be left unlabeled to create a "local" or "private" class (typically you do not want to do this).

nxt

 

 

 

 

Point of style

  protected int _x;
  protected int _y;
  public int x() {return _x;}
  public void set_x(int val) {_x = val;}
back

 

 

 

Problem #3: No constructor

 
int, et. al 0
boolean false
float, double 0.0
char '\0'
Object reference null
 
  protected int x = 0;
  protected int y = 0;
  protected point_list next = null;
 nxt
 

 

 

 

Declaring constructors

  public point_list(int xv, int yv, point_list nxt)
    {
      x = xv; y = yv; next = nxt;
    }
  public point_list(int xv, int yv)
    {
      this(xv,yv, null);
    }  
  public point_list()
    {
      this(0,0);
    }
back

 

 

 

Some misc basics

nxt

 

 

 

 

Hierarchical Types and Inheritance

nxt

 

 

 

 

Inheritance

nxt

 

 

 

 

Declaring inheritance in class declarations

public class colored_point_list extends point_list {
  protected Color _pt_color;
  public Color pt_color() {return _pt_color;}
  public void set_pt_color(Color val) {_pt_color = val;}
  public colored_point_list(
    int xv, int yv, point_list nxt, Color clr)
    {
      super(xv,yv,nxt);
      set_color(clr);
    }
}
nxt

 

 

 

 

Super

public class grid8_point_list extends point_list {
  public void set_x(int val) 
    {
      int rounded = (val / 8)*8 + ((val%8 >= 4)?1:0);
      super.set_x(rounded);
    }
  ...
}
nxt

 

 

 

 

This

nxt

 

 

 

 

Abstract classes

    abstract public class stack {
      abstract public void push(Object obj);
      abstract public Object pop();
      abstract public boolean empty();
    }
nxt

 

 

 

 

Interfaces

nxt

 

 

 

 

Substitutability for interfaces

nxt

 

 

 

 

Packages

      package sub_arctic.lib;
      java.awt  and java.awt.image
nxt

 

 

 

 

 

Imports

      import java.awt.Color;
      import java.awt.*;
nxt

 

 

 

 

 

 

 

Exceptions: (skip ahead)

      throw <exception-expr>;
      throw new exception("error message");
      try {
        ...main code...
      } catch (Exception ex) {
        ...recovery body   ... 
      }
nxt

 

 

 

 

 

 

 

Exceptions Cont.

      try {
        ...main code...
      } catch (ex_type ex) {
        ...recovery body   ... 
      } catch (ex_type2 ex2) {
        ...recovery body 2 ...
      } finally {
        ... always executed ... 
      }
nxt

 

 

 

 

Declaring Exceptions

      java.lang.Throwable
        java.lang.Error
        java.lang.Exception
          java.lang.RuntimeError
      pubic void adjust() throws monkey_wrench { ... }
nxt

 

 

 

 

Static members (skip ahead)

        class a_class {
          protected static int count = 0;
          public a_class() {count++;}
        }
      Math.sqrt(3.1415d)
nxt

 

 

 

 

Initialization of statics

      static {
        ... some code ...
      }
nxt

 

 

 

 

Final members

      public static final double PI2 = Math.PI*Math.PI;
nxt

 

 

 

 

Strings

nxt

 

 

 

 

Arrays

      int[] foo = new int[52];
      ...
      foo = new int[42]
      foo.length == 42
      init[] foo = {3, 5, 9};
      Object[] bar = {"one", new Stack(), new Integer()};
nxt

 

 

 

 

Array types

Arrays are hierarchically typed

If sub is a subclass of base then sub[] is a subclass of base[]

So you could have:

base[] base_var = new sub[42];

nxt

 

 

 

 

Checking and converting types at runtime

Does runtime type check and throws an exception if this is not a legal conversion (i.e., object being cast is not of that type or a subclass thereof).

nxt

 

 

 

 

Output to stdout

 

nxt

 

 

 

 

Threads

 

nxt

 

 

 

 

Using a Java System, some non-obvious things...

Organizing Java source code so it can be found by the tools

nxt

 

 

 

 

Packages

nxt

 

 

 

 

 

 

CLASSPATH

  ~hudson/java_src
    java_cup
      ...
    sub_arctic
      lib
        interactor.class 
        interactor.java
        ...
      input
        ...
      output
        ...
  setenv CLASSPATH ~hudson/java_src  
    -or-
  export CLASSPATH=~hudson/java_src
nxt

 

 

 

 

Running Java programs

  public static void main(String[] args)
nxt

 

 

 

 

Applets

  <APPLET CODE="sub_arctic.test.my_test.class" 
          WIDTH=400 HEIGHT=200 CODEBASE="../..">
    <PARAM NAME="option1" VALUE=5>
    <PARAM NAME="option1" VALUE=5>
    Alternate HTML for non-Java browsers
  </APPLET>
nxt

 

 

 

 

Invoking the compiler (assuming JDK)

In the directory with the source do:

  javac my_source.java
Compiler basically does a "make" -- it will compile anything which is needed and out of date.

nxt

 

 

 

 

To run a standalone program

(in class in a package in your CLASSPATH) do:

  java my_package.myclass
Nifty feature:
  java -cs my_package.myclass
will compile everything it needs first.

nxt

 

 

 

 

To run an applet

  appletviewer my_page.html
nxt
 

 

 

 

 

Good luck...

... and, to quote from Scott: "as always, if you or any of your programming team are caught or killed, I will disavow that I ever taught you anything about Java."