Download CIS 175 Java Programming

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Falcon (programming language) wikipedia , lookup

Library (computing) wikipedia , lookup

C++ wikipedia , lookup

Class (computer programming) wikipedia , lookup

Java syntax wikipedia , lookup

Object-oriented programming wikipedia , lookup

Scala (programming language) wikipedia , lookup

C Sharp syntax wikipedia , lookup

Name mangling wikipedia , lookup

Java (programming language) wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Java performance wikipedia , lookup

Transcript
CIS 175 Java Programming
Lectures of Chuck Kelly
Chapter 1 Introduction to Java
Objectives
 Learn about Java and its history
 Understand the relationship between Java and the Web
 Find Java resources on the Web.
 Become familiar with Java development tools.
 Understand the Java runtime environment.
 Write a simple Java application.
 Write a simple Java applet.
Introduction
Java was developed by James Gosling at Sun Microsystems and was originally called Oak.
It was designed for use in embedded consumer electronic applications in 1991. It was
redesigned for developing Internet applications and renamed Java in 1995.
Java is partially modeled after C++ but has been simplified.
Java is Object Oriented
An object has properties and behaviors.
Properties are described by using data.
Behaviors are defined by using methods.
Objects are defined by using classes. (A class is like a template)
An Object is a real example of a class description.
The process of creating an object from a class is called instantiation.
Car
Class
A Red
Camaro object
A Black
Mustang object
A Java program consists of one or more classes.
Classes can inherit properties and behaviors from other classes.
Java has many pre-defined classes that you can use in your programs.
Object-oriented programming provides greater flexibility, modularity, and reusability.
Java is Distributed
Distributed computing allows networked computers to work together.
Networking capability is built in to Java.
Java is Interpreted
Java programs are run by a Java interpreter.
Java programs are compiled into Java Virtual Machine code called bytecode.
Bytecode is machine independent and can run on any machine that has a Java
interpreter.
Java is Multithreaded
Multithreading allows several tasks to be performed simultaneously.
Multithreading is very useful when programming for a GUI.
Java and the World Wide Web
Java programs called applets can be run by Web browsers.
Java Development Tools
Java Development Kit (JDK) may be download for free from
http://www.javasoft.com
Forte’ by Sun. at http://wwws.sun.com/software/sundev/jde/buy/index.html
Visual J++ by Microsoft
JBuilder by Borland
Java Applications
Applications are stand-alone programs.
Applications can be executed from any computer with a Java interpreter.
Example 1.1
//This application program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println(“Welcome to Java!”);
}
}
Comments
// All text after a double slash in the same line is ignored by the compiler
/* */ All text between these two symbols is ignored by the compiler. This can span
multiple lines.
Reserved Words
Reserved words have special meaning to the compiler.
The following are reserved words in Example 1.1:
public, class, static, void, main
Modifiers
Reserved words that specify properties of program components.
Examples are: public, static, private, final abstract,
protected, and protected private
Statements
A statement represents an action or a sequence of actions.
Every statement ends with a semicolon (;)
x = 5;
x = x + 5;
Blocks
Braces {} in a Java program form a block structure that groups statements together.
Classes
The class is the essential Java construct.
Every Java program has at least one class.
Methods
A method is a collection of statements that performs a sequence of operations.
Similar to the concept of functions, procedures or subroutines from other
programming languages.
Main
Each Java program must have a main() method.
The main() method always looks like this:
public static void main(String[] args) {
// statements;
}
Compiling a Java Program
To execute a Java program you have to compile it first.
The JDK command to compile is
Javac Welcome.java
If there are no syntax errors, the compiler generates a bytecode file named
Welcome.class.
Executing a Java Application
To run the bytecode file ‘Welcome.class’ use the command:
Java Welcome
Java Applets
Very similar to applications.
Do not need a main() method.
Run within a Web browser.
Example 1.2
//This applet prints Welcome to Java!
import java.awt.Graphics;
public class WelcomeApplet extends java.applet.Applet{
public void paint(Graphics g) {
g.drawString(“Welcome to Java!”,10, 10);
}
}
Applets run in a graphical environment.
The drawing area is measured in pixels, with (0,0) at the upper-left corner.
The import Statement
The import statement includes existing Java programs in the current program. This
allows you to reuse software.
Java code is organized into packages and classes.
Classes are inside packages, and packages are libraries of Java code that contain all
kinds of operations ready for you to import and use.
Java provides standard libraries that come with the compiler.
Users can create their own libraries.
Class Instance
The g in the paint() method is called an instance for class Graphics.
An instance is a concrete object of the class.
g can access all the methods defined in Graphics.
drawString() is a method in Graphics, which can now be use in g.
The paint() Method and the Graphics Class
Every applet that displays graphics must have a paint() method like this:
public void paint(Graphics g) {…}
The Java Graphic class provides operations for drawing objects, such as text strings,
lines, rectangles, ovals, arcs, and polygons.
public void paint(Graphics g) {
g.drawLine(10, 10, 50, 30);
g.drawRect(10, 20, 30, 50);
g.drawString(“Welcome to Java”, 30, 10);
}
Welcome to Java!
The extends Keyword and Class Inheritance
extends tells the compiler that the class to be defined is an extension of an existing
class.
The extended applet inherits all functionality and properties from the existing class.
Compiling an Applet
Same as a Java application.
javac WelcomeApplet
Running an Applet
Applets are executed by a Web browser from an HTML file.
Creating an HTML File
HTML – Hyper-Text Markup Language, the language of the World-Wide-Web
The following HTML file runs WelcomeApplet.class
<html>
<body>
<applet code=”WelcomeApplet.class”
width = 100 height = 40>
</applet>
</body>
</html>
The width and height attributes specify the rectangular viewing area of the applet.
Viewing Applets
To view the above applet you would start a Web browser that supports Java and load
the HTML file.
You can also use the Applet Viewer utility that is part of the JDK. The command to
view an applet, assuming the HTML file is called WelcomeApplet.html is:
appletviewer WelcomeApplet.html
Applications Versus Applets
For security reasons applets have the following restrictions.
Applets are not allowed to read from, or write to , the file system.
Applets are not allowed to run any programs on the browser’s computer.
Applets are not allowed to establish connections between the user’s computer
and another computer except with the server where the applets are stored.