Java traps for C++ programmers.

1. All class objects are on the heap. Access is only via pointers.

    Player p ; // p is a pointer to a Player.
    p = new Player(0) ; // Allocate a new object
    Player q ; // q is another pointer
    q = p ;    // p and q point to same object

2. Arrays are objects themselves and must be allocated

    int[] a = new int[10] ;

Now a points to an array of 10 unititialized integers.

3. Arrays of objects really contain pointers

    Player[] a = new Player[10] ;

Now a points to an array of 10 pointers to Players (initially null).

    for(int i=0 ; i<10 ; ++i) {
        a[i] = new Player(i) ; }

Now a points to an array of 10 pointers to different Player objects.

4. Passing, assigning, and returning objects is by pointer, not by value.

    Vect scale( Vect v, double f )
    {    v.x = f * v.x ;
         v.y = f * v.y ;
         f = 0.0 ;  // As in C, this has no effect
         return v ;
    }
    ...
    q = scale( p, 2.0 ) ;
    // p and q point to the same object.
    // The original vector value is lost.

5. No reference parameters.

But since arrays are objects, you can sort of fake them.

    void halve( int[] ref ) { ref[0] = ref[0] / 2 ; }
    ...
    int [] x = new int[1] ;
    x[0] = 10 ;
    halve( x ) ;

6. All functions are "virtual".

    public class Parent {
       public int example() { return 0 ; }
    }
    public class Child extends Parent {
        public int example() { return 1 ; }
    }
    ...
    Parent p = new Child() ;
    System.out.println( "" + p.example() ) ;

7. No accessibility attribute means it is available only in the package.

    public class Foo 
    // Don't forget to declare public classes "public"                
    {
      public    int   a ; // Anyone may use a
      protected int   b ; // All derived classes may use b
                int   c ; // Only classes in same package
      private   int   d ; // Only within this class
    }