You are on page 1of 39

java

Java 45

8
21 Ovum
35

Java

Java

www.java.com

Java.class

Java

Windows

Java

Unix

Java

Java

Java

Java
Java
Class
Java API
Java

JavaJavaJavaAPIJDK
Java Development Kit;
Java APIJava SE APIJava
JREJava Runtime Environment,JRE Java

Java(Java Virtual Machine)


Java

/**
* HelloWorldApp.java
*/
import edu.pku.*;
public class HelloWorldApp{
public static void main (String args[]) {
System.out.println ("Hello World!");
}
}

Javaclass
Java
HelloJava(HelloJava.java)
HelloJava
publicJava
(Public)

Java{}

HelloJavamain()
main()

main()Java

main()public(Member)

main()(Object)
staticpublicstaticJava(ObjectOriented)
main()voidmain()Java
main()voidmain()
String[] args
(Command Line Argument)

main()
System.out.println(" Hello Java ");

java.langSystemPublic
out (out PrintStream )PrintStream
println()(String

Java""println()
print()

(class)
(package)
JDKJava: java.lang, java.io, java.awt , java.net ,
java.util

myapp.school

Student

myapp.store

Teacher

Customer

Seller

package
import

public

---B.java----------package b; // package
import a .*; // importimport a.A;
public class B{ //
public static void main(String args[]){
A a; //access class A
a=new A();
}
}

JDK API1200
javajavaxorg
javaJDK
javaxJDK(xextend)
org(orgorganization
)
JDK APIcom.sunSUN
JDK
JDK API

java.lang

java.langJava(langlanguage())
Java
Java
JavaJava

java.util

java.utilJava
Java

Java
.java

public
(.java)

public

public
public

public

:Which

of

the

a)import java.awt.*;
package mypackage;
class Myclass {}
b)package myPackage;
import java.awt.*;
class MyClass{}
c) /*This is a comment */
package myPackage;
import java.awt.*;
public class MyClass{}

following

will

compile

without

error

JDK:
http://java.sun.com/javase/downloads/index.jsp

JDK Java Development KitJavaJava

JDK
JavaJavaJava

JDKJavajava.langjava.iojava.util
java.awtjavax.swingjava.sqlJDK
javac.exe
java.exejavadoc.exeJavaDocjar.exe

JDK
JDK<JAVA_HOME><JAVA_HOME>\bin

javac.exe JavaJavaJava
jar.exe : Java
java.exe : Java
javadoc.exe JavaDoc
DOS<JAVA_HOME>/bin
PATH

java XXX
java.exeJREXXX
java.exeJRE:
1. JRE2
2. JRE3
3. Windows Registry
(HKEY_LOCAL_MACHINE\ Software\JavaSoft\ Java
Runtime Environment)

classpath
java.exe javac.exe.class
(.java)

classpath

classpath ( -cp)
CLASSPATH
set CLASSPATH=C:\;.

-classpath CLASSPATH

CLASSPATH -classpath

B.java C:\edu\pku C
\my2\edu\pku
Javac classpath C:\ A.java
c:\edu\pkuB.java

Javac classpath .; C:\ A.java


C\my2\edu\pkuBJava

Java

String

new


//comment on one line
/* comment on one or more line */
/** documenting comment */

Javaabstractbooleanbreakbytecase
catchcharclasscontinuedefaultdodoubleelse
extendsfalsefinalfinallyfloatforifimplements
importinstanceofintinterfacelongnativenewnull
packageprivateprotectedpublicreturnshortstatic
super switch synchronized this throw throws
transienttruetryvoidvolatilewhile
Javaconstgoto

Java (sizeof,
friendly,NULL,String)

($123, _param1 )
a-zA-Z$_
a-zA-Z$_

Which of the following identifiers are ILLEGAL?


a) _underscore
b) 5Interstate
c) Interstate5
d) _5_

boolean
char
byte, short, int, long
double, float

boolean isMarried=true;
int age=18;
char grade=A;
double price=44.13;


7:
int(1, 1567)
long(22L,22l)
float(234.5F,1.3f)
double(234.5 , 235.6D,11.0d)
boolean(true, false)
char(a, \u000F)
String(hello)

int score=99;
long length=100L;
float weight=14.12F;
char sex=M;
String name=Tom;

16Unicode

Student tom;
tom=new Student();
int[] intArray=new int[3];
int age=18;

Sample

public class Sample{


S
memberV1(10)
memberV2(42)
byte memberV1;
int memberV2;
public Sample(){ memberV2=2;} //
public static void main(String args[]){Sample s=new Sample();}
}

1.
int[] intArray
int intArray[]
2.
intArray=new int[3];

int intArray[3]; //

new

int

intArray

3
for(int i=0;i<3;i++){
intArray[i]i
}

int[0](40)
int[1](40)
int[2](40)

int intArray[]= new int[] {0,1,3};


int intArray[]= {0,1,3};

int[] intArray=new int[]{0,1,2};

int[] intArray= {0,1,2};

0 length-1
length

1What will happen if you try to compile and run the following code?
public class Q {
public static void main(String argv[]){
int a[]=new int[]{1,2,3};
System.out.println(a[1]);
System.out.println(a[a.length-1]);
}
}


Java

public class Sample{


int v1;
boolean v2;

Sample v3;
long[] v4=new long[3];
String[] v5=new String[3];
public static void main(String args[]){

(0)
(\u0000)
(false)
(null)
(null)

Sample sam=new Sample();


System.out.println(sam.v1);
System.out.println(sam.v2);
System.out.println(sam.v3);
System.out.println(sam.v4[0]);
System.out.println(sam.v5[0]);

}
}

Java

if, switch,for,whiledo
breakcontinue

{}

class Scope{
int x; //
int y; //
Scope(int x){
int y=2;
int z=3;
this.x=x; //this.xx
this.y=y; //this.yy
x=0;
y=0;
}
public static void main(String args[]){
Scope s=new Scope(1);
System.out.println(s.x);
System.out.println(s.y);
System.out.println(s.z); //
}

class Sample{

int v1;
boolean v2;
public static void main(String
args[]){
Sample s=new Sample();
System.out.println(s.v1);
System.out.println(s.v2);
}
}

public void doComputation() {


int x = (int)(Math.random() * 100);
int y;
int z;
if (x > 50) {
y = 9;
}
z = y + x; // Possible use
initialization
}

before