Authentication
Download
Report
Transcript Authentication
Java Introduction
密碼學與應用
海洋大學資訊工程系
丁培毅
Download JDK
http://www.oracle.com/technetwork/java/javase/downloads/index.html
Java Program Development IDEs
Eclipse, http://www.eclipse.org/downloads/
Eclipse IDE for Java Developers, 151 MB
Apache Ant, http://ant.apache.org/bindownload.cgi
Apache Maven,
http://maven.apache.org/docs/3.1.1/release-notes.html
Or simply notepad++ and cmd.exe (javac and java)
jdk7u40.bat
or modify 系統內容/進階/環境變數
@echo off
set path=%path%;c:\progra~1\java\jdk1.7.0_40\bin
set classpath=.;c:\progra~1\Java\jdk1.7.0_40\src.zip
@echo on
Java Programs
Programs
are built from classes
Classes define data storages and behaviors of
objects (class instances)
data
structure of an object defined by data members
behavior of an object are defined by methods that
manipulate the data members.
HelloWorld.java
class HelloWorld {
public static void main(String[] arg)
{
System.out.println("Hello World");
}
}
Compiling: javac HelloWorld.java
Executing: java HelloWorld
Factorial.java
factorial(21) overflows a 64-bit integer (long)
java.math.BigInteger supports arbitrarily long integers
(java.math.BigDecimal supports arbitrarily long real
numbers)
import java.math.BigInteger;
class BigFactorial {
public static void main(String[] args) {
BigInteger fact = new BigInteger("1");
for (int i=1; i<60; i++) {
fact = fact.multiply(BigInteger.valueOf(i));
System.out.println(fact);
}
}
}
Comments
Three
forms
/* comment */
// comment to end of line
/** documentation comment for javadoc
tool */
all
characters in Java programs are unicode (16 bit)
Constants
examples
static final int face = Suit.DIAMONDS;
class Suit
static
static
static
static
}
{
final
final
final
final
int
int
int
int
CLUBS = 1;
DIAMONDS = 2;
HEARTS = 3;
SPADES = 4;
Control Flow & Operators
control statements
mostly cloned from C++
differences:
boolean
type requires operands of &&, || etc. to be
boolean
while(1) not OK…while(true) is fine
+ is string concatenation
I/O is not like C++
arrays are not like C++
Classes and Objects
Fields
are data variables associated with a class or
with instances of the class
primitive data (int, double, …)
object data ( BigInteger r )
Methods provide execution behavior and operate
on the data fields
Classes and Interfaces can be members of other
Classes or Interfaces.
Interfaces
Interfaces
define the methods that may be used but
do not specify instance storage.
interface ConstAccount {
double getBalance();
}
class Account implements ConstAccount {
double d_ = 0.0;
void setBalance(double d) {d_ = d;}
double getBalance() { return d_;}
}
Creating Objects
Objects
are primitive or user defined.
Primitive objects are by value, user defined objects
are references.
User defined Objects are created with constructors
and the “new” keyword.
Point center = new Point(4.0,5.9);
Static fields
static
fields in a class or interface belong to the
class. If there are instances of a class with static
data fields then all instances share the static data
fields
static methods are invoked using the class name.
Garbage Collection
When
there are no active references to an object
storage is automatically reclaimed by a garbage
collector thread running in the background.
Methods and Parameters
methods
operate on the data fields of a class.
methods have zero or more parameters and may
return values or be void.
a methods name, number of parameters and their
type make up the signature of the method.
two methods may share a name if they have
different signatures.
Invoking a method
a
non-static method is invoked using an object of
the class and the “dot” operator.
Point p = new Point(3.2,3.3);
p.clear();
the object on which the method is invoked is the
“receiver”. The method is a “message” to the
object.
“this”
the
keyword “this” refers to the current receiving
object.
public void clear() {
this.x = 0.0;
y = 0.0; // this is assumed
}
Arrays
An
array is a collection of variables of the same
type
Card [] deck = new Card[52];
for (int i = 0; i < deck.length; i++)
{ deck[i] = . . .
Strings
String
objects are immutable objects.
String myName = “W. H. Carlisle”;
System.out.println( myName + “ III”);
Use stringBuffer objects if you wish to manipulate
the storage.
there are methods that can be invoked on string
objects.
myName.length()
Inheritance
Classes
may extend other classes.
variables of a subclass with the same name as
those of a superclass shadow the superclass storage.
a method of a subclass with the same signature as
that of a superclass overrides the superclass method.
objects of a subclass may be used in superclass
variables.
Inheritance
if
a method is overridden, the keyword “super”
refers to the superclass.
Classes may extend only one class. If a class does
not extend a class by default it extends the class
Object.
A class may implement many interfaces.
Exceptions
The
exception mechanism of Java is very similar
to that of C++
try { } catch( Exception e) { }
if a method throws a “checked” exception the
compiler requires that a handler catch the
exception or that the invoking method also
indicates that it also throws the exception.
a “finally” block may follow “catches” to close
files, etc.
Packages
packages
are Java’s way to manage name spaces.
Packages are implemented as directories within a
file system. When package names must span
systems, the common practice is to reverse the
internet domain name
import COM.Sun.games;
import is not inclusion and is only for the compiler.
Class paths are used to find the class information at
run time.