Intro To Java
Intro To Java
Java
l Application
é Standalone Java program that can run independent of any Web browser
l Applet
é Java program that runs within a Java-enabled Web browser
l Servlet
é Java software that is loaded into a Web server to provide additional server
functionality ala CGI programs
l Note that the name of the file is the name of the public class with
a .java extension added
l Compile: javac Hello.java
é Produces the class file Hello.class
l Run: java Hello
é Starts up the JVM
é Note that the .class extension is not specified
l Primitive Types
é byte (1 byte) -128 to +127
é short (2 bytes) -32,768 to 32,767
é int (4 bytes) -2.15E+9 to +2.15E+9
é long (8 bytes) -4.61E+18 to +4.61E+18
é float (4 bytes) 1.0E-38 to 1.0E+38
é double (8 bytes) -1.0E-308 to 1.0E+308
é char (2 bytes) 0 to 0xffff (Unicode)
é boolean (1 byte) true and false
é All numeric types are sign extended
l Operators
é Similar to C++
é Differences between C++ and Java Operators:
Ý + is used to concatenate strings
l Decision Constructs
é if-else (test expression must resolve into a boolean)
é switch (switch expression must resolve to an int, short, byte or char)
l Loops
é Same as C/C++
é while, do-while
é for
Design Patterns In Java
Introduction To Java Bob Tarr
10
Some Java OO Terminology
/**
* Class Point implements a geometric point.
* @author Bob Tarr
*/
public class Point {
/**
* Creates a new Point with coordinates 0,0.
*/
public Point() {
x = 0;
y = 0;
System.out.println("Point() constructor: " + this);
}
/**
* Creates a new Point with the specified coordinates.
* @param x The x coordinate.
* @param y The y coordinate.
*/
public Point(int x, int y) {
this.x = x;
this.y = y;
System.out.println("Point(int,int) constructor: " + this);
}
/**
* Returns the x coordinate.
* @return The x coordinate.
*/
public int getX() {return x;}
/**
* Returns the y coordinate.
* @return The y coordinate.
*/
public int getY() {return y;}
/**
* Sets the x coordinate.
* @param x The x coordinate.
*/
public void setX(int x) {this.x = x;}
/**
* Sets the y coordinate.
* @param y The y coordinate.
*/
public void setY(int y) {this.y = y;}
/**
* Converts a Point to a String.
* @return The Point as a String.
*/
public String toString() {
return ("[" + x + "," + y + "]");
}
l Creation:
data = new Point[10]; // Now the variable data refers to the
// array of 10 elements that can refer
// to a Point object. All the references
// in the array are null.
l Initialization:
data[0] = new Point(4, 5); // First array element initialized.
// It is now referring to the new
// Point object.
Superclass
Subclass
//Polymorphism
A a = new A();
B b = new B();
a.f(); // Invokes A's f()
b.f(); // Invokes B's f()
A a1 = b;
a1.f(); // Invokes B's f()
// Up Casting
A a2 = (A) b; // Ok
// Down Casting
//B b1 = a; // Illegal at compile time,
// explicit cast needed
//B b2 = (B) a; // Ok at compile time,
// exception at run time
// Other stuff
int i = a.aData; // Ok, same package
//i = a.bData; // Illegal at compile time,
// bData not defined in A
//a.g(); // Illegal at compile time,
// g() not found in A
}
protected double w;
protected double h;
l The import statement does not "read in" the class or "include" it;
it just saves typing:
import BT.Tools.Graphics.Point;
Point p = new Point();
l We've already seen that a class member can be modified with the
public, private or protected keywords
l If none of these modifiers are used, the member has the default
visibility or "package" visibility
l A package member is only accessible from within the class that
defines it or a class in the same package
l Here's the definitive chart!
MEMBER VISIBILITY
ACCESSIBLE TO: public protected package private
Same class Yes Yes Yes Yes
Class in same package Yes Yes Yes No
Subclass in different package Yes Yes No No
Non-subclass in different package Yes No No No
l Inner classes
é Member class
Ý A class defined as a member of another class
Ý Can not be declared static
Ý Can not have any static members
Ý Can access all members (even private) of its containing class
Ý Also useful for helper classes
é Local class
Ý Class defined inside a block of code
Ý Is visible only within the enclosing block
Ý Analogous to a local variable
Ý Can access all members (even private) of its enclosing class
Ý Similar in use to a member class, but can be put close to the location in the
code where it is actually used, thus improving readability
l Inner classes
é Anonymous class
Ý A local class which is defined and instantiated in one statement
Ý Does not have a name!
l Inner classes are frequently used to implement the event listener
objects required by the Abstract Window Toolkit (AWT) or
Swing Java Foundation Classes GUI components