|
Introduction
The Java Programming Language
Java Development Environment
Software Installation
Compilation Process
How to Compile and Run a Java Application
How to Compile and Run a Java Applet
The Java Programming Language
Objects
Reusable software components that model real-world items
Look all around you
People, animals, plants, cars, etc.
Attributes
Size, shape, color, weight, etc.
Behaviors (Methods)
Cars: forward, backward, turn, etc.
Object-Oriented Programming
Object-oriented design (OOD)
Models real-world objects
Models communication among objects
Encapsulates data (attributes) and functions (behaviors)
Information hiding
Communication through well-defined interfaces
Object-oriented language
Programming is called object-oriented programming (OOP)
Java Programming Languages
Classes are a fundamental concept in Java.
It role is as ‘factories’ for objects.
An object is an entity that can be manipulated in a program.
All program statements are placed inside methods (functions or procedures).
A method is a collection of programming instructions for a particular task.
Java Class


Java Environment
Java programs normally undergo five phases
Edit
Programmer writes program and stores program on disk with extension .java
Compile
Compiler creates bytecodes from program and store a program in a file with extension .class
Load
Class loader stores bytecodes in memory
Execute (Interpret)
Interpreter translates bytecodes into machine language
Software Installation
Install Java 2 SDK
Compiler : j2sdk1_4_2-win.exe
Documentation: j2sdk1_4_2-doc.zip
Edit Autoexec.bat
SET PATH=c:\jdk1.4.2\bin;%PATH%
SET CLASSPATH= .;c:\working_directory
Compilation Process


**Figure from java.sun.com
How to Compile and Run a Java Application
Edit-Compile-Debug Loop
1. To Edit :
- use any text editor such as Notepad
- save source code with extension .java
2. To Compile : use the command
javac Hello.java
if no error, a file names “Hello.class” is created
3. To Run : use command
java Hello
(java virtual machine)

Java Syntax
1.1 Simple Program
public class ClassName
{ public static void main(String[] args)
{ statements
}
}
1.2 Method Call
object.methodName(parameters)
Example: System.out.println(“Hi”);
A Simple Java Application
1. // Example 1 : Hello Application
2. // Hello.java
3.
4. public class Hello {
5. public static void main(String args[]) {
6. System.out.println(“Hello!”);
7. }
8. }
Java
Java is case-sensitive.
On line 4, the keyword “public” indicates that the class is usable by the “public”
The name of the public class must be the same as the file name
Line 5 defines a method called main
String[ ] args is a parameter that contains the command line arguments
Every statement must end with a semicolon
Method in a particular object can be called by
class.object.method
For example:
System.out.println
System.out.println(“Hello”) statement calls method println of object names System.out that prints “Hello”
“Hello” is a string which is enclosed insidedouble quotation marks
System.out.println(5 + 4); // 9
9
System.out.println(“5 + 4”); // 5 + 4
5 + 4
System.out.print(“Hello”); System.out.println(“Java”);
HelloJava
Escape Sequences
To print
Hello "Java"
Can’t use
System.out.println("Hello "Java"");
Must use escape sequence (\)
System.out.println("Hello \"Java\"");
System.out.println("c:\\data\\a.txt"); will print
c:\data\a.txt
\n starts a new line, for example:
System.out.print(“A\nB\nC”); will print
A
B
C
\t moves the screen cursor to the next tab stop
\r positions the cursor to the beginning of the current line
Import
To use classes from Java Class Library, we need to import those classes from a package in the library.
import packageName.Classname;
Package is a collection of classes with a related purpose.
For example:
import java.applet.applet;
A Simple Java Applet
// Example 2: HelloApplet
import java.applet.Applet;
import java.awt.Graphics;
public class HelloApplet extends Applet {
public void paint(Graphics g) {
g.drawString(“Hello !”, 25, 25);
}
}
How to Compile and Run a Java Applet
To Compile
c:> javac HelloApplet.java
To Run an applet, we need HelloApplet.html
<HTML>
<APPLET code=”HelloApplet.class” width=500 height=300 </APPLET>
</HTML>
Then, To run this applet,
c:> appletviewer HelloApplet.html
or use any browsers, e.g. Netscape, Internet Explorer.
|
|