Objects: Reusable Software Components That Model Real-World Items Look All Around You Attributes Behaviors (Methods)
Objects: Reusable Software Components That Model Real-World Items Look All Around You Attributes Behaviors (Methods)
php#java
•
•
Introducti
on
Fundame
Introduction
ntal The Java Programming Language
Programm Java Development Environment
ing
Concept Software Installation
• Decisions Compilation Process
• Applets & How to Compile and Run a Java Application
Graphics How to Compile and Run a Java Applet
• Introducti
on to
Classes
•
•
Methods
Arrays &
Vectors
The Java Programming
• Inheritanc
e&
Interfaces
Language
• Streams
and Objects
Exception
s Reusable software components that model real-world items
• Event Look all around you
Handling
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
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
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
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);
}
}