What is Java?

Download Report

Transcript What is Java?

What is Java?
Java technology is both a programming
language and a platform;
Programming Language :
Modern high-level language (Simple, Architecture neutral, Object oriented,
Portable, Distributed, High performance, Interpreted, Multithreaded,
Robust, Dynamic, Secure)
The Java programming language is unusual in
that a program is both compiled and interpreted.
Created by Ron Beglieter (based
on the Java Tutorial)
1
Java Programming Lang.
“Write once, run anywhere”
Generates byteCode
Implementation of the
Java Virtual Machine
(JVM) Spec.
2
Java Platform
The Java platform has two components:
– The Java Virtual Machine (Java VM)
– The Java Application Programming Interface
(Java API)
3
HelloWorld Application
/**
* The HelloWorldApp class implements an application that
* simply displays "Hello World!" to the standard output.
*/
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
4
Hello World App. (2)
HelloWorldApp.java
/**
* The HelloWorldApp class
implements an application that
* simply displays "Hello World!" to
c:\app>
class Robber {
class javac
Bank HelloWorldApp.java
{
c:\app>
…
… dir
HelloWorldApp.java
}
}
HelloWorldApp.class
Bank.java
Robber.java
c:\app> java HelloWorldApp
Hello World!
the standard output.
*/
class HelloWorldApp {
c:\app>
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
5
Object Oriented Concepts
What Is an Object?
An object is a software bundle of related variables and methods. Software
objects are often used to model real-world objects you find in everyday life.
What Is a Message?
Software objects interact and communicate with each other using messages.
What Is a Class?
A class is a blueprint or prototype that defines the variables and the methods
common to all objects of a certain kind.
What Is Inheritance?
A class inherits state and behavior from its superclass. Inheritance provides a
powerful and natural mechanism for organizing and structuring software
programs.
What Is an Interface?
An interface is a contract in the form of a collection of method and constant
declarations. When a class implements an interface, it promises to
implement all of the methods declared in that interface.
6
An Example
The following example demonstrates OOP (Object Oriented Programming) key concepts as
well as the Java language basics
1. public class Spot {
2.
public int size;
3.
public int x, y;
Attributes
4.
public Spot(int intSize) {
5.
size = intSize;
6.
x = -1;
7.
y = -1;
8.
}
9. }
Constructor
7
Example (continued)
import java.awt.*;
“same” as #include
import java.awt.event.*;
import javax.swing.*;
public class ClickMe extends JPanel implements
MouseListener {
private Spot spot = null;
private static final int RADIUS = 7;
public void init() {
addMouseListener(this);
}
…. // full code is supplied
8
Example (continued)
public class ClickMe extends JPanel implements
MouseListener { … // the missing code is supplied
public static void main(String args[]) {
ClickMe clicker = new ClickMe();
clicker.init();
JFrame frame = new JFrame();
frame.getContentPane().add(clicker,
BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
9
Example (continued)
public void mousePressed(MouseEvent event) {
if (spot == null) {
spot = new Spot(RADIUS);
}
spot.x = event.getX();
spot.y = event.getY();
repaint();
}
10
Quiz
Use the API documentation for the Java 2 Platform to answer these
questions:
The ClickMe application uses Color.red to set the drawing color to red.
1.
What other colors can you get by name like this?
2.
How would you specify other colors, like purple?
Now, use what you learned from the Java API to make the following
modifications to the ClickMe Program: (To compile this program,
you also need the Spot.java file.)
1.
Modify the application to draw a green square instead of a red
spot.
2.
Modify the application to display your name in purple instead of a
red spot.
11
Summary – Java Class
12