23
Java Introduction 密密密密密密 密密密密密密密密密 密密密

Java Introduction 密碼學與應用 海洋大學資訊工程系 丁培毅. Download JDK

Embed Size (px)

Citation preview

Page 1: Java Introduction 密碼學與應用 海洋大學資訊工程系 丁培毅. Download JDK

Java Introduction

密碼學與應用海洋大學資訊工程系

丁培毅

Page 2: Java Introduction 密碼學與應用 海洋大學資訊工程系 丁培毅. Download JDK

Download JDKhttp://www.oracle.com/technetwork/java/javase/downloads/index.html

Page 3: Java Introduction 密碼學與應用 海洋大學資訊工程系 丁培毅. Download JDK

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/releas

e-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

Page 4: Java Introduction 密碼學與應用 海洋大學資訊工程系 丁培毅. Download JDK

Java ProgramsPrograms are built from classesClasses define data storages and behaviors of

objects (class instances)data structure of an object defined by data membersbehavior of an object are defined by methods that

manipulate the data members.

Page 5: Java Introduction 密碼學與應用 海洋大學資訊工程系 丁培毅. Download JDK

HelloWorld.javaclass HelloWorld {

public static void main(String[] arg)

{

System.out.println("Hello World");

}

}

Compiling: javac HelloWorld.java

Executing: java HelloWorld

Page 6: Java Introduction 密碼學與應用 海洋大學資訊工程系 丁培毅. Download JDK

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); } }}

Page 7: Java Introduction 密碼學與應用 海洋大學資訊工程系 丁培毅. Download JDK

CommentsThree forms

/* comment */

// comment to end of line

/** documentation comment for javadoc

tool */

all characters in Java programs are unicode (16 bit)

Page 8: Java Introduction 密碼學與應用 海洋大學資訊工程系 丁培毅. Download JDK

Constantsexamples

static final int face = Suit.DIAMONDS;

class Suit {

static final int CLUBS = 1;

static final int DIAMONDS = 2;

static final int HEARTS = 3;

static final int SPADES = 4;

}

Page 9: Java Introduction 密碼學與應用 海洋大學資訊工程系 丁培毅. Download JDK

Control Flow & Operatorscontrol 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++

Page 10: Java Introduction 密碼學與應用 海洋大學資訊工程系 丁培毅. Download JDK

Classes and ObjectsFields 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.

Page 11: Java Introduction 密碼學與應用 海洋大學資訊工程系 丁培毅. Download JDK

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_;}

}

Page 12: Java Introduction 密碼學與應用 海洋大學資訊工程系 丁培毅. Download JDK

Creating ObjectsObjects 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);

Page 13: Java Introduction 密碼學與應用 海洋大學資訊工程系 丁培毅. Download JDK

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.

Page 14: Java Introduction 密碼學與應用 海洋大學資訊工程系 丁培毅. Download JDK

Garbage CollectionWhen there are no active references to an object

storage is automatically reclaimed by a garbage collector thread running in the background.

Page 15: Java Introduction 密碼學與應用 海洋大學資訊工程系 丁培毅. Download JDK

Methods and Parametersmethods 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.

Page 16: Java Introduction 密碼學與應用 海洋大學資訊工程系 丁培毅. Download JDK

Invoking a methoda 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.

Page 17: Java Introduction 密碼學與應用 海洋大學資訊工程系 丁培毅. Download JDK

“this” the keyword “this” refers to the current receiving

object. public void clear() {

this.x = 0.0; y = 0.0; // this is assumed }

Page 18: Java Introduction 密碼學與應用 海洋大學資訊工程系 丁培毅. Download JDK

ArraysAn array is a collection of variables of the same typ

eCard [] deck = new Card[52];

for (int i = 0; i < deck.length; i++) { deck[i] = . . .

Page 19: Java Introduction 密碼學與應用 海洋大學資訊工程系 丁培毅. Download JDK

StringsString 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 obj

ects. myName.length()

Page 20: Java Introduction 密碼學與應用 海洋大學資訊工程系 丁培毅. Download JDK

InheritanceClasses may extend other classes. variables of a subclass with the same name as thos

e of a superclass shadow the superclass storage.a method of a subclass with the same signature as t

hat of a superclass overrides the superclass method.objects of a subclass may be used in superclass vari

ables.

Page 21: Java Introduction 密碼學與應用 海洋大學資訊工程系 丁培毅. Download JDK

Inheritance if a method is overridden, the keyword “super” refe

rs 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.

Page 22: Java Introduction 密碼學與應用 海洋大學資訊工程系 丁培毅. Download JDK

ExceptionsThe 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.

Page 23: Java Introduction 密碼學與應用 海洋大學資訊工程系 丁培毅. Download JDK

Packagespackages are Java’s way to manage name spaces. Packages are implemented as directories within a fi

le 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.