January 27, 1999 CS536

Notes by Ho-Seop Kim
 

  • Java can be thought as "C+++" : it throws away bads and inherits goods; more straightforward than C++
  • Primitive data types: int, short, long, char, byte, float, double, boolean; everything else is a class object
  • "There's no pointer in Java" <-> "Everything in Java is a pointer"
  • First declare an object, then create it
  • Simple example
  • If no user-defined constructor is given, Java will run default constructor; it sets fields to some default values (false for boolean, 0 for int etc.)

  •  
  • All dynamically allocated memory spaces are automatically garbage-collected when it is not referenced any more

  •  
  • Classes have
  • Members
       Members are
  • Visibility: public, private, package (default; similar to public), protected

  •  
  • Static vs instance
  • point p = new point();
    point q = new point(1, 2);

           +-----+        point
    p ---> |x = 0|       +-----------+
           |y = 0|       |howmany = 2|
           +-----+       +-----------+

           +-----+
    q ---> |x = 1|
           |y = 2|
           +-----+

       Sometimes we need variable generic to class. But there's no global variable in Java. All data should be in a class. With public static int howmany = 0; we can use point.howmany.
     
  • Static function: You don't need to create class instance to use it. For example, Math.sin(). Static function should not use any non-static member.

  •  
  • Simple code in C++ vs Java
  • To compile and run a Java program in CS environment (JDK),
  • Some useful methods inherited from Object class