- The java command compiles a .java file into a .class file as
java NameOfClass.java
- The javac file starts the interpreter. The syntax is
javac NameOfClass
Once we have created the .class files by using the javac compile
the interpreter only
needs the .class files (not the .java files).
- Java is a purely object-oriented language (i.e. everything is defined
within a class
definition.
- In order to run an application (as opposed to an applet on a web
page) we must define
the method
public static void main( String args[] )
in the class definition.
- System.out.print is used to print strings. System.out.println is
the same except it
automatically puts the return character ('\n') after
the string.
- The library or rather as it is called in Java, the package
java.lang is automatically
imported into every Java class. Thats why the classes String, Object,
Math System and many
more are defined in every class.
- Java only reads and prints Strings. We use the static methods
String.toString( variable or value ) to convert to a String
- Variables have a scope, an access, and a duration
- We have the three access modifiers: public, protected, and private
- The scope of a variable (or reference) follows the same rules
as in C++. A variable
(or reference) is local to the curly brackets in which it is declared
- Instance variables have class scope and are declared outside of
any curly
brackets except those of the class itself. Each instantiation
of a class object
has its own copy of the instance variables
- Class variables have the same scope but are static
- Of course anything declared within a method is local to the method
- Due to the way the Java compiler resolves names (i.e. determines
what a particular
name refers to) local variables with the same name as an instance
variable, hide the
instance variable within their local scope.
- A similar thing happens in the inheritance mechanism. A subclass
instance variable,
with the same name as one of the superclass' instance variables,
hides the superclass
variable within the scope of the subclass.
- The signature of a method is everything in the "function header"
(I am actually
using C++ terminology when I say function header) except
the return type.
- Defining two or more methods with the same name but different
signatures within
the same class is called overloading (just as in C++)
- The keyword extends allows one class to inherit from another
- In order for a subclass method to override a superclass method
the two method's
signatures must agree exactly and the return type must be the same.
In this case one
can get dynamic binding.
- If a method in the subclass has the same name as a method in the
superclass but
the signatures differ then the method in the subclass will hide
(just like with
instance variables) the method in the superclass
when we are in the scope of the subclass.
- final methods cannot be overridden
- final classes cannot be inherited from
- An abstract class cannot be instantiated (there can however be
references of an
abstract class type)
- A concrete class that inherits from an abstract class must
define all the
abstract methods inherited
- If an abstract class inherits from another abstract class then
the inherited
abstract methods do not have to be defined
- static methods can use only local and static variables
- Every array has an instance variable (called length) that stores
the length of
the string
- declaring and array of primitive type allocates the primitive
variables
double a[]=new double[10]; //10 primitive variables
- declaring an array of a class type simply declares references
Square a[] = new Square[10];//10 references
a[0] = new Square();//if an object is desired, it
//must be instantiated as a
//second step
- A superclass reference can refer to a subclass object. We can
also assign a
subclass reference to a superclass reference. In this case an
implicit reference
conversion is made
Shape a;
Square b = new Square();
a = b; //an implicit conversion happens,
//a is assigned a Shape reference that
//refers to the same object as b
- A superclass reference CANNOT be assigned to a subclass reference.
We can however call
a cast method to return a subclass reference
Shape a = new Square();
Square b;
b = ( (Square) a );//right side returns a
//Square reference referring to
//the same object as a
- ANY reference whatsoever can only call a method whose signature
appears in the
class definition of the reference's class.
- The this reference is the same as in C++ (except it is a reference
not a pointer)
- Know the StringTokenizer class