java 程序设计 java programming fall, 2013. 2 contents for today java program structure how to...
TRANSCRIPT
![Page 1: Java 程序设计 Java Programming Fall, 2013. 2 Contents for Today Java Program Structure How to Compile a Java Program How to Run a Java Program Environment](https://reader030.vdocuments.pub/reader030/viewer/2022013115/56649e215503460f94b0d658/html5/thumbnails/1.jpg)
Java Java 程序设计程序设计Java Programming
Fall, 2013
![Page 2: Java 程序设计 Java Programming Fall, 2013. 2 Contents for Today Java Program Structure How to Compile a Java Program How to Run a Java Program Environment](https://reader030.vdocuments.pub/reader030/viewer/2022013115/56649e215503460f94b0d658/html5/thumbnails/2.jpg)
2
Contents for Today
Java Program Structure How to Compile a Java Program How to Run a Java Program
Environment Variables Java Commands
![Page 3: Java 程序设计 Java Programming Fall, 2013. 2 Contents for Today Java Program Structure How to Compile a Java Program How to Run a Java Program Environment](https://reader030.vdocuments.pub/reader030/viewer/2022013115/56649e215503460f94b0d658/html5/thumbnails/3.jpg)
3
Example 2.1:
/*****First Java Program: HelloWorld.java *****///* Author: Mary
public class HelloWorld{ public static void main( String[ ] args ) {
System.out.println( "Hello, world!" ); }}
“main” method
![Page 4: Java 程序设计 Java Programming Fall, 2013. 2 Contents for Today Java Program Structure How to Compile a Java Program How to Run a Java Program Environment](https://reader030.vdocuments.pub/reader030/viewer/2022013115/56649e215503460f94b0d658/html5/thumbnails/4.jpg)
4
Create a Java Source File
![Page 5: Java 程序设计 Java Programming Fall, 2013. 2 Contents for Today Java Program Structure How to Compile a Java Program How to Run a Java Program Environment](https://reader030.vdocuments.pub/reader030/viewer/2022013115/56649e215503460f94b0d658/html5/thumbnails/5.jpg)
5
Create(创建 ) a Java Source File
Step 1: Start the Notepad editor and type in the source
code; Save the source code in a directory the name of
HelloWorld.java
Java源程序后缀名
![Page 6: Java 程序设计 Java Programming Fall, 2013. 2 Contents for Today Java Program Structure How to Compile a Java Program How to Run a Java Program Environment](https://reader030.vdocuments.pub/reader030/viewer/2022013115/56649e215503460f94b0d658/html5/thumbnails/6.jpg)
6
Compile the Source Code
![Page 7: Java 程序设计 Java Programming Fall, 2013. 2 Contents for Today Java Program Structure How to Compile a Java Program How to Run a Java Program Environment](https://reader030.vdocuments.pub/reader030/viewer/2022013115/56649e215503460f94b0d658/html5/thumbnails/7.jpg)
7
Compile(编译 ) the Source Code
Step 2: Compile the Source File into a .class File
![Page 8: Java 程序设计 Java Programming Fall, 2013. 2 Contents for Today Java Program Structure How to Compile a Java Program How to Run a Java Program Environment](https://reader030.vdocuments.pub/reader030/viewer/2022013115/56649e215503460f94b0d658/html5/thumbnails/8.jpg)
8
Run the Program
![Page 9: Java 程序设计 Java Programming Fall, 2013. 2 Contents for Today Java Program Structure How to Compile a Java Program How to Run a Java Program Environment](https://reader030.vdocuments.pub/reader030/viewer/2022013115/56649e215503460f94b0d658/html5/thumbnails/9.jpg)
9
Run(运行 ) the Program
Step 3: Run the program
![Page 10: Java 程序设计 Java Programming Fall, 2013. 2 Contents for Today Java Program Structure How to Compile a Java Program How to Run a Java Program Environment](https://reader030.vdocuments.pub/reader030/viewer/2022013115/56649e215503460f94b0d658/html5/thumbnails/10.jpg)
10
Java Environment Variables(环境变量)
JAVA_HOME: The path in which JDK is installed provide general information to applications which
need to use Java commands and JVM; JAVA_HOME = C:\j2sdk1.4.2_08
![Page 11: Java 程序设计 Java Programming Fall, 2013. 2 Contents for Today Java Program Structure How to Compile a Java Program How to Run a Java Program Environment](https://reader030.vdocuments.pub/reader030/viewer/2022013115/56649e215503460f94b0d658/html5/thumbnails/11.jpg)
11
Java Environment Variables
Path: used by Operating System to search the
command to execute; PATH =…;C:\j2sdk1.4.2_08\bin
![Page 12: Java 程序设计 Java Programming Fall, 2013. 2 Contents for Today Java Program Structure How to Compile a Java Program How to Run a Java Program Environment](https://reader030.vdocuments.pub/reader030/viewer/2022013115/56649e215503460f94b0d658/html5/thumbnails/12.jpg)
12
Java Environment Variables
CLASSPATH:
CLASSPATH = .; C:\j2sdk1.4.2_02\lib
‘.’ — current path; to tell JVM the path to find the class library.
![Page 13: Java 程序设计 Java Programming Fall, 2013. 2 Contents for Today Java Program Structure How to Compile a Java Program How to Run a Java Program Environment](https://reader030.vdocuments.pub/reader030/viewer/2022013115/56649e215503460f94b0d658/html5/thumbnails/13.jpg)
13
Java Commands
javac - the Java compiler java - the Java bytecode interpreter (JVM) jdb - the Java debugger javadoc – a documentation generator that
lets you generate documentation from your Java source code and the Javadoc comments you place in your source code
jar - Java archive utility
![Page 14: Java 程序设计 Java Programming Fall, 2013. 2 Contents for Today Java Program Structure How to Compile a Java Program How to Run a Java Program Environment](https://reader030.vdocuments.pub/reader030/viewer/2022013115/56649e215503460f94b0d658/html5/thumbnails/14.jpg)
14
Java Program Structure
What is a Class( 类 )? A class is a prototype that defines the variables and the
methods common to all objects of a certain kind. What is an Object(对象 )?
Software objects are often used to model real-world objects in everyday life.
An object is a software bundle of related variables and methods.
An object is an instance(实例 ) of some class.
![Page 15: Java 程序设计 Java Programming Fall, 2013. 2 Contents for Today Java Program Structure How to Compile a Java Program How to Run a Java Program Environment](https://reader030.vdocuments.pub/reader030/viewer/2022013115/56649e215503460f94b0d658/html5/thumbnails/15.jpg)
15
Example 2.1:
/***** HelloWorld.java *****///* Author: Mary
public class HelloWorld{ public static void main( String[ ] args ) {
System.out.println( "Hello, world!" ); }}
![Page 16: Java 程序设计 Java Programming Fall, 2013. 2 Contents for Today Java Program Structure How to Compile a Java Program How to Run a Java Program Environment](https://reader030.vdocuments.pub/reader030/viewer/2022013115/56649e215503460f94b0d658/html5/thumbnails/16.jpg)
16
Java Program Structure
Comments(注释) Anything between /* and */
or following // on a single line Programmers use comments in their code to provide
information to the reader on what the code is doing. When a program is compiled, the compiler skips all
comments. It is common (and good) practice to use a comment at
the top of the code containing general information about the file (known as a header).
![Page 17: Java 程序设计 Java Programming Fall, 2013. 2 Contents for Today Java Program Structure How to Compile a Java Program How to Run a Java Program Environment](https://reader030.vdocuments.pub/reader030/viewer/2022013115/56649e215503460f94b0d658/html5/thumbnails/17.jpg)
17
Java Program Structure Class
All Java code must be part of a “class”. Every class has a name:
public class HelloWorld{……}
Braces { and } are used to mark the start and end of the class.
Other class information may also appear with the name: A “public” class is one that can be referred to by
other classes.
class namemodifie
r
![Page 18: Java 程序设计 Java Programming Fall, 2013. 2 Contents for Today Java Program Structure How to Compile a Java Program How to Run a Java Program Environment](https://reader030.vdocuments.pub/reader030/viewer/2022013115/56649e215503460f94b0d658/html5/thumbnails/18.jpg)
18
Components of Classes
Inside a class, there can be fields( 域 ): store values of some information methods(方法 ): a collection of statements
that performs a sequence of operations on the variables.
In our first program, we have zero attributes( 域 /属性 ) and one method.
![Page 19: Java 程序设计 Java Programming Fall, 2013. 2 Contents for Today Java Program Structure How to Compile a Java Program How to Run a Java Program Environment](https://reader030.vdocuments.pub/reader030/viewer/2022013115/56649e215503460f94b0d658/html5/thumbnails/19.jpg)
19
Example 2.2/***** Point.java *****///* Author: Mary
public class Point { public int x;
public int y;
public Point(int a; int b) { //constructor(构造函数 ) x = a; y = b; } public void print() { //method System.out.print("The Point's coordinate is"); System.out.println("("+x+", "+y+")"); }} // end of the class ‘Point’
域
![Page 20: Java 程序设计 Java Programming Fall, 2013. 2 Contents for Today Java Program Structure How to Compile a Java Program How to Run a Java Program Environment](https://reader030.vdocuments.pub/reader030/viewer/2022013115/56649e215503460f94b0d658/html5/thumbnails/20.jpg)
20
Method(方法) Each method has 2 parts:
a header The header contains the name and other information
about the method.
a body The body describes what the method should do.
Braces { and } are used to mark the start and end of the method body.
A method body consists of a set of statements(语句 ).
![Page 21: Java 程序设计 Java Programming Fall, 2013. 2 Contents for Today Java Program Structure How to Compile a Java Program How to Run a Java Program Environment](https://reader030.vdocuments.pub/reader030/viewer/2022013115/56649e215503460f94b0d658/html5/thumbnails/21.jpg)
21
Example 2.1:
/***** HelloWorld.java *****///* Author: Mary
public class HelloWorld{ public static void main( String[ ] args ) {
System.out.println( "Hello, world!" );
}}
![Page 22: Java 程序设计 Java Programming Fall, 2013. 2 Contents for Today Java Program Structure How to Compile a Java Program How to Run a Java Program Environment](https://reader030.vdocuments.pub/reader030/viewer/2022013115/56649e215503460f94b0d658/html5/thumbnails/22.jpg)
22
Method(方法 )
In our first program Example 2.1, we have one method. The name of the method is main main is a special name. It means:
“when you run the program, start here.”
The header of main must be defined in the format as follows:
Our main method has only one statement(语句 ).
public static void main(String[ ] args) {…}
![Page 23: Java 程序设计 Java Programming Fall, 2013. 2 Contents for Today Java Program Structure How to Compile a Java Program How to Run a Java Program Environment](https://reader030.vdocuments.pub/reader030/viewer/2022013115/56649e215503460f94b0d658/html5/thumbnails/23.jpg)
23
Statements(语句 )
A statement ( 语句 ) represents an action or a sequence of actions.
System.out.println("Welcome to Java!");
a statement to display the greeting "Welcome to Java!" ;
Every statement in Java ends with a semicolon (;).
![Page 24: Java 程序设计 Java Programming Fall, 2013. 2 Contents for Today Java Program Structure How to Compile a Java Program How to Run a Java Program Environment](https://reader030.vdocuments.pub/reader030/viewer/2022013115/56649e215503460f94b0d658/html5/thumbnails/24.jpg)
24
Keywords(关键字)
Reserved Words/keywords(保留字 / 关键字 ) have a specific meaning to the compiler; cannot be used for other purposes in the program.
eg.: class, public, static, and void
![Page 25: Java 程序设计 Java Programming Fall, 2013. 2 Contents for Today Java Program Structure How to Compile a Java Program How to Run a Java Program Environment](https://reader030.vdocuments.pub/reader030/viewer/2022013115/56649e215503460f94b0d658/html5/thumbnails/25.jpg)
25
Java关键字具有特殊含义的字符序列,共 48个。
Abstract defaultif private thisBoolean do implements protected throwbreak double import public
throwsbyte else instanceof return transientcase extends int short trycatch final interface static voidchar finally long strictfp volatileclass float native super whileconst for new switchcontinue goto package synchronized
![Page 26: Java 程序设计 Java Programming Fall, 2013. 2 Contents for Today Java Program Structure How to Compile a Java Program How to Run a Java Program Environment](https://reader030.vdocuments.pub/reader030/viewer/2022013115/56649e215503460f94b0d658/html5/thumbnails/26.jpg)
26
Example 2.2/***** Point.java *****///* Author: Mary
public class Point { public int x;
public int y;
public Point(int a; int b) { x = a; y = b; } public void print(){ System.out.print("The Point's coordinate is"); System.out.println("("+x+", "+y+")"); }
} // end of the class ‘Point’
Attributes
Constructor
Class Header
Method
![Page 27: Java 程序设计 Java Programming Fall, 2013. 2 Contents for Today Java Program Structure How to Compile a Java Program How to Run a Java Program Environment](https://reader030.vdocuments.pub/reader030/viewer/2022013115/56649e215503460f94b0d658/html5/thumbnails/27.jpg)
27
Assignment(作业 )
1. Download Eclipse & J2SDK(Java Software Development Tool), and install them on your computers.
2. To create, compile, and run the HelloWorld.java program on your own computer, and get familiar with the commands and process.